Jul 9, 2010

Generating Pie Charts in Java


EasyCharts is 100% Java based charting library that enables the user to embed charts in the java applications and web pages with very little coding effort. The library contains AWT based chart components, applets and a chart servlet. Antialiasing, gradient colors, transparency and rounded bars and lines improve the appearance of the chart.

Key Features

  • Bar and Column Charts – Create bar charts with multiple data series, configurations, styles and colors. Bars can be placed vertically or horizontally, side-by-side, stacked or behind each other.
  • Line and Area Charts – Create Line and Area charts with different line styles, stacked data series, mouse-over labels and sample point markers. The charts can be zoomed by dragging the mouse across the chart data.
  • Plotter and Bubble Charts – Create a wide array of plotter and bubble charts with many different configurations. Set the size and shapes of the plots. Display free standing plots or connect to form data series.
  • Pie Charts – Create 2D or 3D pie charts with multiple data series, detached pies, mouse-over labels and drilldown functionality.
  • Combination Charts – Combine different bar, line and area charts into new great looking combination charts. There is no limit to the number of charts that can be stacked on top of each other.
  • Gauge and Dial charts – This helps to create speedometer-type charts for network utilization and other applications.


Please follow the following steps to install the jar file:

  • Go to http://www.objectplanet.com
  • From “Products” choose “EasyCharts”.
  • Place the jar files (chart.jar, chart.ext.jar, chartServer.jar) in the lib directory of the ear file.

In this technical document I will cover the different aspects of creating a Pie Chart.

Simple Pie Chart

Please follow the following steps to create a simple pie chart:

  • To assign values to the pie chart’s slices use:

double[] sampleValues = new double[] {643, 257, 825, 829, 376, 500, 450};

  • To assign colors to the different pie chart’s slices use:

Color[] sampleColors = new Color[] {Color.BLACK, Color.BLUE, Color.CYAN, Color.LIGHT_GRAY, Color.MAGENTA, Color.GREEN, Color.YELLOW};

  • To assign legends to the different pie chart’s slices use:

String[] legendLables = new String[] {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”};

  • To set the title of the chart use:
    • setTitleOn(true) and setTitle(“Days in a week”) – It will show the title as “Days in a week”.


    • setTitleOn(false) and setTitle(“Days in a week”) – It will not show the title in the chart even if the setTitle() has been set.


  • To set the fonts use:
    • setFont(“titleFont”, new Font(“Serif”, Font.BOLD, 12)) – This will set the font of the title.
    • setFont(“insideLabelFont”, new Font(“Serif”, Font.BOLD, 14)) – This will set the font of the slice labels.
    • setFont(“legendFont”, new Font(“Serif”, Font.PLAIN, 13)) – This will set the font of the slice labels.
  • To show the value labels use:
    • setValueLablesOn(true) and setValueLabelStyle(Chart.INSIDE) – This will show the value labels inside the slices.


    • setValueLablesOn(false) and setValueLabelStyle(Chart.INSIDE) – It will not show the value labels even if the setValuesLableStyle() has been set.


    • setValueLableStyle(Chart.OUTSIDE) – It will show the values labels outside the slices.


  • To show the legends use:
    • setLegendOn(true) – This will show the legends of the pie chart.


    • setLegendOn(false) – This will not show the legends of the pie chart.


  • Viewing the chart:
    • To view the chart inside a frame use:

com.objectplanet.chart.NonFlickerPanel p = new

com.objectplanet.chart.NonFlickerPanel(new BorderLayout());

p.add("Center", pieChart);

Frame f = new Frame();

f.add("Center", p);




    • To generate the chart in a JPEG format and store use:

Image image = pieChart.getImage(300, 200);

String file = new


FileOutputStream fos = new FileOutputStream(file);

JpegEncoder encoder = new JpegEncoder(image, 75, fos);



· Multiple Data Series

When the chart has multiple data series the number of pies will equal the number of samples and the number of segments per pie will equal the number of data series.

  • Basic Multiple Data Series Pie Chart

a. setSeriesCount(3) – This will set the number of series means it will designate each pie will contain 3 slices.

b. setSampleCount(4) – This will set the number of Pies in the chart.

c. setSampleValues(0, new double[] {100, 150, 200, 250}) – Sets the sample values for the specified data series. “0”, “1”, “2” designate the series count.


  • Showing the legends for each slice in a pie use:

setSeriesLabels(new String[] {“Dept A”, “Dept B”, “Dept C”}) and setLegendOn(true)


Different Styles of Labels

· To create sample labels for each slice use:



pieChart.setSampleLabels(new String[] {"Dept A", "Dept B", "Dept C", "Dept D", "Dept E"});


pieChart.setFont(“insideLabelFont”, new Font(“Serif”, Font.BOLD, 14));


· To create pointing label use:



Making Pie Chart 3D

  • To have 3D effect use:



  • To set the angle of the pie chart use:



  • To set the depth of the 3D pie chart use:


  • clip_image032

· Other Features

· To show slices in detached mode use:

pieChart.setDetachedSlice(<series number>, <slice number>, <distance>) like pieChart.setDetachedSlice(0, 3, 0.05);


· To hide the slice separators use:



· To show the slice separator use:




· To set the graphinsets use:

pieChart.setGraphInsets(-1, -1, -1, -1);


pieChart.setGraphInsets(-1, 40, -1, 40);



Text Widget

Copyright © Vinay's Blog | Powered by Blogger

Design by | Blogger Theme by