Generating PieChart using JFreeChart

This code snippet will create a PNG image from the given set of data using JFreeChart.

//Creating the dataset 
DefaultPieDataset pieDataset = new DefaultPieDataset(); 
pieDataset.setValue("Subin", new Integer(70)); 
pieDataset.setValue("Sachin", new Integer(33)); 
pieDataset.setValue("Salman", new Integer(43)); 
pieDataset.setValue("SRK", new Integer(6));
JFreeChart chart = ChartFactory.createPieChart( 
    "Sample Pie-Chart created using JFreeChart!", pieDataset, true, 
    true, true);
chart.setTitle(""); 
chart.setAntiAlias(true); 
chart.setTextAntiAlias(false);
PiePlot plot = (PiePlot) chart.getPlot(); 
plot.setLabelFont(new Font("Tahoma", Font.PLAIN, 11)); 
try 
{ 
    // Converting the PieChart into a PNG image 
    File image = new File("MyChart.png"); 
    ChartUtilities.saveChartAsPNG(image, chart, 700, 200); 
    System.out.println("[done]"); 
    System.out.println(image.getAbsolutePath()); 
} 
catch (java.io.IOException exc) 
{ 
    System.err.println("Error writing image to file"); 
}
Advertisements