1 40 41 package org.jfree.chart.demo; 42 43 import org.jfree.chart.ChartPanel; 44 import org.jfree.chart.JFreeChart; 45 import org.jfree.chart.StandardLegend; 46 import org.jfree.chart.axis.CategoryAxis; 47 import org.jfree.chart.axis.NumberAxis; 48 import org.jfree.chart.axis.ValueAxis; 49 import org.jfree.chart.labels.CategoryItemLabelGenerator; 50 import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; 51 import org.jfree.chart.plot.CategoryPlot; 52 import org.jfree.chart.plot.DatasetRenderingOrder; 53 import org.jfree.chart.plot.PlotOrientation; 54 import org.jfree.chart.renderer.BarRenderer; 55 import org.jfree.chart.renderer.CategoryItemRenderer; 56 import org.jfree.chart.renderer.LineAndShapeRenderer; 57 import org.jfree.data.CategoryDataset; 58 import org.jfree.data.DatasetUtilities; 59 import org.jfree.ui.ApplicationFrame; 60 import org.jfree.ui.RefineryUtilities; 61 62 70 public class OverlaidBarChartDemo extends ApplicationFrame { 71 72 77 public OverlaidBarChartDemo(String title) { 78 79 super(title); 80 81 double[][] data1 = new double[][] { 83 {1.0, 4.0, 3.0, 5.0, 5.0, 7.0, 7.0, 8.0}, 84 {5.0, 7.0, 6.0, 8.0, 4.0, 4.0, 2.0, 1.0} 85 }; 86 CategoryDataset dataset1 = DatasetUtilities.createCategoryDataset( 87 "S", "Category ", data1 88 ); 89 90 CategoryItemLabelGenerator tooltips = new StandardCategoryItemLabelGenerator(); 92 CategoryItemRenderer renderer = new BarRenderer(); 93 renderer.setItemLabelGenerator(tooltips); 94 CategoryPlot plot = new CategoryPlot(); 95 plot.setDataset(dataset1); 96 plot.setRenderer(new BarRenderer()); 97 98 plot.setDomainAxis(new CategoryAxis("Category")); 99 plot.setRangeAxis(new NumberAxis("Value")); 100 101 plot.setOrientation(PlotOrientation.VERTICAL); 102 plot.setRangeGridlinesVisible(true); 103 plot.setDomainGridlinesVisible(true); 104 105 ValueAxis rangeAxis2 = new NumberAxis("Axis 2"); 106 plot.setSecondaryRangeAxis(0, rangeAxis2); 107 108 double[][] data2 = new double[][] { 110 {9.0, 7.0, 2.0, 6.0, 6.0, 9.0, 5.0, 4.0} 111 }; 112 CategoryDataset dataset2 = DatasetUtilities.createCategoryDataset("T", "Category", data2); 113 CategoryItemRenderer renderer2 = new LineAndShapeRenderer(); 114 plot.setSecondaryDataset(0, dataset2); 115 plot.setSecondaryRenderer(0, renderer2); 116 117 double[][] data3 = new double[][] { 119 {94.0, 75.0, 22.0, 74.0, 83.0, 9.0, 23.0, 98.0} 120 }; 121 CategoryDataset dataset3 = DatasetUtilities.createCategoryDataset("R", "Category", data3); 122 plot.setSecondaryDataset(1, dataset3); 123 plot.setSecondaryRenderer(1, new LineAndShapeRenderer()); 124 plot.mapSecondaryDatasetToRangeAxis(1, new Integer (0)); 125 126 plot.setDatasetRenderingOrder(DatasetRenderingOrder.REVERSE); 127 JFreeChart chart = new JFreeChart(plot); 128 chart.setTitle("Overlaid Bar Chart"); 129 chart.setLegend(new StandardLegend(chart)); 130 131 ChartPanel chartPanel = new ChartPanel(chart); 133 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 134 setContentPane(chartPanel); 135 136 } 137 138 149 154 public static void main(String [] args) { 155 156 OverlaidBarChartDemo demo = new OverlaidBarChartDemo("Overlaid Bar Chart Demo"); 157 demo.pack(); 158 RefineryUtilities.centerFrameOnScreen(demo); 159 demo.setVisible(true); 160 161 } 162 163 } 164 | Popular Tags |