• Nie Znaleziono Wyników

A Bar Chart

W dokumencie Version 0.9.18 (Stron 38-43)

Bar Charts

5.2.2 The Dataset

The first step in generating the chart is to create a dataset. You can use any class that implements the CategoryDatasetinterface—for the example, we have used theDefaultCategoryDatasetclass (included in the JFreeChart distribution):

/**

* Returns a sample dataset.

*

* @return The dataset.

*/

private CategoryDataset createDataset() { // row keys...

String series1 = "First";

String series2 = "Second";

String series3 = "Third";

// column keys...

String category1 = "Category 1";

String category2 = "Category 2";

String category3 = "Category 3";

String category4 = "Category 4";

String category5 = "Category 5";

// create the dataset...

DefaultCategoryDataset dataset = new DefaultCategoryDataset();

dataset.addValue(1.0, series1, category1);

dataset.addValue(4.0, series1, category2);

dataset.addValue(3.0, series1, category3);

dataset.addValue(5.0, series1, category4);

dataset.addValue(5.0, series1, category5);

dataset.addValue(5.0, series2, category1);

dataset.addValue(7.0, series2, category2);

dataset.addValue(6.0, series2, category3);

dataset.addValue(8.0, series2, category4);

dataset.addValue(4.0, series2, category5);

dataset.addValue(4.0, series3, category1);

dataset.addValue(3.0, series3, category2);

dataset.addValue(2.0, series3, category3);

dataset.addValue(3.0, series3, category4);

dataset.addValue(6.0, series3, category5);

return dataset;

}

Notice that we have used String objects as the row and column keys for the data values. You can use any class that implements theComparableinterface as the keys for your data values.

5.2.3 Constructing the Chart

ThecreateBarChart() method in the ChartFactoryclass provides a convenient way to create the chart:1

// create the chart...

JFreeChart chart = ChartFactory.createBarChart(

"Bar Chart Demo", // chart title

"Category", // domain axis label

"Value", // range axis label

1Take a look at the source code for this method, if you are interested to know how the bar chart is constructed from the components (axes, plots, renderers etc.) in the JFreeChart library.

dataset, // data PlotOrientation.VERTICAL,

true, // include legend

true, // tooltips?

false // URLs?

);

This method constructs aJFreeChartobject with a title, legend, and plot with appropriate axes, renderer and tooltip generator. Thedatasetis the one created in the previous section.

5.2.4 Customising the Chart

The chart will be initialised using default settings for most attributes. You are, of course, free to modify any of the settings to change the appearance of your chart. In this example, several attributes are modified:

• the chart background color;

• the “auto tick units” on the range axis (so that the tick labels always display integer values);

• gradient paint is used for the series colors;

Changing the chart’s background color is simple, because this is an attribute maintained by theJFreeChartclass:

// set the background color for the chart...

chart.setBackgroundPaint(new Color(0xBBBBDD));

To change other attributes, we first need to obtain a reference to theCategoryPlot object used by the chart:

CategoryPlot plot = chart.getCategoryPlot();

The range axis is modified so that the tick units are always integers:

// change the auto tick unit selection to integer units only...

NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();

rangeAxis.setStandardTickUnits(TickUnits.createIntegerTickUnits());

The bar renderer is modified so that bar outlines are not drawn, andGradientPaint instances are used for the series colors:

// disable bar outlines...

BarRenderer renderer = (BarRenderer) plot.getRenderer();

renderer.setDrawBarOutline(false);

// set up gradient paints for series...

GradientPaint gp0 = new GradientPaint(

0.0f, 0.0f, Color.blue, 0.0f, 0.0f, Color.lightGray );

GradientPaint gp1 = new GradientPaint(

0.0f, 0.0f, Color.green, 0.0f, 0.0f, Color.lightGray );

GradientPaint gp2 = new GradientPaint(

0.0f, 0.0f, Color.red, 0.0f, 0.0f, Color.lightGray );

renderer.setSeriesPaint(0, gp0);

renderer.setSeriesPaint(1, gp1);

renderer.setSeriesPaint(2, gp2);

Refer to the source code, Javadoc API documentation or elsewhere in this doc-ument for details of the other customisations that you can make to a bar plot.

5.2.5 The Complete Program

The code for the demonstration application is presented in full, complete with the import statements. You should find this code included in the JFreeChart distribution.

