分类: Java
2008-03-29 13:45:37
概要 This section describes the line charts that can be created with JFreeChart. It is possible to create line charts using data from either the CategoryDataset interface or the XYDataset interface. |
1,基于Category Dataset的Line Chart #工作原理 A line chart based on a CategoryDataset simply connects each (category, value) data item using straight lines. #The complete CODE package com.biaoflying; //package demo; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import javax.swing.JPanel; 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.category.LineAndShapeRenderer; import org.jfree.chart.title.TextTitle; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.ui.ApplicationFrame; import org.jfree.ui.HorizontalAlignment; import org.jfree.ui.RectangleEdge; import org.jfree.ui.RefineryUtilities; public class LineChartDemo1 extends ApplicationFrame { public LineChartDemo1(String title) { super(title); CategoryDataset dataset = createDataset(); JFreeChart chart = createChart(dataset); ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new Dimension(500, 270)); setContentPane(chartPanel); } private static CategoryDataset createDataset() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); #可以设想为一个Category只有一个series,故都为“Classes” dataset.addValue(212, "Classes", "JDK 1.0"); dataset.addValue(504, "Classes", "JDK 1.1"); dataset.addValue(1520, "Classes", "SDK 1.2"); dataset.addValue(1842, "Classes", "SDK 1.3"); dataset.addValue(2991, "Classes", "SDK 1.4"); return dataset; } private static JFreeChart createChart(CategoryDataset dataset) { // create the chart... JFreeChart chart = ChartFactory.createLineChart( "Java Standard Class Library", // chart title "Release", // domain axis label "Class Count", // range axis label dataset, // data PlotOrientation.VERTICAL, // orientation false, // include legend true, // tooltips false // urls ); #添加子标题 chart.addSubtitle(new TextTitle( "Number of Classes By Release")); #定制添加文字,如设置文字的颜色,字体,位置等。 TextTitle source = new TextTitle( "Source: Java In A Nutshell (4th Edition) " + "by David Flanagan (O’Reilly)" ); source.setFont(new Font( "SansSerif", Font.PLAIN, 10)); source.setPosition(RectangleEdge.BOTTOM); source.setHorizontalAlignment( HorizontalAlignment.RIGHT); chart.addSubtitle(source); chart.setBackgroundPaint(Color.white); CategoryPlot plot = (CategoryPlot) chart.getPlot(); plot.setBackgroundPaint(Color.lightGray); plot.setRangeGridlinePaint(Color.white); // customise the range axis... NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits( NumberAxis.createIntegerTickUnits()); // customise the renderer...注意Line Chart //所使用的 Renderer LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer(); renderer.setShapesVisible(true); renderer.setDrawOutlines(true); renderer.setUseFillPaint(true); renderer.setFillPaint(Color.white); return chart; } #例行公事 public static JPanel createDemoPanel() { JFreeChart chart = createChart(createDataset()); return new ChartPanel(chart); } public static void main(String[] args) { LineChartDemo1 demo = new LineChartDemo1( "Line Chart Demo"); demo.pack(); RefineryUtilities.centerFrameOnScreen(demo); demo.setVisible(true); } } 2,基于XYDataset的Line Chart package com.biaoflying; import java.awt.Color; import javax.swing.JPanel; 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.PlotOrientation; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; import org.jfree.data.xy.XYDataset; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; import org.jfree.ui.ApplicationFrame; import org.jfree.ui.RectangleInsets; import org.jfree.ui.RefineryUtilities; /** * A simple demonstration application showing * how to create a line chart using * data from an {@link XYDataset}. *
|