Jul 9, 2010

Generating Pie Charts in Java

Introduction

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.

Installation

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

clip_image002

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

clip_image004

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

clip_image006

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

clip_image008

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

clip_image010

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

clip_image012

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

clip_image014

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

f.setSize(450,320);

f.show();

clip_image016

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

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

String file = new

String("C:\\Vinay\\Software\\EasyChart\\SimplePieChart.jpg");

FileOutputStream fos = new FileOutputStream(file);

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

encoder.Compress();

fos.flush();

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

clip_image020

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

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

clip_image022

Different Styles of Labels

· To create sample labels for each slice use:

pieChart.setValueLabelsOn(false);

pieChart.setSampleLabelsOn(true);

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

pieChart.setSampleLabelStyle(Chart.INSIDE);

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

clip_image024

· To create pointing label use:

pieChart.setSampleLabelStyle(Chart.POINTING);

clip_image026

Making Pie Chart 3D

  • To have 3D effect use:

pieChart.set3DModeOn(true);

clip_image028

  • To set the angle of the pie chart use:

pieChart.setAngle(60);

clip_image030

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

pieChart.setDepth(0.2);

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

clip_image034

· To hide the slice separators use:

pieChart.setSliceSeparatorOn(false);

clip_image036

· To show the slice separator use:

pieChart.setSliceSeparatorOn(true);

pieChart.setSliceSeparatorColor(Color.BLACK);

clip_image038

· To set the graphinsets use:

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

clip_image040

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

clip_image042

0 comments:

Text Widget

Copyright © Vinay's Blog | Powered by Blogger

Design by | Blogger Theme by