JFreeChart 时间序列图
精华
小牛编辑
163浏览
2023-03-14
什么是JFreeChart 时间序列图
时间序列图表表示以相等的时间间隔变化的数字数据序列。
下图显示了 JFreeChart 库中包含的时间序列图表的一些演示版本:
JFreeChart 时间序列图的示例
让我们考虑以下时间序列图表的示例数据。
日期 | 列1 | 列2 |
---|---|---|
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 |
以下代码根据上述示例数据创建气泡图:
package cn.xnip;
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;
import javax.swing.*;
public class TimeSeriesChartExample extends JFrame {
private static final long serialVersionUID = 1L;
public TimeSeriesChartExample(String title) {
super(title);
// Create dataset
XYDataset dataset = createDataset();
// Create chart
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Time Series Chart Example", // Chart
"Date", // X-Axis Label
"Number", // Y-Axis Label
dataset);
//Changes background color
XYPlot 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);
});
}
}
输出结果为: