JfreeChart中的timeSeries(时序图)使用示例
生成时序图:
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 对象。
//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 的使用方式。
//实例化: TimeSeriesCollection dataset=new TimeSeriesCollection(); //添加数据操作: dataset. addSeries(TimeSeries); // 后面讲解 TimeSeries 对象 //删除数据操作: dataset. removeSeries(int index); //查询数据操作 : dataset. getSeries(int series);
TimeSeries 对象操作
//实例化: 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…… 等,不再详述
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(); } }
效果图
本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!