📅  最后修改于: 2020-11-30 05:09:28             🧑  作者: Mango
折线图或折线图将信息显示为由直线段连接的一系列数据点(标记)。折线图显示了数据如何以相同的时间频率变化。
本章演示了如何使用JFreeChart从给定的一组业务数据中创建折线图。
以下示例绘制了折线图,以显示从1970年开始在不同年份开设的学校数量的趋势。
给定数据如下-
Year | Number OF Schools |
---|---|
1970 | 15 |
1980 | 30 |
1990 | 60 |
2000 | 120 |
2013 | 240 |
2014 | 300 |
以下是根据上述信息创建折线图的代码。此代码可帮助您将折线图嵌入任何基于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
如果一切都很好,它将编译并运行以生成以下线图-
让我们重写上面的示例,以从命令行生成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图像文件。