1 40 41 package org.jfree.chart.demo; 42 43 import java.awt.BasicStroke ; 44 import java.awt.Color ; 45 46 import org.jfree.chart.ChartFactory; 47 import org.jfree.chart.ChartPanel; 48 import org.jfree.chart.JFreeChart; 49 import org.jfree.chart.StandardLegend; 50 import org.jfree.chart.axis.NumberAxis; 51 import org.jfree.chart.plot.CategoryPlot; 52 import org.jfree.chart.plot.PlotOrientation; 53 import org.jfree.chart.renderer.ItemLabelPosition; 54 import org.jfree.chart.renderer.LineAndShapeRenderer; 55 import org.jfree.data.CategoryDataset; 56 import org.jfree.data.DefaultCategoryDataset; 57 import org.jfree.ui.ApplicationFrame; 58 import org.jfree.ui.RefineryUtilities; 59 60 66 public class LineChartDemo1 extends ApplicationFrame { 67 68 73 public LineChartDemo1(String title) { 74 75 super(title); 76 77 CategoryDataset dataset = createDataset(); 78 JFreeChart chart = createChart(dataset); 79 80 ChartPanel chartPanel = new ChartPanel(chart); 82 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 83 setContentPane(chartPanel); 84 85 } 86 87 92 private CategoryDataset createDataset() { 93 94 String series1 = "First"; 96 String series2 = "Second"; 97 String series3 = "Third"; 98 99 String type1 = "Type 1"; 101 String type2 = "Type 2"; 102 String type3 = "Type 3"; 103 String type4 = "Type 4"; 104 String type5 = "Type 5"; 105 String type6 = "Type 6"; 106 String type7 = "Type 7"; 107 String type8 = "Type 8"; 108 109 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 111 112 dataset.addValue(1.0, series1, type1); 113 dataset.addValue(4.0, series1, type2); 114 dataset.addValue(3.0, series1, type3); 115 dataset.addValue(5.0, series1, type4); 116 dataset.addValue(5.0, series1, type5); 117 dataset.addValue(7.0, series1, type6); 118 dataset.addValue(7.0, series1, type7); 119 dataset.addValue(8.0, series1, type8); 120 121 dataset.addValue(5.0, series2, type1); 122 dataset.addValue(7.0, series2, type2); 123 dataset.addValue(6.0, series2, type3); 124 dataset.addValue(8.0, series2, type4); 125 dataset.addValue(4.0, series2, type5); 126 dataset.addValue(4.0, series2, type6); 127 dataset.addValue(2.0, series2, type7); 128 dataset.addValue(1.0, series2, type8); 129 130 dataset.addValue(4.0, series3, type1); 131 dataset.addValue(3.0, series3, type2); 132 dataset.addValue(2.0, series3, type3); 133 dataset.addValue(3.0, series3, type4); 134 dataset.addValue(6.0, series3, type5); 135 dataset.addValue(3.0, series3, type6); 136 dataset.addValue(4.0, series3, type7); 137 dataset.addValue(3.0, series3, type8); 138 139 return dataset; 140 141 } 142 143 150 private JFreeChart createChart(CategoryDataset dataset) { 151 152 JFreeChart chart = ChartFactory.createLineChart( 154 "Line Chart Demo 1", "Type", "Value", dataset, PlotOrientation.VERTICAL, true, true, false ); 163 164 StandardLegend legend = (StandardLegend) chart.getLegend(); 166 legend.setDisplaySeriesShapes(true); 167 legend.setDisplaySeriesLines(true); 168 169 chart.setBackgroundPaint(new Color (0xCC, 0xCC, 0xFF)); 170 171 CategoryPlot plot = chart.getCategoryPlot(); 172 173 NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); 175 rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 176 rangeAxis.setAutoRangeIncludesZero(true); 177 rangeAxis.setUpperMargin(0.20); 178 rangeAxis.setLabelAngle(Math.PI / 2.0); 179 180 191 LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer(); 193 renderer.setDrawShapes(true); 194 195 renderer.setSeriesStroke( 196 0, new BasicStroke (2.0f, 197 BasicStroke.CAP_ROUND, 198 BasicStroke.JOIN_ROUND, 199 1.0f, 200 new float[] {10.0f, 6.0f}, 201 0.0f) 202 ); 203 renderer.setSeriesStroke( 204 1, new BasicStroke (2.0f, 205 BasicStroke.CAP_ROUND, 206 BasicStroke.JOIN_ROUND, 207 1.0f, 208 new float[] {6.0f, 6.0f}, 209 0.0f) 210 ); 211 renderer.setSeriesStroke( 212 2, new BasicStroke (2.0f, 213 BasicStroke.CAP_ROUND, 214 BasicStroke.JOIN_ROUND, 215 1.0f, 216 new float[] {2.0f, 6.0f}, 217 0.0f) 218 ); 219 220 renderer.setItemLabelsVisible(true); 221 renderer.setPositiveItemLabelPosition(new ItemLabelPosition()); 222 renderer.setNegativeItemLabelPosition(new ItemLabelPosition()); 223 225 return chart; 226 } 227 228 233 public static void main(String [] args) { 234 235 LineChartDemo1 demo = new LineChartDemo1("Line Chart Demo"); 236 demo.pack(); 237 RefineryUtilities.centerFrameOnScreen(demo); 238 demo.setVisible(true); 239 240 } 241 242 } 243 | Popular Tags |