生成时序图:
Java代码 收藏代码
1. JFreeChart chart = ChartFactory.createTimeSeriesChart(
2. String title, // 图标题
3. String timeAxisLabel, //x 轴标题
4. String valueAxisLabel, //y 轴标题
5. XYDataset dataset, // 数据源
6. boolean legend, // 是否显示图例
7. boolean tooltips, // 是否显示 tooltip
8. boolean urls); // 是否指定 url
JFreeChart chart = ChartFactory.createTimeSeriesChart(
String title, // 图标题
String timeAxisLabel, //x 轴标题
String valueAxisLabel, //y 轴标题
XYDataset dataset, // 数据源
boolean legend, // 是否显示图例
boolean tooltips, // 是否显示 tooltip
boolean urls); // 是否指定 url
时序图的 Plot 对象是 XYPlot 类型。 XYPlot 对象的 x 轴是 DateAxis 对象, y 轴是 NumberAxis 对象,绘制单元是 XYLineAndShapeRenderer 对象,数据源是 XYDataset 对象。
Java代码 收藏代码
1. //Plot 对象的获取操作:
2. XYPlot plot = (XYPlot) chart.getPlot();
3. //X 轴对象的获取操作:
4. DateAxis xAxis = (DateAxis) plot.getDomainAxis();
5. //Y 轴对象的获取操作:
6. NumberAxis yAxis =(NumberAxis) plot.getRangeAxis();
7. //Renderer 对象的获取操作:
8. XYItemRenderer renderer= plot.getRenderer();
//Plot 对象的获取操作:
XYPlot plot = (XYPlot) chart.getPlot();
//X 轴对象的获取操作:
DateAxis xAxis = (DateAxis) plot.getDomainAxis();
//Y 轴对象的获取操作:
NumberAxis yAxis =(NumberAxis) plot.getRangeAxis();
//Renderer 对象的获取操作:
XYItemRenderer renderer= plot.getRenderer();
时序图可以接受一切 XYDataset 类型的数据源,下面讲解一下常用的 XYDataset 类型 TimeSeriesCollection 的使用方式。
Java代码 收藏代码
1. //实例化:
2. TimeSeriesCollection dataset=new TimeSeriesCollection();
3. //添加数据操作:
4. dataset. addSeries(TimeSeries); // 后面讲解 TimeSeries 对象
5. //删除数据操作:
6. dataset. removeSeries(int index);
7. //查询数据操作 :
8. dataset. getSeries(int series);
//实例化:
TimeSeriesCollection dataset=new TimeSeriesCollection();
//添加数据操作:
dataset. addSeries(TimeSeries); // 后面讲解 TimeSeries 对象
//删除数据操作:
dataset. removeSeries(int index);
//查询数据操作 :
dataset. getSeries(int series);
TimeSeries 对象操作
Java代码 收藏代码
1. //实例化:
2. TimeSeries ts=TimeSeries(String name, Class timePeriodClass);
3. //增加数据操作:
4. ts. add(RegularTimePeriod period, double value);
5. //删除数据操作:
6. ts. delete(RegularTimePeriod period);
7. //修改数据操作:
8. ts. update(RegularTimePeriod period, Number value);
9. //查询数据操作:
10. ts. getValue(RegularTimePeriod period);
//实例化:
TimeSeries ts=TimeSeries(String name, Class timePeriodClass);
//增加数据操作:
ts. add(RegularTimePeriod period, double value);
//删除数据操作:
ts. delete(RegularTimePeriod period);
//修改数据操作:
ts. update(RegularTimePeriod period, Number value);
//查询数据操作:
ts. getValue(RegularTimePeriod period);
类 RegularTimePeriod 是 JFreeChart 提供的时间模板类,它有很多具体的时间类,比如: Minute 、 Second 、 Hour 、 Day…… 等,不再详述
Java代码 收藏代码
1. package com.px1987.jfreechart;
2.
3. import java.awt.Color;
4. import org.jfree.chart.ChartFactory;
5. import org.jfree.chart.ChartFrame;
6. import org.jfree.chart.JFreeChart;
7. import org.jfree.chart.plot.XYPlot;
8. import org.jfree.chart.renderer.xy.XYItemRenderer;
9. import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
10. import org.jfree.data.time.Day;
11. import org.jfree.data.time.Hour;
12. import org.jfree.data.time.Minute;
13. import org.jfree.data.time.TimeSeries;
14. import org.jfree.data.time.TimeSeriesCollection;
15. import org.jfree.data.xy.XYDataset;
16. import org.jfree.ui.RectangleInsets;
17.
18. public class TestTimeSeriesChart {
19.
20. private static XYDataset createDataset() {
21. TimeSeriesCollection dataset = new TimeSeriesCollection();
22. Day day = new Day(21, 9, 2008);
23. Hour hour22 = new Hour(22, day);
24. Hour hour23 = new Hour(23, day);
25.
26. TimeSeries timeSeries1 = new TimeSeries("篮球火", Minute.class);
27. timeSeries1.add(new Minute(25, hour23), 2.80);
28. timeSeries1.add(new Minute(22, hour23), 2.59);
29. timeSeries1.add(new Minute(32, hour22), 2.38);
30. timeSeries1.add(new Minute(14, hour22), 2.35);
31. timeSeries1.add(new Minute(18, hour23), 2.34);
32. timeSeries1.add(new Minute(57, hour23), 2.31);
33. timeSeries1.add(new Minute(14, hour23), 2.28);
34. timeSeries1.add(new Minute(23, hour22), 2.25);
35. timeSeries1.add(new Minute(46, hour22), 2.16);
36. timeSeries1.add(new Minute(40, hour22), 2.16);
37. timeSeries1.add(new Minute(6, hour22), 1.95);
38. timeSeries1.add(new Minute(51, hour22), 1.93);
39. timeSeries1.add(new Minute(2, hour23), 1.86);
40. timeSeries1.add(new Minute(6, hour23), 1.84);
41. timeSeries1.add(new Minute(9, hour23), 1.79);
42. timeSeries1.add(new Minute(29, hour23), 1.72);
43.
44. TimeSeries timeSeries2 = new TimeSeries("无敌珊宝妹", Minute.class);
45. timeSeries2.add(new Minute(36, hour22), 3.63);
46. timeSeries2.add(new Minute(55, hour22), 3.63);
47. timeSeries2.add(new Minute(21, hour23), 3.36);
48. timeSeries2.add(new Minute(30, hour22), 3.28);
49. timeSeries2.add(new Minute(33, hour23), 3.18);
50. timeSeries2.add(new Minute(6, hour23), 3.13);
51. timeSeries2.add(new Minute(13, hour22), 3.05);
52. timeSeries2.add(new Minute(0, hour23), 2.98);
53. timeSeries2.add(new Minute(40, hour22), 2.98);
54. timeSeries2.add(new Minute(48, hour22), 2.91);
55. timeSeries2.add(new Minute(14, hour23), 2.73);
56. timeSeries2.add(new Minute(27, hour23), 2.59);
57. timeSeries2.add(new Minute(38, hour23), 2.37);
58.
59. TimeSeries timeSeries3 = new TimeSeries("不良笑花", Minute.class);
60. timeSeries3.add(new Minute(8, hour23), 2.84);
61. timeSeries3.add(new Minute(29, hour22), 2.51);
62. timeSeries3.add(new Minute(56, hour22), 2.23);
63. timeSeries3.add(new Minute(5, hour23), 2.17);
64. timeSeries3.add(new Minute(47, hour22), 2.10);
65. timeSeries3.add(new Minute(17, hour22), 1.86);
66. timeSeries3.add(new Minute(24, hour23), 1.84);
67. timeSeries3.add(new Minute(5, hour22), 1.84);
68. timeSeries3.add(new Minute(15, hour23), 1.73);
69. timeSeries3.add(new Minute(36, hour22), 1.46);
70.
71. dataset.addSeries(timeSeries1);
72. dataset.addSeries(timeSeries2);
73. dataset.addSeries(timeSeries3);
74. return dataset;
75. }
76.
77. public static void createTimeSeriesChart() {
78. JFreeChart timeSeriesChart = ChartFactory.createTimeSeriesChart(
79. "台湾偶像剧收视率(2008-9-21)", "播放时间", "收视率百分点", createDataset(), true,
80. true, false);
81. timeSeriesChart.setBackgroundPaint(Color.YELLOW);
82. XYPlot plot = timeSeriesChart.getXYPlot();
83. setXYPolt(plot);
84.
85. ChartFrame frame = new ChartFrame("TestPieChart", timeSeriesChart);
86. frame.pack();
87. frame.setVisible(true);
88. }
89.
90. public static void setXYPolt(XYPlot plot) {
91. plot.setDomainGridlinePaint(Color.LIGHT_GRAY);
92. plot.setRangeGridlinePaint(Color.LIGHT_GRAY);
93. // plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
94. XYItemRenderer r = plot.getRenderer();
95. if (r instanceof XYLineAndShapeRenderer) {
96. XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
97. renderer.setBaseShapesVisible(true);
98. renderer.setBaseShapesFilled(false);
99. }
100. }
101.
102. public static void main(String[] args) {
103. createTimeSeriesChart();
104. }
105.
106. }
package com.px1987.jfreechart;
import java.awt.Color;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.Hour;
import org.jfree.data.time.Minute;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.RectangleInsets;
public class TestTimeSeriesChart {
private static XYDataset createDataset() {
TimeSeriesCollection dataset = new TimeSeriesCollection();
Day day = new Day(21, 9, 2008);
Hour hour22 = new Hour(22, day);
Hour hour23 = new Hour(23, day);
TimeSeries timeSeries1 = new TimeSeries("篮球火", Minute.class);
timeSeries1.add(new Minute(25, hour23), 2.80);
timeSeries1.add(new Minute(22, hour23), 2.59);
timeSeries1.add(new Minute(32, hour22), 2.38);
timeSeries1.add(new Minute(14, hour22), 2.35);
timeSeries1.add(new Minute(18, hour23), 2.34);
timeSeries1.add(new Minute(57, hour23), 2.31);
timeSeries1.add(new Minute(14, hour23), 2.28);
timeSeries1.add(new Minute(23, hour22), 2.25);
timeSeries1.add(new Minute(46, hour22), 2.16);
timeSeries1.add(new Minute(40, hour22), 2.16);
timeSeries1.add(new Minute(6, hour22), 1.95);
timeSeries1.add(new Minute(51, hour22), 1.93);
timeSeries1.add(new Minute(2, hour23), 1.86);
timeSeries1.add(new Minute(6, hour23), 1.84);
timeSeries1.add(new Minute(9, hour23), 1.79);
timeSeries1.add(new Minute(29, hour23), 1.72);
TimeSeries timeSeries2 = new TimeSeries("无敌珊宝妹", Minute.class);
timeSeries2.add(new Minute(36, hour22), 3.63);
timeSeries2.add(new Minute(55, hour22), 3.63);
timeSeries2.add(new Minute(21, hour23), 3.36);
timeSeries2.add(new Minute(30, hour22), 3.28);
timeSeries2.add(new Minute(33, hour23), 3.18);
timeSeries2.add(new Minute(6, hour23), 3.13);
timeSeries2.add(new Minute(13, hour22), 3.05);
timeSeries2.add(new Minute(0, hour23), 2.98);
timeSeries2.add(new Minute(40, hour22), 2.98);
timeSeries2.add(new Minute(48, hour22), 2.91);
timeSeries2.add(new Minute(14, hour23), 2.73);
timeSeries2.add(new Minute(27, hour23), 2.59);
timeSeries2.add(new Minute(38, hour23), 2.37);
TimeSeries timeSeries3 = new TimeSeries("不良笑花", Minute.class);
timeSeries3.add(new Minute(8, hour23), 2.84);
timeSeries3.add(new Minute(29, hour22), 2.51);
timeSeries3.add(new Minute(56, hour22), 2.23);
timeSeries3.add(new Minute(5, hour23), 2.17);
timeSeries3.add(new Minute(47, hour22), 2.10);
timeSeries3.add(new Minute(17, hour22), 1.86);
timeSeries3.add(new Minute(24, hour23), 1.84);
timeSeries3.add(new Minute(5, hour22), 1.84);
timeSeries3.add(new Minute(15, hour23), 1.73);
timeSeries3.add(new Minute(36, hour22), 1.46);
dataset.addSeries(timeSeries1);
dataset.addSeries(timeSeries2);
dataset.addSeries(timeSeries3);
return dataset;
}
public static void createTimeSeriesChart() {
JFreeChart timeSeriesChart = ChartFactory.createTimeSeriesChart(
"台湾偶像剧收视率(2008-9-21)", "播放时间", "收视率百分点", createDataset(), true,
true, false);
timeSeriesChart.setBackgroundPaint(Color.YELLOW);
XYPlot plot = timeSeriesChart.getXYPlot();
setXYPolt(plot);
ChartFrame frame = new ChartFrame("TestPieChart", timeSeriesChart);
frame.pack();
frame.setVisible(true);
}
public static void setXYPolt(XYPlot plot) {
plot.setDomainGridlinePaint(Color.LIGHT_GRAY);
plot.setRangeGridlinePaint(Color.LIGHT_GRAY);
// plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
XYItemRenderer r = plot.getRenderer();
if (r instanceof XYLineAndShapeRenderer) {
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
renderer.setBaseShapesVisible(true);
renderer.setBaseShapesFilled(false);
}
}
public static void main(String[] args) {
createTimeSeriesChart();
}
}
效果图
文章来源:
http://sktoo.iteye.com/blog/249123