效果以下:java
實現步驟:ide
1.導入JAR包網站
jfreechart官網下載的zip文件中包含這兩個jar包ui
2.代碼編寫spa
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 }