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 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

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(); 
           BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
   String output;
           StringBuilder sb=new StringBuilder(); 

          while ((output = br.readLine()) != null) {


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();

Chart Config
       //DateAxis config
        //NumberAxis config
         numberAxis.setLabel("Articles per Year");
        //BarChart config
         barChart.setTitle("NewYork Times Articles Search");
         barChart.setMaxHeight(700); barChart.setMaxWidth(1200);

Chart css

.chart {
    -fx-padding: 15 25 15 15;    
    -fx-border-color: black;
.chart-bar {
-fx-background-color:linear-gradient(to top,orange,orangered);
.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.

The words kill and attack
The words MAU and BRITISH


Popular Posts