折线图(Line Chart)

优质
小牛编辑
130浏览
2023-12-01

折线图或折线图将信息显示为由直线段连接的一系列数据点(标记)。 折线图显示数据在相同时间频率下的变化情况。

本章演示了如何使用JFreeChart从给定的业务数据集创建Line Chart

业务数据

以下示例绘制折线图,​​以显示从1970年开始的不同年份开设的学校数量的趋势。

鉴于数据如下 -

学校数量
197015
198030
199060
2000120
2013240
2014300

基于AWT的应用 (AWT Based Application)

以下是从上面给出的信息创建折线图的代码。 此代码可帮助您在任何基于AWT的应用程序中嵌入折线图。

import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
public class LineChart_AWT extends ApplicationFrame {
   public LineChart_AWT( String applicationTitle , String chartTitle ) {
      super(applicationTitle);
      JFreeChart lineChart = ChartFactory.createLineChart(
         chartTitle,
         "Years","Number of Schools",
         createDataset(),
         PlotOrientation.VERTICAL,
         true,true,false);
      ChartPanel chartPanel = new ChartPanel( lineChart );
      chartPanel.setPreferredSize( new java.awt.Dimension( 560 , 367 ) );
      setContentPane( chartPanel );
   }
   private DefaultCategoryDataset createDataset( ) {
      DefaultCategoryDataset dataset = new DefaultCategoryDataset( );
      dataset.addValue( 15 , "schools" , "1970" );
      dataset.addValue( 30 , "schools" , "1980" );
      dataset.addValue( 60 , "schools" ,  "1990" );
      dataset.addValue( 120 , "schools" , "2000" );
      dataset.addValue( 240 , "schools" , "2010" );
      dataset.addValue( 300 , "schools" , "2014" );
      return dataset;
   }
   public static void main( String[ ] args ) {
      LineChart_AWT chart = new LineChart_AWT(
         "School Vs Years" ,
         "Numer of Schools vs years");
      chart.pack( );
      RefineryUtilities.centerFrameOnScreen( chart );
      chart.setVisible( true );
   }
}

让我们将上述Java代码保存在LineChart_AWT.java文件中,然后从命令提示编译并运行它 -

$javac LineChart_AWT.java  
$java LineChart_AWT 

如果一切正常,它将编译并运行以生成以下线图 -

JFreeChart折线图

JPEG图像创建 (JPEG Image Creation)

让我们重新编写上面的例子,从命令行生成JPEG图像。

import java.io.*;
import org.jfree.chart.JFreeChart; 
import org.jfree.chart.ChartFactory; 
import org.jfree.chart.ChartUtilities; 
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
public class LineChart {
   public static void main( String[ ] args ) throws Exception {
      DefaultCategoryDataset line_chart_dataset = new DefaultCategoryDataset();
      line_chart_dataset.addValue( 15 , "schools" , "1970" );
      line_chart_dataset.addValue( 30 , "schools" , "1980" );
      line_chart_dataset.addValue( 60 , "schools" , "1990" );
      line_chart_dataset.addValue( 120 , "schools" , "2000" );
      line_chart_dataset.addValue( 240 , "schools" , "2010" ); 
      line_chart_dataset.addValue( 300 , "schools" , "2014" );
      JFreeChart lineChartObject = ChartFactory.createLineChart(
         "Schools Vs Years","Year",
         "Schools Count",
         line_chart_dataset,PlotOrientation.VERTICAL,
         true,true,false);
      int width = 640;    /* Width of the image */
      int height = 480;   /* Height of the image */ 
      File lineChart = new File( "LineChart.jpeg" ); 
      ChartUtilities.saveChartAsJPEG(lineChart ,lineChartObject, width ,height);
   }
}

让我们将上述Java代码保存在LineChart.java文件中,然后从命令提示编译并运行它 -

$javac LineChart.java  
$java LineChart

如果一切正常,它将编译并执行以在当前目录中创建名为LineChart.jpeg的JPEG图像文件。