本文概述
- 折线图示例
- LineChartExample.java
下图显示了JFreeChart库中包含的折线图的一些演示版本:
折线图演示1:
文章图片
折线图演示2:
文章图片
【JFreeChart折线图】折线图演示3:
文章图片
折线图演示4:
文章图片
折线图示例 让我们考虑一个示例数据, 该数据显示了我们网站www.srcmini.com上的频繁访问。
日期 | 每日访客总数 |
---|---|
2016-12-19 | 200 |
2016-12-20 | 150 |
2016-12-21 | 100 |
2016-12-22 | 210 |
2016-12-23 | 240 |
2016-12-24 | 195 |
2016-12-25 | 245 |
日期 | 唯一身份访问者总数 |
---|---|
2016-12-19 | 150 |
2016-12-20 | 130 |
2016-12-21 | 95 |
2016-12-22 | 195 |
2016-12-23 | 200 |
2016-12-24 | 180 |
2016-12-25 | 230 |
LineChartExample.java
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.category.DefaultCategoryDataset;
public class LineChartExample extends JFrame {private static final long serialVersionUID = 1L;
public LineChartExample(String title) {super(title);
// Create datasetDefaultCategoryDataset dataset = createDataset();
// Create chartJFreeChart chart = ChartFactory.createLineChart("Site Traffic", // Chart title"Date", // X-Axis Label"Number of Visitor", // Y-Axis Labeldataset);
ChartPanel panel = new ChartPanel(chart);
setContentPane(panel);
}private DefaultCategoryDataset createDataset() {String series1 = "Visitor";
String series2 = "Unique Visitor";
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(200, series1, "2016-12-19");
dataset.addValue(150, series1, "2016-12-20");
dataset.addValue(100, series1, "2016-12-21");
dataset.addValue(210, series1, "2016-12-22");
dataset.addValue(240, series1, "2016-12-23");
dataset.addValue(195, series1, "2016-12-24");
dataset.addValue(245, series1, "2016-12-25");
dataset.addValue(150, series2, "2016-12-19");
dataset.addValue(130, series2, "2016-12-20");
dataset.addValue(95, series2, "2016-12-21");
dataset.addValue(195, series2, "2016-12-22");
dataset.addValue(200, series2, "2016-12-23");
dataset.addValue(180, series2, "2016-12-24");
dataset.addValue(230, series2, "2016-12-25");
return dataset;
}public static void main(String[] args) {SwingUtilities.invokeLater(() ->
{LineChartExample example = new LineChartExample("Line Chart Example");
example.setAlwaysOnTop(true);
example.pack();
example.setSize(600, 400);
example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
example.setVisible(true);
});
}}
输出:
文章图片
推荐阅读
- NumberAxis类
- 接口PieDataset
- JFreeChart-Gantt图表
- DefaultPieDataset类
- DefaultCategoryDataset类
- JFreeChart-气泡图
- JFreeChart条形图
- JFreeChart架构
- 什么是JFreeChart()