📄 chartfactory.java
字号:
/* ======================================
* JFreeChart : a free Java chart library
* ======================================
*
* Project Info: http://www.jfree.org/jfreechart/index.html
* Project Lead: David Gilbert (david.gilbert@object-refinery.com);
*
* (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* -----------------
* ChartFactory.java
* -----------------
* (C) Copyright 2001-2003, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): Serge V. Grachov;
* Joao Guilherme Del Valle;
* Bill Kelemen;
* Jon Iles;
* Jelai Wang;
* Richard Atkinson;
*
* $Id: ChartFactory.java,v 1.29 2003/08/06 15:15:01 mungady Exp $
*
* Changes
* -------
* 19-Oct-2001 : Version 1, most methods transferred from JFreeChart.java (DG);
* 22-Oct-2001 : Added methods to create stacked bar charts (DG);
* Renamed DataSource.java --> Dataset.java etc. (DG);
* 31-Oct-2001 : Added 3D-effect vertical bar and stacked-bar charts, contributed by
* Serge V. Grachov (DG);
* 07-Nov-2001 : Added a flag to control whether or not a legend is added to the chart (DG);
* 17-Nov-2001 : For pie chart, changed dataset from CategoryDataset to PieDataset (DG);
* 30-Nov-2001 : Removed try/catch handlers from chart creation, as the exception are now
* RuntimeExceptions, as suggested by Joao Guilherme Del Valle (DG);
* 06-Dec-2001 : Added createCombinableXXXXXCharts methods (BK);
* 12-Dec-2001 : Added createCandlestickChart(...) method (DG);
* 13-Dec-2001 : Updated methods for charts with new renderers (DG);
* 08-Jan-2002 : Added import for com.jrefinery.chart.combination.CombinedChart (DG);
* 31-Jan-2002 : Changed the createCombinableVerticalXYBarChart(...) method to use renderer (DG);
* 06-Feb-2002 : Added new method createWindPlot(...) (DG);
* 23-Apr-2002 : Updates to the chart and plot constructor API (DG);
* 21-May-2002 : Added new method createAreaChart(...) (JI);
* 06-Jun-2002 : Added new method createGanttChart(...) (DG);
* 11-Jun-2002 : Renamed createHorizontalStackedBarChart() --> createStackedHorizontalBarChart() for
* consistency (DG);
* 06-Aug-2002 : Updated Javadoc comments (DG);
* 21-Aug-2002 : Added createPieChart(CategoryDataset) method (DG);
* 02-Oct-2002 : Fixed errors reported by Checkstyle (DG);
* 09-Oct-2002 : Added methods including tooltips and URL flags (DG);
* 06-Nov-2002 : Moved renderers into a separate package (DG);
* 18-Nov-2002 : Changed CategoryDataset to TableDataset (DG);
* 21-Mar-2003 : Incorporated HorizontalCategoryAxis3D, see bug id 685501 (DG);
* 13-May-2003 : Merged some horizontal and vertical methods (DG);
* 24-May-2003 : Added support for timeline in createHighLowChart (BK);
* 07-Jul-2003 : Added createHistogram(...) method contributed by Jelai Wang (DG);
* 27-Jul-2003 : Added createStackedAreaXYChart(...) method (RA);
*
*/
package org.jfree.chart;
import java.awt.Insets;
import java.text.DateFormat;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryAxis3D;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberAxis3D;
import org.jfree.chart.axis.Timeline;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.CategoryItemLabelGenerator;
import org.jfree.chart.labels.HighLowToolTipGenerator;
import org.jfree.chart.labels.IntervalCategoryItemLabelGenerator;
import org.jfree.chart.labels.PieToolTipGenerator;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieToolTipGenerator;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.labels.StandardXYZToolTipGenerator;
import org.jfree.chart.labels.TimeSeriesToolTipGenerator;
import org.jfree.chart.labels.XYToolTipGenerator;
import org.jfree.chart.labels.XYZToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.Pie3DPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.AreaRenderer;
import org.jfree.chart.renderer.AreaXYRenderer;
import org.jfree.chart.renderer.BarRenderer;
import org.jfree.chart.renderer.BarRenderer3D;
import org.jfree.chart.renderer.BoxAndWhiskerRenderer;
import org.jfree.chart.renderer.CandlestickRenderer;
import org.jfree.chart.renderer.CategoryItemRenderer;
import org.jfree.chart.renderer.HighLowRenderer;
import org.jfree.chart.renderer.IntervalBarRenderer;
import org.jfree.chart.renderer.LineAndShapeRenderer;
import org.jfree.chart.renderer.SignalRenderer;
import org.jfree.chart.renderer.StackedAreaRenderer;
import org.jfree.chart.renderer.StackedAreaXYRenderer;
import org.jfree.chart.renderer.StackedBarRenderer;
import org.jfree.chart.renderer.StackedBarRenderer3D;
import org.jfree.chart.renderer.StandardXYItemRenderer;
import org.jfree.chart.renderer.WindItemRenderer;
import org.jfree.chart.renderer.XYBarRenderer;
import org.jfree.chart.renderer.XYBubbleRenderer;
import org.jfree.chart.renderer.XYItemRenderer;
import org.jfree.chart.renderer.XYStepRenderer;
import org.jfree.chart.urls.CategoryURLGenerator;
import org.jfree.chart.urls.PieURLGenerator;
import org.jfree.chart.urls.StandardCategoryURLGenerator;
import org.jfree.chart.urls.StandardPieURLGenerator;
import org.jfree.chart.urls.StandardXYURLGenerator;
import org.jfree.chart.urls.StandardXYZURLGenerator;
import org.jfree.chart.urls.XYURLGenerator;
import org.jfree.chart.urls.XYZURLGenerator;
import org.jfree.data.BoxAndWhiskerDataset;
import org.jfree.data.CategoryDataset;
import org.jfree.data.HighLowDataset;
import org.jfree.data.IntervalCategoryDataset;
import org.jfree.data.IntervalXYDataset;
import org.jfree.data.PieDataset;
import org.jfree.data.SignalsDataset;
import org.jfree.data.TableXYDataset;
import org.jfree.data.WindDataset;
import org.jfree.data.XYDataset;
import org.jfree.data.XYZDataset;
/**
* A collection of utility methods for creating some standard charts with JFreeChart.
*
* @author David Gilbert
*/
public abstract class ChartFactory {
/**
* Creates a pie chart with default settings.
*
* @param title the chart title.
* @param data the dataset for the chart.
* @param legend a flag specifying whether or not a legend is required.
* @param tooltips configure chart to generate tool tips?
* @param urls configure chart to generate URLs?
*
* @return a pie chart.
*/
public static JFreeChart createPieChart(String title,
PieDataset data,
boolean legend,
boolean tooltips,
boolean urls) {
PiePlot plot = new PiePlot(data);
plot.setInsets(new Insets(0, 5, 5, 5));
PieToolTipGenerator tooltipGenerator = null;
if (tooltips) {
tooltipGenerator = new StandardPieToolTipGenerator();
}
PieURLGenerator urlGenerator = null;
if (urls) {
urlGenerator = new StandardPieURLGenerator();
}
plot.setToolTipGenerator(tooltipGenerator);
plot.setURLGenerator(urlGenerator);
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend);
return chart;
}
/**
* Creates a chart containing multiple pie charts, from a TableDataset.
*
* @param title the chart title.
* @param data the dataset for the chart.
* @param extractType <code>PER_ROW</code> or <code>PER_COLUMN</code> (defined in
* {@link PiePlot}).
* @param legend a flag specifying whether or not a legend is required.
* @param tooltips configure chart to generate tool tips?
* @param urls configure chart to generate URLs?
*
* @return a pie chart.
*/
public static JFreeChart createPieChart(String title,
CategoryDataset data,
int extractType,
boolean legend,
boolean tooltips,
boolean urls) {
PiePlot plot = new PiePlot(data, extractType);
plot.setInsets(new Insets(0, 5, 5, 5));
PieToolTipGenerator tooltipGenerator = null;
if (tooltips) {
tooltipGenerator = new StandardPieToolTipGenerator();
}
PieURLGenerator urlGenerator = null;
if (urls) {
urlGenerator = new StandardPieURLGenerator();
}
plot.setToolTipGenerator(tooltipGenerator);
plot.setURLGenerator(urlGenerator);
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend);
return chart;
}
/**
* Creates a pie chart with default settings.
*
* @param title the chart title.
* @param data the dataset for the chart.
* @param legend a flag specifying whether or not a legend is required.
* @param tooltips configure chart to generate tool tips?
* @param urls configure chart to generate URLs?
*
* @return a pie chart.
*/
public static JFreeChart createPie3DChart(String title,
PieDataset data,
boolean legend,
boolean tooltips,
boolean urls) {
Pie3DPlot plot = new Pie3DPlot(data);
plot.setInsets(new Insets(0, 5, 5, 5));
PieToolTipGenerator tooltipGenerator = null;
if (tooltips) {
tooltipGenerator = new StandardPieToolTipGenerator();
}
PieURLGenerator urlGenerator = null;
if (urls) {
urlGenerator = new StandardPieURLGenerator();
}
plot.setToolTipGenerator(tooltipGenerator);
plot.setURLGenerator(urlGenerator);
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend);
return chart;
}
/**
* Creates an area chart with default settings.
*
* @param title the chart title.
* @param categoryAxisLabel the label for the category axis.
* @param valueAxisLabel the label for the value axis.
* @param data the dataset for the chart.
* @param orientation the plot orientation.
* @param legend a flag specifying whether or not a legend is required.
* @param tooltips configure chart to generate tool tips?
* @param urls configure chart to generate URLs?
*
* @return an area chart.
*/
public static JFreeChart createAreaChart(String title,
String categoryAxisLabel,
String valueAxisLabel,
CategoryDataset data,
PlotOrientation orientation,
boolean legend,
boolean tooltips,
boolean urls) {
CategoryAxis categoryAxis = new CategoryAxis(categoryAxisLabel);
categoryAxis.setCategoryMargin(0.0);
ValueAxis valueAxis = new NumberAxis(valueAxisLabel);
AreaRenderer renderer = new AreaRenderer();
if (tooltips) {
renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
}
if (urls) {
renderer.setItemURLGenerator(new StandardCategoryURLGenerator());
}
CategoryPlot plot = new CategoryPlot(data, categoryAxis, valueAxis, renderer);
plot.setOrientation(orientation);
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend);
return chart;
}
/**
* Creates an area chart with default settings.
*
* @param title the chart title.
* @param categoryAxisLabel the label for the category axis.
* @param valueAxisLabel the label for the value axis.
* @param data the dataset for the chart.
* @param orientation the plot orientation (horizontal or vertical).
* @param legend a flag specifying whether or not a legend is required.
* @param tooltips configure chart to generate tool tips?
* @param urls configure chart to generate URLs?
*
* @return an area chart.
*/
public static JFreeChart createStackedAreaChart(String title,
String categoryAxisLabel,
String valueAxisLabel,
CategoryDataset data,
PlotOrientation orientation,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -