Chinaunix首页 | 论坛 | 博客
  • 博客访问: 208048
  • 博文数量: 73
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 750
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-13 18:32
文章分类

全部博文(73)

文章存档

2009年(1)

2008年(72)

我的朋友

分类: 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}.
   *


   * IMPORTANT NOTE: THIS DEMO IS
   * DOCUMENTED IN THE JFREECHART     DEVELOPER GUIDE.
   * DO NOT MAKE CHANGES WITHOUT UPDATING THE GUIDE ALSO!!
   */
  public class LineChartDemo2 extends ApplicationFrame {
   /**
   * Creates a new demo.
   *
   * @param title the frame title.
   */
    public LineChartDemo2(String title) {
      super(title);
      XYDataset dataset = createDataset();
      JFreeChart chart = createChart(dataset);
      ChartPanel chartPanel = new ChartPanel(chart);
      chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
      setContentPane(chartPanel);
   }
   /**
    * Creates a sample dataset.
    *
    * @return a sample dataset.
    */
      private static XYDataset createDataset() {
      #使用XYSeriesCollection表示数据,它可以包含多个XYSeries.
      XYSeries series1 = new XYSeries("First");
      series1.add(1.0, 1.0);
      series1.add(2.0, 4.0);
      series1.add(3.0, 3.0);
      series1.add(4.0, 5.0);
      series1.add(5.0, 5.0);
      series1.add(6.0, 7.0);
      series1.add(7.0, 7.0);
      series1.add(8.0, 8.0);
      XYSeries series2 = new XYSeries("Second");
      series2.add(1.0, 5.0);
      series2.add(2.0, 7.0);
      series2.add(3.0, 6.0);
      series2.add(4.0, 8.0);
      series2.add(5.0, 4.0);
      series2.add(6.0, 4.0);
      series2.add(7.0, 2.0);
      series2.add(8.0, 1.0);
      XYSeries series3 = new XYSeries("Third");
      series3.add(3.0, 4.0);
      series3.add(4.0, 3.0);
      series3.add(5.0, 2.0);
      series3.add(6.0, 3.0);
      series3.add(7.0, 6.0);
      series3.add(8.0, 3.0);
      series3.add(9.0, 4.0);
      series3.add(10.0, 3.0);
      XYSeriesCollection dataset = new XYSeriesCollection();
      dataset.addSeries(series1);
      dataset.addSeries(series2);
      dataset.addSeries(series3);
      return dataset;
   }
    /**
     * Creates a chart.
     *
     * @param dataset the data for the chart.
     *
     * @return a chart.
     */
      private static JFreeChart createChart(XYDataset dataset) {
     // create the chart...
        JFreeChart chart = ChartFactory.createXYLineChart(
          "Line Chart Demo 2", // chart title
          "X", // x axis label
          "Y", // y axis label
          dataset, // data
          PlotOrientation.VERTICAL,
          true, // include legend
          true, // tooltips
          false // urls
       );
     #customize 线性chart
     // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
     chart.setBackgroundPaint(Color.white);
     // get a reference to the plot for further customisation...
     XYPlot plot = (XYPlot) chart.getPlot();
     plot.setBackgroundPaint(Color.lightGray);
     #设置横纵坐标与axises之间的间距
     plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
     #分别设置横向Gridline和纵向Gridline的颜色
     plot.setDomainGridlinePaint(Color.white);

     plot.setRangeGridlinePaint(Color.white);
     XYLineAndShapeRenderer renderer
       = (XYLineAndShapeRenderer) plot.getRenderer();
     renderer.setShapesVisible(true);
     renderer.setShapesFilled(true);
     //只使用整数的scale
     // change the auto tick unit selection to integer units only...
     NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
     rangeAxis.setStandardTickUnits(
        NumberAxis.createIntegerTickUnits());
     // OPTIONAL CUSTOMISATION COMPLETED.
     return chart;
  }
     /**
      * Creates a panel for the demo (used by SuperDemo.java).
      *
      * @return A panel.
      */
     #例行公事
     public static JPanel createDemoPanel() {
       JFreeChart chart = createChart(createDataset());
       return new ChartPanel(chart);
     }
     /**
      * Starting point for the demonstration application.
      *
      * @param args ignored.
      */
      public static void main(String[] args) {
        LineChartDemo2 demo = new LineChartDemo2(
          "Line Chart Demo  2");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
      }
  }
  TIME SERIES CHARTS类似于基于XYDataset的Line Chart.Refer to TimeSeriesDemo1.java.












阅读(2322) | 评论(0) | 转发(0) |
0

上一篇:对象的集合 二

下一篇:对象的集合 二(续)

给主人留下些什么吧!~~