JavaFx BarChart 2("NY Times Article Search API")
This a continuation from my previously barchart post.In this post were are going to use the
NY TIMES Article Search API to query for articles with the headline "kenya" whats fascinating about this data is.it dates back to articles from 1933 and a total of 1335 articles.
Preview All headline
The article search api has a very useful console to try out your own queries here.My URI looks like this
http://api.nytimes.com/svc/search/v2/articlesearch.json?fq=headline%3A%28%22KENYA%22%29+AND+glocations%3A%28%22KENYA%22%29&fl=headline%2Cpub_date&api-key=sample-key
You need to register for an API key first here,It will be used to track your application usage
Key rate limits are 10 calls per/sec.and limits the pages to the first 1000 pages.
To work around this you can use filters to narrow your search.
So i used filters in standard Lucene syntax .i.e headline:("KENYA") AND glocations:("KENYA")
and restricted the number of fields returned .Will be working with the pub_date and headlines only.
I was able to get all 1335 headlines ,I sorted the results from oldest to newest after the page limit i changed the results from newest to oldest.little bit of coding and manual work the data was ready.
The results from the API Console
Sending the Request
//throws IOException
URL url = new URL(link);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String output;
StringBuilder sb=new StringBuilder();
while ((output = br.readLine()) != null) {
sb.append(output);
sb.append("\n");
}
conn.close();
Getting the headline and date from json format
//throws JSONException
JSONObject ob=new JSONObject(sb.toString());
JSONObject response=new JSONObject(ob.get("response").toString());
JSONArray docs=new JSONArray(response.get("docs").toString());
for(int i=0;i<docs.length();i++){
JSONObject obj=new JSONObject(docs.get(i).toString());
JSONObject headline=new JSONObject(obj.get("headline").toString());
String main_headline=headline.get("main").toString();
String date=obj.get("pub_date").toString();
System.out.println(main_headline+"\t"+date);
}
Chart Config
//DateAxis config
dateAxis.setTickLabelFill(Color.GREY);
dateAxis.setTickLabelFont(Font.font(16));
dateAxis.setTickLabelRotation(45);
//NumberAxis config
numberAxis.setTickLabelFill(Color.BLACK);
numberAxis.setTickLabelGap(1);
numberAxis.setLabel("Articles per Year");
numberAxis.setTickMarkVisible(false);
numberAxis.setMinorTickVisible(false);
//BarChart config
barChart.setTitle("NewYork Times Articles Search");
barChart.setVerticalGridLinesVisible(false);
barChart.setVerticalZeroLineVisible(false);
barChart.setHorizontalGridLinesVisible(false);
barChart.setLegendVisible(false);barChart.setHorizontalZeroLineVisible(false);
barChart.setMaxHeight(700); barChart.setMaxWidth(1200);
barChart.setMinHeight(700);barChart.setMaxWidth(1200);
barChart.setBarGap(-13);
barChart.setCategoryGap(0);
Chart css
.chart {
-fx-background-color:black;
-fx-padding: 15 25 15 15;
-fx-border-color: black;
}
.chart-bar {
-fx-background-color:linear-gradient(to top,orange,orangered);
-fx-border-color:yellowgreen;
-fx-border-width:1;
}
.axis-label {
-fx-text-fill: black;
}
The barchart has two XYChart.Series one at the bottom and the other one at the top.I just add a negative to achieve the results.
I wanted to compare two keywords frequency over the years.
Preview
The words kill and attack
The words MAU and BRITISH
NY TIMES Article Search API to query for articles with the headline "kenya" whats fascinating about this data is.it dates back to articles from 1933 and a total of 1335 articles.
Preview All headline
The article search api has a very useful console to try out your own queries here.My URI looks like this
http://api.nytimes.com/svc/search/v2/articlesearch.json?fq=headline%3A%28%22KENYA%22%29+AND+glocations%3A%28%22KENYA%22%29&fl=headline%2Cpub_date&api-key=sample-key
You need to register for an API key first here,It will be used to track your application usage
Key rate limits are 10 calls per/sec.and limits the pages to the first 1000 pages.
To work around this you can use filters to narrow your search.
So i used filters in standard Lucene syntax .i.e headline:("KENYA") AND glocations:("KENYA")
and restricted the number of fields returned .Will be working with the pub_date and headlines only.
I was able to get all 1335 headlines ,I sorted the results from oldest to newest after the page limit i changed the results from newest to oldest.little bit of coding and manual work the data was ready.
The results from the API Console
Sending the Request
//throws IOException
URL url = new URL(link);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String output;
StringBuilder sb=new StringBuilder();
while ((output = br.readLine()) != null) {
sb.append(output);
sb.append("\n");
}
conn.close();
Getting the headline and date from json format
//throws JSONException
JSONObject ob=new JSONObject(sb.toString());
JSONObject response=new JSONObject(ob.get("response").toString());
JSONArray docs=new JSONArray(response.get("docs").toString());
for(int i=0;i<docs.length();i++){
JSONObject obj=new JSONObject(docs.get(i).toString());
JSONObject headline=new JSONObject(obj.get("headline").toString());
String main_headline=headline.get("main").toString();
String date=obj.get("pub_date").toString();
System.out.println(main_headline+"\t"+date);
}
Chart Config
//DateAxis config
dateAxis.setTickLabelFill(Color.GREY);
dateAxis.setTickLabelFont(Font.font(16));
dateAxis.setTickLabelRotation(45);
//NumberAxis config
numberAxis.setTickLabelFill(Color.BLACK);
numberAxis.setTickLabelGap(1);
numberAxis.setLabel("Articles per Year");
numberAxis.setTickMarkVisible(false);
numberAxis.setMinorTickVisible(false);
//BarChart config
barChart.setTitle("NewYork Times Articles Search");
barChart.setVerticalGridLinesVisible(false);
barChart.setVerticalZeroLineVisible(false);
barChart.setHorizontalGridLinesVisible(false);
barChart.setLegendVisible(false);barChart.setHorizontalZeroLineVisible(false);
barChart.setMaxHeight(700); barChart.setMaxWidth(1200);
barChart.setMinHeight(700);barChart.setMaxWidth(1200);
barChart.setBarGap(-13);
barChart.setCategoryGap(0);
Chart css
.chart {
-fx-background-color:black;
-fx-padding: 15 25 15 15;
-fx-border-color: black;
}
.chart-bar {
-fx-background-color:linear-gradient(to top,orange,orangered);
-fx-border-color:yellowgreen;
-fx-border-width:1;
}
.axis-label {
-fx-text-fill: black;
}
The barchart has two XYChart.Series one at the bottom and the other one at the top.I just add a negative to achieve the results.
I wanted to compare two keywords frequency over the years.
Preview
The words kill and attack
The words MAU and BRITISH
Comments
Post a Comment