本文概述
- 时间序列图示例
- TimeSeriesChartExample.java
下图显示了JFreeChart库中合并的时间序列图的一些演示版本:
【JFreeChart-时间序列图】时间序列图演示1:
文章图片
时间序列图演示2:
文章图片
时间序列图演示3:
文章图片
时间序列图演示4:
文章图片
时间序列图演示5:
文章图片
时间序列图示例 让我们考虑一下时间序列图的以下示例数据。
日期 | 系列一 | 系列二 |
---|---|---|
2017-01-01 | 50 | 40 |
2017-01-02 | 40 | 35 |
2017-01-03 | 45 | 26 |
2017-01-04 | 30 | 45 |
2017-01-05 | 50 | 40 |
2017-01-06 | 45 | 35 |
2017-01-07 | 60 | 45 |
2017-01-08 | 45 | 48 |
2017-01-09 | 55 | 31 |
2017-01-10 | 48 | 32 |
2017-01-11 | 60 | 21 |
2017-01-12 | 45 | 35 |
2017-01-13 | 65 | 10 |
2017-01-14 | 45 | 25 |
2017-01-15 | 55 | 15 |
TimeSeriesChartExample.java
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
public class TimeSeriesChartExample extends JFrame {private static final long serialVersionUID = 1L;
public TimeSeriesChartExample(String title) {super(title);
// Create datasetXYDataset dataset = createDataset();
// Create chartJFreeChart chart = ChartFactory.createTimeSeriesChart("Time Series Chart Example", // Chart"Date", // X-Axis Label"Number", // Y-Axis Labeldataset);
//Changes background colorXYPlot plot = (XYPlot)chart.getPlot();
plot.setBackgroundPaint(new Color(255, 228, 196));
ChartPanel panel = new ChartPanel(chart);
setContentPane(panel);
}private XYDataset createDataset() {TimeSeriesCollection dataset = new TimeSeriesCollection();
TimeSeries series1 = new TimeSeries("Series1");
series1.add(new Day(1, 1, 2017), 50);
series1.add(new Day(2, 1, 2017), 40);
series1.add(new Day(3, 1, 2017), 45);
series1.add(new Day(4, 1, 2017), 30);
series1.add(new Day(5, 1, 2017), 50);
series1.add(new Day(6, 1, 2017), 45);
series1.add(new Day(7, 1, 2017), 60);
series1.add(new Day(8, 1, 2017), 45);
series1.add(new Day(9, 1, 2017), 55);
series1.add(new Day(10, 1, 2017), 48);
series1.add(new Day(11, 1, 2017), 60);
series1.add(new Day(12, 1, 2017), 45);
series1.add(new Day(13, 1, 2017), 65);
series1.add(new Day(14, 1, 2017), 45);
series1.add(new Day(15, 1, 2017), 55);
dataset.addSeries(series1);
TimeSeries series2 = new TimeSeries("Series2");
series2.add(new Day(1, 1, 2017), 40);
series2.add(new Day(2, 1, 2017), 35);
series2.add(new Day(3, 1, 2017), 26);
series2.add(new Day(4, 1, 2017), 45);
series2.add(new Day(5, 1, 2017), 40);
series2.add(new Day(6, 1, 2017), 35);
series2.add(new Day(7, 1, 2017), 45);
series2.add(new Day(8, 1, 2017), 48);
series2.add(new Day(9, 1, 2017), 31);
series2.add(new Day(10, 1, 2017), 32);
series2.add(new Day(11, 1, 2017), 21);
series2.add(new Day(12, 1, 2017), 35);
series2.add(new Day(13, 1, 2017), 10);
series2.add(new Day(14, 1, 2017), 25);
series2.add(new Day(15, 1, 2017), 15);
dataset.addSeries(series2);
return dataset;
}public static void main(String[] args) {SwingUtilities.invokeLater(() ->
{TimeSeriesChartExample example = new TimeSeriesChartExample("Time Series Chart");
example.setSize(800, 400);
example.setLocationRelativeTo(null);
example.setVisible(true);
example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
});
}}
输出:
文章图片
推荐阅读
- TimeSeries类
- JFreeChart-散点图
- JFreeChart PlotOrientation绘图方向类
- PiePlot 3D类
- JFreeChart-饼图
- NumberAxis类
- JFreeChart折线图
- 接口PieDataset
- JFreeChart-Gantt图表