码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA Swing使用JFreeChart实现折线图绘制

时间:2017-04-08 14:45:53      阅读:509      评论:0      收藏:0      [点我收藏+]

标签:title   pch   create   导入   factory   label   代码   gen   步骤   

 

效果如下:

  技术分享

实现步骤:

  1.导入JAR包

    技术分享

    jfreechart官网下载的zip文件中包含这两个jar包

  2.代码编写

  1 import org.jfree.chart.ChartFactory;
  2 import org.jfree.chart.ChartPanel;
  3 import org.jfree.chart.JFreeChart;
  4 import org.jfree.chart.axis.DateAxis;
  5 import org.jfree.chart.axis.DateTickUnit;
  6 import org.jfree.chart.labels.ItemLabelAnchor;
  7 import org.jfree.chart.labels.ItemLabelPosition;
  8 import org.jfree.chart.labels.StandardXYItemLabelGenerator;
  9 import org.jfree.chart.plot.XYPlot;
 10 import org.jfree.chart.renderer.xy.XYItemRenderer;
 11 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
 12 import org.jfree.chart.title.TextTitle;
 13 import org.jfree.data.time.Month;
 14 import org.jfree.data.time.TimeSeries;
 15 import org.jfree.data.time.TimeSeriesCollection;
 16 import org.jfree.ui.TextAnchor;
 17 
 18 import javax.swing.*;
 19 import java.awt.*;
 20 import java.text.SimpleDateFormat;
 21 
 22 public class TimeSeriesChart {
 23     private TimeSeriesChart() {
 24 
 25         // A网站的访问量统计
 26         TimeSeries timeSeries1 = new TimeSeries("A", Month.class);
 27         // 添加数据
 28         timeSeries1.add(new Month(1, 2016), 154);
 29         timeSeries1.add(new Month(2, 2016), 256);
 30         timeSeries1.add(new Month(3, 2016), 312);
 31         timeSeries1.add(new Month(4, 2016), 489);
 32         timeSeries1.add(new Month(5, 2016), 563);
 33         timeSeries1.add(new Month(6, 2016), 555);
 34         timeSeries1.add(new Month(7, 2016), 359);
 35         timeSeries1.add(new Month(8, 2016), 291);
 36         timeSeries1.add(new Month(9, 2016), 123);
 37         timeSeries1.add(new Month(10, 2016), 438);
 38         timeSeries1.add(new Month(11, 2016), 286);
 39 
 40         // A网站的访问量统计
 41         TimeSeries timeSeries2 = new TimeSeries("A", Month.class);
 42         // 添加数据
 43         timeSeries2.add(new Month(1, 2016), 124);
 44         timeSeries2.add(new Month(2, 2016), 326);
 45         timeSeries2.add(new Month(3, 2016), 12);
 46         timeSeries2.add(new Month(4, 2016), 567);
 47         timeSeries2.add(new Month(5, 2016), 546);
 48         timeSeries2.add(new Month(6, 2016), 123);
 49         timeSeries2.add(new Month(7, 2016), 222);
 50         timeSeries2.add(new Month(8, 2016), 545);
 51         timeSeries2.add(new Month(9, 2016), 56);
 52         timeSeries2.add(new Month(10, 2016), 543);
 53         timeSeries2.add(new Month(11, 2016), 221);
 54 
 55         // 定义时间序列的集合
 56         TimeSeriesCollection lineDataset = new TimeSeriesCollection();
 57         lineDataset.addSeries(timeSeries1);
 58         lineDataset.addSeries(timeSeries2);
 59 
 60 
 61 //         JFreeChart chart = ChartFactory.createXYStepChart("Time line graph", "M", "F", xySeriesCollection, PlotOrientation.HORIZONTAL, false, false, false);
 62         JFreeChart chart = ChartFactory.createTimeSeriesChart("Time line graph", "M", "F", lineDataset, false, false, false);
 63         //设置主标题
 64         chart.setTitle(new TextTitle("A,B网站访问量统计对比图"));
 65         //设置子标题
 66         TextTitle subtitle = new TextTitle("2016年度", new Font("宋体", Font.BOLD, 12));
 67         chart.addSubtitle(subtitle);
 68 
 69         chart.setAntiAlias(true);
 70 
 71         //设置时间轴的范围。
 72         XYPlot plot = (XYPlot) chart.getPlot();
 73         DateAxis dateaxis = (DateAxis) plot.getDomainAxis();
 74         dateaxis.setDateFormatOverride(new SimpleDateFormat("M"));
 75         dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.MONTH, 1));
 76 
 77         //设置曲线是否显示数据点
 78         XYLineAndShapeRenderer xylinerenderer = (XYLineAndShapeRenderer) plot.getRenderer();
 79         xylinerenderer.setBaseShapesVisible(true);
 80 
 81         //设置曲线显示各数据点的值
 82         XYItemRenderer xyitem = plot.getRenderer();
 83         xyitem.setBaseItemLabelsVisible(true);
 84         xyitem.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
 85         xyitem.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
 86         xyitem.setBaseItemLabelFont(new Font("Dialog", Font.BOLD, 12));
 87         plot.setRenderer(xyitem);
 88 
 89         JPanel jPanel = new ChartPanel(chart);
 90 
 91 
 92         JFrame frame = new JFrame("JFreechart Test");
 93         frame.add(jPanel);
 94         frame.setBounds(0, 0, 800, 600);
 95         frame.setVisible(true);
 96     }
 97 
 98     public static void main(String[] args) {
 99         TimeSeriesChart timeSeriesChart = new TimeSeriesChart();
100     }
101 }

 

JAVA Swing使用JFreeChart实现折线图绘制

标签:title   pch   create   导入   factory   label   代码   gen   步骤   

原文地址:http://www.cnblogs.com/adversary/p/6681776.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!