JFreeChart does the job

It was quite a busy month before, I rarely even had time to post an entry. Luckily I am here now to tell about this exciting tool from the java community. It's called JFreeChart. Our team decided to use it(ehem, thanks to my research) and it seems to do a very good job. Although, the online documentation isn't complete... You have to buy it which doesn't make JFreeChart a free software after all. But with or without the docs they sell you can still use it effectively.


Here's a snipper of code that loads a data from a database, puts them in a List and sets the value for a PieChart. This is by no means a complete code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    public JFreeChart createChart()
{
     DAOFactory dao = DAOFactory.getDAOFactory(DAOFactory.MYSQL);
    
GeographicDAO geographicdao = dao.getGeographicDAO();
  
     ArrayList<CountryBean> countryList = geographicdao.selectCountryList(magazine, timeStamp);

     geographicdao.freeConnection();
    
     DefaultPieDataset dataSet = new DefaultPieDataset();
    
     Country country = null;
    
     for (int i = 0; i < countryList.size(); i++)
     {
       country= countryList.get(i);
      
       dataSet.setValue( country.getCountryName() + " = " + country.getPercentage() + "%", Float.parseFloat( country.getPercentage() ) );
      
       country = null;
     }

     chart = ChartFactory.createPieChart3D("Country graph", dataSet, true, true, false);
    
   return chart;
}

public void writeChart(OutputStream ostream, int width, int height) throws IOException
{
  ChartUtilities.writeChartAsJPEG( ostream, chart, width, height);
}


And here's how it might look



Published 10-26-2006 9:28 PM by lamia
Filed under: