1 37 38 package org.jfree.chart.demo; 39 40 import java.awt.Font ; 41 42 import org.jfree.chart.ChartPanel; 43 import org.jfree.chart.JFreeChart; 44 import org.jfree.chart.Legend; 45 import org.jfree.chart.axis.CategoryAxis; 46 import org.jfree.chart.axis.NumberAxis; 47 import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; 48 import org.jfree.chart.plot.CategoryPlot; 49 import org.jfree.chart.plot.CombinedDomainCategoryPlot; 50 import org.jfree.chart.renderer.BarRenderer; 51 import org.jfree.chart.renderer.LineAndShapeRenderer; 52 import org.jfree.data.CategoryDataset; 53 import org.jfree.data.DefaultCategoryDataset; 54 import org.jfree.ui.ApplicationFrame; 55 import org.jfree.ui.RefineryUtilities; 56 57 62 public class CombinedCategoryPlotDemo1 extends ApplicationFrame { 63 64 69 public CombinedCategoryPlotDemo1(String title) { 70 71 super(title); 72 73 ChartPanel chartPanel = new ChartPanel(createChart()); 75 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 76 setContentPane(chartPanel); 77 78 } 79 80 85 public CategoryDataset createDataset1() { 86 87 DefaultCategoryDataset result = new DefaultCategoryDataset(); 88 89 String series1 = "First"; 91 String series2 = "Second"; 92 93 String type1 = "Type 1"; 95 String type2 = "Type 2"; 96 String type3 = "Type 3"; 97 String type4 = "Type 4"; 98 String type5 = "Type 5"; 99 String type6 = "Type 6"; 100 String type7 = "Type 7"; 101 String type8 = "Type 8"; 102 103 result.addValue(1.0, series1, type1); 104 result.addValue(4.0, series1, type2); 105 result.addValue(3.0, series1, type3); 106 result.addValue(5.0, series1, type4); 107 result.addValue(5.0, series1, type5); 108 result.addValue(7.0, series1, type6); 109 result.addValue(7.0, series1, type7); 110 result.addValue(8.0, series1, type8); 111 112 result.addValue(5.0, series2, type1); 113 result.addValue(7.0, series2, type2); 114 result.addValue(6.0, series2, type3); 115 result.addValue(8.0, series2, type4); 116 result.addValue(4.0, series2, type5); 117 result.addValue(4.0, series2, type6); 118 result.addValue(2.0, series2, type7); 119 result.addValue(1.0, series2, type8); 120 121 return result; 122 123 } 124 125 130 public CategoryDataset createDataset2() { 131 132 DefaultCategoryDataset result = new DefaultCategoryDataset(); 133 134 String series1 = "Third"; 136 String series2 = "Fourth"; 137 138 String type1 = "Type 1"; 140 String type2 = "Type 2"; 141 String type3 = "Type 3"; 142 String type4 = "Type 4"; 143 String type5 = "Type 5"; 144 String type6 = "Type 6"; 145 String type7 = "Type 7"; 146 String type8 = "Type 8"; 147 148 result.addValue(11.0, series1, type1); 149 result.addValue(14.0, series1, type2); 150 result.addValue(13.0, series1, type3); 151 result.addValue(15.0, series1, type4); 152 result.addValue(15.0, series1, type5); 153 result.addValue(17.0, series1, type6); 154 result.addValue(17.0, series1, type7); 155 result.addValue(18.0, series1, type8); 156 157 result.addValue(15.0, series2, type1); 158 result.addValue(17.0, series2, type2); 159 result.addValue(16.0, series2, type3); 160 result.addValue(18.0, series2, type4); 161 result.addValue(14.0, series2, type5); 162 result.addValue(14.0, series2, type6); 163 result.addValue(12.0, series2, type7); 164 result.addValue(11.0, series2, type8); 165 166 return result; 167 168 } 169 170 181 186 private JFreeChart createChart() { 187 188 CategoryDataset dataset1 = createDataset1(); 189 NumberAxis rangeAxis1 = new NumberAxis("Value"); 190 rangeAxis1.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 191 LineAndShapeRenderer renderer1 = new LineAndShapeRenderer(); 192 renderer1.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); 193 CategoryPlot subplot1 = new CategoryPlot(dataset1, null, rangeAxis1, renderer1); 194 subplot1.setDomainGridlinesVisible(true); 195 196 CategoryDataset dataset2 = createDataset2(); 197 NumberAxis rangeAxis2 = new NumberAxis("Value"); 198 rangeAxis2.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 199 BarRenderer renderer2 = new BarRenderer(); 200 renderer2.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); 201 CategoryPlot subplot2 = new CategoryPlot(dataset2, null, rangeAxis2, renderer2); 202 subplot2.setDomainGridlinesVisible(true); 203 204 CategoryAxis domainAxis = new CategoryAxis("Category"); 205 CombinedDomainCategoryPlot plot = new CombinedDomainCategoryPlot(domainAxis); 206 plot.add(subplot1, 2); 207 plot.add(subplot2, 1); 208 209 JFreeChart result = new JFreeChart( 210 "Combined Domain Category Plot Demo", 211 new Font ("SansSerif", Font.BOLD, 12), 212 plot, 213 true 214 ); 215 result.getLegend().setAnchor(Legend.SOUTH); 216 return result; 217 218 } 219 220 225 public static void main(String [] args) { 226 227 String title = "Combined Category Plot Demo 1"; 228 CombinedCategoryPlotDemo1 demo = new CombinedCategoryPlotDemo1(title); 229 demo.pack(); 230 RefineryUtilities.centerFrameOnScreen(demo); 231 demo.setVisible(true); 232 233 } 234 235 } 236 | Popular Tags |