package org.jfree.chart.demo;

import java.awt.Color;

import java.awt.GradientPaint;

import org.jfree.chart.ChartFactory;

import org.jfree.chart.ChartPanel;

import org.jfree.chart.JFreeChart;

import org.jfree.chart.axis.NumberAxis;

import org.jfree.chart.plot.CategoryPlot;

import org.jfree.chart.plot.PlotOrientation;

import org.jfree.chart.renderer.BarRenderer;

import org.jfree.data.CategoryDataset;

import org.jfree.data.DefaultCategoryDataset;

import org.jfree.ui.ApplicationFrame;

import org.jfree.ui.RefineryUtilities;

/**

* A simple demonstration application showing how to create a bar chart.

*

* @author David Gilbert

*/

public class BarChartDemo extends ApplicationFrame { /**

* Creates a new demo instance.

*

* @param title the frame title.

*/

public BarChartDemo(String title) { super(title);

CategoryDataset dataset = createDataset();

JFreeChart chart = createChart(dataset);

// add the chart to a panel...

ChartPanel chartPanel = new ChartPanel(chart);

chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));

setContentPane(chartPanel);

} /**

* Returns a sample dataset.

*

* @return The dataset.

*/

private CategoryDataset createDataset() { // row keys...

String series1 = "First";

String series2 = "Second";

String series3 = "Third";

// column keys...

String category1 = "Category 1";

String category2 = "Category 2";

String category3 = "Category 3";

String category4 = "Category 4";

String category5 = "Category 5";

// create the dataset...

DefaultCategoryDataset dataset = new DefaultCategoryDataset();

dataset.addValue(1.0, series1, category1);

dataset.addValue(4.0, series1, category2);

dataset.addValue(3.0, series1, category3);

dataset.addValue(5.0, series1, category4);

dataset.addValue(5.0, series1, category5);

dataset.addValue(5.0, series2, category1);

dataset.addValue(7.0, series2, category2);

dataset.addValue(6.0, series2, category3);

dataset.addValue(8.0, series2, category4);

dataset.addValue(4.0, series2, category5);

dataset.addValue(4.0, series3, category1);

dataset.addValue(3.0, series3, category2);

dataset.addValue(2.0, series3, category3);

dataset.addValue(3.0, series3, category4);

dataset.addValue(6.0, series3, category5);

return dataset;

} /**

* Creates a sample chart.

*

* @param dataset the dataset.

*

* @return The chart.

*/

private JFreeChart createChart(CategoryDataset dataset) { // create the chart...

JFreeChart chart = ChartFactory.createBarChart(

"Bar Chart Demo", // chart title

"Category", // domain axis label

"Value", // range axis label

dataset, // data

PlotOrientation.VERTICAL,

true, // include legend

true, // tooltips?

false // URLs?

);

// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...

// set the background color for the chart...

chart.setBackgroundPaint(new Color(0xBBBBDD));

// get a reference to the plot for further customisation...

CategoryPlot plot = chart.getCategoryPlot();

// set the range axis to display integers only...

NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();

rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

// disable bar outlines...

BarRenderer renderer = (BarRenderer) plot.getRenderer();

renderer.setDrawBarOutline(false);

// set up gradient paints for series...

GradientPaint gp0 = new GradientPaint(

0.0f, 0.0f, Color.blue, 0.0f, 0.0f, Color.lightGray );

GradientPaint gp1 = new GradientPaint(

0.0f, 0.0f, Color.green, 0.0f, 0.0f, Color.lightGray );

GradientPaint gp2 = new GradientPaint(

0.0f, 0.0f, Color.red, 0.0f, 0.0f, Color.lightGray );

renderer.setSeriesPaint(0, gp0);

renderer.setSeriesPaint(1, gp1);

renderer.setSeriesPaint(2, gp2);

// OPTIONAL CUSTOMISATION COMPLETED.

return chart;

} /**

* Starting point for the demonstration application.

*

* @param args ignored.

*/

public static void main(String[] args) {

BarChartDemo demo = new BarChartDemo("Bar Chart Demo");

demo.pack();

RefineryUtilities.centerFrameOnScreen(demo);

demo.setVisible(true);

} }

W dokumencie Version 0.9.18 (Stron 38-43)

Powiązane dokumenty