分类: Java
2008-04-05 16:14:11
Introduction JFreeChart supports the use of multiple axes and datasets in the CategoryPlot and XYPlot classes. You can use this feature to display two or more datasets on a single chart, while making allowance for the fact that the datasets may contain data of vastly different magnitudes. Refer to 1.bmp. Typical charts constructed with JFreeChart use a plot that has a single dataset, a single renderer, a single domain axis and a single range axis. However, it is possible to add multiple datasets, renderers and axes to a plot. |
Steps Create a Chart To create a chart with multiple axes, datasets, and renderers, you should first create a regular chart (for example, using the ChartFactory class). You can use any chart that is constructed using a CategoryPlot or an XYPlot. In the example, a time series chart is created as follows: XYDataset dataset1 = createDataset( "Series 1", 100.0,new Minute(), 200); JFreeChart chart = ChartFactory.createTimeSeriesChart( "Multiple Axis Demo 1", "Time of Day", "Primary Range Axis", dataset1, true, true, false ); Adding an Additional Axis To add an additional axis to a plot, you can use the setRangeAxis() method: NumberAxis axis2 = new NumberAxis("Range Axis 2"); plot.setRangeAxis(1, axis2); plot.setRangeAxisLocation(1, AxisLocation.BOTTOM OR RIGHT); Note that an index of 1 (one) has been used—you can add as many additional axes as you require, by incrementing the index each time you add a new axis. The setRangeAxisLocation() method allows you to specify where the axis will appear on the chart, using the AxisLocation class. In the example, BOTTOM OR RIGHT is specified, which means (for a range axis) on the right if the plot has a vertical orientation, or at the bottom if the plot has a horizontal orientation. At this point, no additional dataset has been added to the chart, so if you were to display the chart you would see the additional axis, but it would have no data plotted against it. Adding an Additional Dataset To add an additional dataset to a plot, use the setDataset() method: XYDataset dataset2 = ... // up to you plot.setDataset(1, dataset2); Combine the newly-added axis and dataset: By default, the dataset will be plotted against the primary range axis. To have the dataset plotted against a different axis, use the mapDatasetToDomainAxis() and mapDatasetToRangeAxis() methods. These methods accept two arguments, the first is the index of the dataset, and the second is the index of the axis. Adding an Additional Renderer When you add an additional dataset, usually it makes sense to add an additional renderer to go with the dataset. Use the setRenderer() method: XYItemRenderer renderer2 = ... // up to you plot.setRenderer(1, renderer2); The index (1 in this case) should correspond to the index of the dataset added previously. #It's almost a must to add a new renderer,when you add a new dataset. Note: if you don’t specify an additional renderer, the primary renderer will be used instead. In that case, the series colors will be shared between the primary dataset and the additional dataset.
|