JFreechart画时序图

JFreechart是java绘图API,可以绘制折线图,柱形图,饼图,时序图。能够在swing和web中制作自定义的图表或报表。

1.下载JFreechart,我的在D盘,demo.jar是例子,

2.绘制折线图:

TimeSeries,曲线上所有数据,要添加到TimeSeriesCollection的对象里

TimeSeriesCollection,表中所有曲线的集合,生成JFreeChart时要用到。

ChartFactory,利用该类的createTimeSeriesChart方法创建绘制图的ChartFactory对象,

JFreeChart,绘制图表

//访问量统计时间线 TimeSeries timeSeries = new TimeSeries("阿蜜果blog访问量统计", Month.class);//时间曲线数据集合 TimeSeriesCollection lineDataset = new TimeSeriesCollection(); //构造数据集合 timeSeries.add(new Month(1, 2007), 11200); timeSeries.add(new Month(2, 2007), 9000); timeSeries.add(new Month(3, 2007), 6200); timeSeries.add(new Month(4, 2007), 8200); timeSeries.add(new Month(5, 2007), 8200); timeSeries.add(new Month(6, 2007), 12200); timeSeries.add(new Month(7, 2007), 13200); timeSeries.add(new Month(8, 2007), 8300); timeSeries.add(new Month(9, 2007), 12400); timeSeries.add(new Month(10, 2007), 12500); timeSeries.add(new Month(11, 2007), 13600); timeSeries.add(new Month(12, 2007), 12500); lineDataset.addSeries(timeSeries); JFreeChart chart = ChartFactory.createTimeSeriesChart("访问量统计时间线", "month", "visit amount", lineDataset, true, true, true); //设置子标题 TextTitle subtitle = new TextTitle("2007年度", new Font("黑体", Font.BOLD, 12)); chart.addSubtitle(subtitle); //设置主标题 chart.setTitle(new TextTitle("阿蜜果blog访问量统计", new Font("隶书", Font.ITALIC, 15))); chart.setAntiAlias(true); String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session); String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;

ChartFrame,将JFreechart对象放在java容器组件里,ChartFrame继承自JFrame类,

	ChartFrame chartFrame=new ChartFrame("标题",JFreeChart对象); 
chartFrame.pack(); //以合适的大小展现图形
chartFrame.setVisible(true);//图形是否可见
XYPlot由JFreeChart对象的getXYPlot方法获得

<span style="white-space:pre">	</span>   XYPlot plot = timeSeriesChart.getXYPlot(); 
<span style="white-space:pre">	</span>   plot.setDomainGridlinePaint(Color.LIGHT_GRAY);  //数值的
           plot.setRangeGridlinePaint(Color.black);  //水平的
           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);    //每个节点处被填充
           } 
    DateAxis 横轴(日期类型)
<span style="white-space:pre">	</span>DateAxis axis=(DateAxis)plot.getDomainAxis(); //获取x轴对象
<span style="white-space:pre">	</span>axis.setDateFormatOverride(new SimpleDateFormat("dd:MM:yyyy HH:mm"));






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值