1 37 38 package org.jfree.chart.demo; 39 40 import java.awt.BasicStroke ; 41 import java.awt.Color ; 42 import java.awt.Polygon ; 43 import java.awt.Shape ; 44 import java.awt.geom.Rectangle2D ; 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.DefaultDrawingSupplier; 53 import org.jfree.chart.plot.DrawingSupplier; 54 import org.jfree.chart.plot.PlotOrientation; 55 import org.jfree.chart.renderer.LineAndShapeRenderer; 56 import org.jfree.data.DefaultCategoryDataset; 57 import org.jfree.ui.ApplicationFrame; 58 import org.jfree.ui.RefineryUtilities; 59 60 65 public class LineChartDemo5 extends ApplicationFrame { 66 67 72 public LineChartDemo5(String title) { 73 74 super(title); 75 76 String series1 = "First"; 78 String series2 = "Second"; 79 String series3 = "Third"; 80 81 String type1 = "Type 1"; 83 String type2 = "Type 2"; 84 String type3 = "Type 3"; 85 String type4 = "Type 4"; 86 String type5 = "Type 5"; 87 String type6 = "Type 6"; 88 String type7 = "Type 7"; 89 String type8 = "Type 8"; 90 91 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 93 94 dataset.addValue(1.0, series1, type1); 95 dataset.addValue(4.0, series1, type2); 96 dataset.addValue(3.0, series1, type3); 97 dataset.addValue(5.0, series1, type4); 98 dataset.addValue(5.0, series1, type5); 99 dataset.addValue(7.0, series1, type6); 100 dataset.addValue(7.0, series1, type7); 101 dataset.addValue(8.0, series1, type8); 102 103 dataset.addValue(5.0, series2, type1); 104 dataset.addValue(7.0, series2, type2); 105 dataset.addValue(6.0, series2, type3); 106 dataset.addValue(8.0, series2, type4); 107 dataset.addValue(4.0, series2, type5); 108 dataset.addValue(4.0, series2, type6); 109 dataset.addValue(2.0, series2, type7); 110 dataset.addValue(1.0, series2, type8); 111 112 dataset.addValue(4.0, series3, type1); 113 dataset.addValue(3.0, series3, type2); 114 dataset.addValue(2.0, series3, type3); 115 dataset.addValue(3.0, series3, type4); 116 dataset.addValue(6.0, series3, type5); 117 dataset.addValue(3.0, series3, type6); 118 dataset.addValue(4.0, series3, type7); 119 dataset.addValue(3.0, series3, type8); 120 121 JFreeChart chart = ChartFactory.createLineChart( 123 "Line Chart Demo 5", "Type", "Value", dataset, PlotOrientation.VERTICAL, true, true, false ); 132 133 StandardLegend legend = (StandardLegend) chart.getLegend(); 135 legend.setDisplaySeriesShapes(true); 136 137 Shape [] shapes = new Shape [3]; 138 int[] xpoints; 139 int[] ypoints; 140 141 xpoints = new int[] {-3, 3, -3}; 143 ypoints = new int[] {-3, 0, 3}; 144 shapes[0] = new Polygon (xpoints, ypoints, 3); 145 146 shapes[1] = new Rectangle2D.Double (-2, -3, 3, 6); 148 149 xpoints = new int[] {-3, 3, 3}; 151 ypoints = new int[] {0, -3, 3}; 152 shapes[2] = new Polygon (xpoints, ypoints, 3); 153 154 DrawingSupplier supplier = new DefaultDrawingSupplier( 155 DefaultDrawingSupplier.DEFAULT_PAINT_SEQUENCE, 156 DefaultDrawingSupplier.DEFAULT_OUTLINE_PAINT_SEQUENCE, 157 DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE, 158 DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE, 159 shapes 160 ); 161 CategoryPlot plot = chart.getCategoryPlot(); 162 plot.setDrawingSupplier(supplier); 163 164 chart.setBackgroundPaint(Color.yellow); 165 166 plot.getRenderer().setSeriesStroke(0, new BasicStroke (2.0f, 168 BasicStroke.CAP_ROUND, 169 BasicStroke.JOIN_ROUND, 170 1.0f, 171 new float[] {10.0f, 6.0f}, 172 0.0f)); 173 plot.getRenderer().setSeriesStroke(1, new BasicStroke (2.0f, 174 BasicStroke.CAP_ROUND, 175 BasicStroke.JOIN_ROUND, 176 1.0f, 177 new float[] {6.0f, 6.0f}, 178 0.0f)); 179 plot.getRenderer().setSeriesStroke(2, new BasicStroke (2.0f, 180 BasicStroke.CAP_ROUND, 181 BasicStroke.JOIN_ROUND, 182 1.0f, 183 new float[] {2.0f, 6.0f}, 184 0.0f)); 185 186 LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer(); 188 renderer.setDrawShapes(true); 189 renderer.setItemLabelsVisible(Boolean.TRUE); 190 191 NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); 193 rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 194 rangeAxis.setAutoRangeIncludesZero(false); 195 rangeAxis.setUpperMargin(0.12); 196 197 199 ChartPanel chartPanel = new ChartPanel(chart); 201 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 202 setContentPane(chartPanel); 203 204 } 205 206 217 222 public static void main(String [] args) { 223 224 LineChartDemo5 demo = new LineChartDemo5("Line Chart Demo 5"); 225 demo.pack(); 226 RefineryUtilities.centerFrameOnScreen(demo); 227 demo.setVisible(true); 228 229 } 230 231 } 232 | Popular Tags |