JFreeChart时序图

来源:互联网 发布:iphone抹除数据 编辑:程序博客网 时间:2024/06/05 18:18

使用的jfreecart版本为jfreechart-1.0.14.jar,如果要使用jfreechart,还必须要导入其依赖包jcommon,使用的版本为jcommon-1.0.17.jar

下图展示了所使用的jfreechart基本类之间的关系:

public class charttest {public static void main(String[] args) {JFreeChart chart = ChartFactory.createTimeSeriesChart("示例", "x轴名称", "y轴名称", getDataSet(), true, false, false);chart.getTitle().setFont(new Font("宋体", Font.PLAIN, 12));//标题chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));//图例XYPlot plot = chart.getXYPlot();//图表DateAxis x = (DateAxis)plot.getDomainAxis();//x轴x.setLabelFont(new Font("宋体", Font.PLAIN, 12));//x轴名称字体x.setTickLabelFont(new Font("宋体", Font.PLAIN, 12));//x轴节点字体x.setDateFormatOverride(new SimpleDateFormat("yyyy-MM-dd HH:mm"));//x轴节点格式NumberAxis y = (NumberAxis)plot.getRangeAxis();//y轴y.setLabelFont(new Font("宋体", Font.PLAIN, 12));//y轴名称字体y.setTickLabelFont(new Font("宋体", Font.PLAIN, 12));//y轴节点字体NumberFormat nf = NumberFormat.getInstance();nf.setMaximumFractionDigits(1);y.setNumberFormatOverride(nf);//y轴节点格式XYLineAndShapeRenderer render = (XYLineAndShapeRenderer) plot.getRenderer();//图表式样plot.setBackgroundPaint(Color.LIGHT_GRAY);//背景颜色    plot.setDomainGridlinePaint(Color.green);//网格竖线颜色     plot.setRangeGridlinePaint(Color.green);//网格横线颜色render.setBaseShapesVisible(true);//显示节点FileOutputStream output;try {output = new FileOutputStream("D:\\test.jpg");ChartUtilities.writeChartAsPNG(output, chart, 450, 400);} catch (Exception e) {e.printStackTrace();}}private static XYDataset getDataSet(){TimeSeriesCollection tsSet = new TimeSeriesCollection();TimeSeries ts = new TimeSeries("test");ts.add(new Day(1, 1, 2013), 11.1);ts.add(new Day(2, 1, 2013), 12.2);//ts.add(new Month(2, 2013), 133);//ts.add(new Month(3, 2013), 111);tsSet.addSeries(ts);return tsSet;}}

显示的结果为:


 

 

 

原创粉丝点击