1 38 39 package org.jfree.chart.demo; 40 41 import java.awt.Color ; 42 43 import org.jfree.chart.ChartFactory; 44 import org.jfree.chart.ChartPanel; 45 import org.jfree.chart.JFreeChart; 46 import org.jfree.chart.Spacer; 47 import org.jfree.chart.StandardLegend; 48 import org.jfree.chart.axis.NumberAxis; 49 import org.jfree.chart.plot.PlotOrientation; 50 import org.jfree.chart.plot.XYPlot; 51 import org.jfree.chart.renderer.StandardXYItemRenderer; 52 import org.jfree.data.XYDataset; 53 import org.jfree.data.XYSeries; 54 import org.jfree.data.XYSeriesCollection; 55 import org.jfree.ui.ApplicationFrame; 56 import org.jfree.ui.RefineryUtilities; 57 58 64 public class LineChartDemo2 extends ApplicationFrame { 65 66 71 public LineChartDemo2(String title) { 72 73 super(title); 74 75 XYDataset dataset = createDataset(); 76 JFreeChart chart = createChart(dataset); 77 ChartPanel chartPanel = new ChartPanel(chart); 78 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 79 setContentPane(chartPanel); 80 81 } 82 83 88 private XYDataset createDataset() { 89 90 XYSeries series1 = new XYSeries("First"); 91 series1.add(1.0, 1.0); 92 series1.add(2.0, 4.0); 93 series1.add(3.0, 3.0); 94 series1.add(4.0, 5.0); 95 series1.add(5.0, 5.0); 96 series1.add(6.0, 7.0); 97 series1.add(7.0, 7.0); 98 series1.add(8.0, 8.0); 99 100 XYSeries series2 = new XYSeries("Second"); 101 series2.add(1.0, 5.0); 102 series2.add(2.0, 7.0); 103 series2.add(3.0, 6.0); 104 series2.add(4.0, 8.0); 105 series2.add(5.0, 4.0); 106 series2.add(6.0, 4.0); 107 series2.add(7.0, 2.0); 108 series2.add(8.0, 1.0); 109 110 XYSeries series3 = new XYSeries("Third"); 111 series3.add(3.0, 4.0); 112 series3.add(4.0, 3.0); 113 series3.add(5.0, 2.0); 114 series3.add(6.0, 3.0); 115 series3.add(7.0, 6.0); 116 series3.add(8.0, 3.0); 117 series3.add(9.0, 4.0); 118 series3.add(10.0, 3.0); 119 120 XYSeriesCollection dataset = new XYSeriesCollection(); 121 dataset.addSeries(series1); 122 dataset.addSeries(series2); 123 dataset.addSeries(series3); 124 125 return dataset; 126 127 } 128 129 136 private JFreeChart createChart(XYDataset dataset) { 137 138 JFreeChart chart = ChartFactory.createXYLineChart( 140 "Line Chart Demo 2", "X", "Y", dataset, PlotOrientation.VERTICAL, 145 true, true, false ); 149 150 chart.setBackgroundPaint(Color.white); 152 153 StandardLegend legend = (StandardLegend) chart.getLegend(); 154 legend.setDisplaySeriesShapes(true); 155 156 XYPlot plot = chart.getXYPlot(); 158 plot.setBackgroundPaint(Color.lightGray); 159 plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0)); 160 plot.setDomainGridlinePaint(Color.white); 161 plot.setRangeGridlinePaint(Color.white); 162 163 StandardXYItemRenderer renderer = (StandardXYItemRenderer) plot.getRenderer(); 164 renderer.setPlotShapes(true); 165 renderer.setShapesFilled(true); 166 167 NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); 169 rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 170 172 return chart; 173 174 } 175 176 187 192 public static void main(String [] args) { 193 194 LineChartDemo2 demo = new LineChartDemo2("Line Chart Demo 2"); 195 demo.pack(); 196 RefineryUtilities.centerFrameOnScreen(demo); 197 demo.setVisible(true); 198 199 } 200 201 } 202 | Popular Tags |