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.CategoryLabelPosition; 47 import org.jfree.chart.axis.NumberAxis; 48 import org.jfree.chart.axis.ValueAxis; 49 import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; 50 import org.jfree.chart.plot.CategoryPlot; 51 import org.jfree.chart.plot.CombinedRangeCategoryPlot; 52 import org.jfree.chart.renderer.BarRenderer; 53 import org.jfree.chart.renderer.LineAndShapeRenderer; 54 import org.jfree.data.CategoryDataset; 55 import org.jfree.data.DefaultCategoryDataset; 56 import org.jfree.text.TextBlockAnchor; 57 import org.jfree.ui.ApplicationFrame; 58 import org.jfree.ui.RectangleAnchor; 59 import org.jfree.ui.RefineryUtilities; 60 import org.jfree.ui.TextAnchor; 61 62 67 public class CombinedCategoryPlotDemo2 extends ApplicationFrame { 68 69 74 public CombinedCategoryPlotDemo2(String title) { 75 76 super(title); 77 78 ChartPanel chartPanel = new ChartPanel(createChart()); 80 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 81 setContentPane(chartPanel); 82 83 } 84 85 90 public CategoryDataset createDataset1() { 91 92 DefaultCategoryDataset result = new DefaultCategoryDataset(); 93 94 String series1 = "First"; 96 String series2 = "Second"; 97 98 String type1 = "Type 1"; 100 String type2 = "Type 2"; 101 String type3 = "Type 3"; 102 String type4 = "Type 4"; 103 String type5 = "Type 5"; 104 String type6 = "Type 6"; 105 String type7 = "Type 7"; 106 String type8 = "Type 8"; 107 108 result.addValue(1.0, series1, type1); 109 result.addValue(4.0, series1, type2); 110 result.addValue(3.0, series1, type3); 111 result.addValue(5.0, series1, type4); 112 result.addValue(5.0, series1, type5); 113 result.addValue(7.0, series1, type6); 114 result.addValue(7.0, series1, type7); 115 result.addValue(8.0, series1, type8); 116 117 result.addValue(5.0, series2, type1); 118 result.addValue(7.0, series2, type2); 119 result.addValue(6.0, series2, type3); 120 result.addValue(8.0, series2, type4); 121 result.addValue(4.0, series2, type5); 122 result.addValue(4.0, series2, type6); 123 result.addValue(2.0, series2, type7); 124 result.addValue(1.0, series2, type8); 125 126 return result; 127 128 } 129 130 135 public CategoryDataset createDataset2() { 136 137 DefaultCategoryDataset result = new DefaultCategoryDataset(); 138 139 String series1 = "Third"; 141 String series2 = "Fourth"; 142 143 String sector1 = "Sector 1"; 145 String sector2 = "Sector 2"; 146 String sector3 = "Sector 3"; 147 String sector4 = "Sector 4"; 148 149 result.addValue(11.0, series1, sector1); 150 result.addValue(14.0, series1, sector2); 151 result.addValue(13.0, series1, sector3); 152 result.addValue(15.0, series1, sector4); 153 154 result.addValue(15.0, series2, sector1); 155 result.addValue(17.0, series2, sector2); 156 result.addValue(16.0, series2, sector3); 157 result.addValue(18.0, series2, sector4); 158 159 return result; 160 161 } 162 163 174 179 private JFreeChart createChart() { 180 181 CategoryDataset dataset1 = createDataset1(); 182 CategoryAxis domainAxis1 = new CategoryAxis("Class 1"); 183 LineAndShapeRenderer renderer1 = new LineAndShapeRenderer(); 184 renderer1.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); 185 CategoryPlot subplot1 = new CategoryPlot(dataset1, domainAxis1, null, renderer1); 186 subplot1.setDomainGridlinesVisible(true); 187 CategoryLabelPosition position1 = new CategoryLabelPosition( 188 RectangleAnchor.TOP, TextBlockAnchor.TOP_RIGHT, TextAnchor.TOP_RIGHT, -Math.PI / 8.0 189 ); 190 domainAxis1.setBottomCategoryLabelPosition(position1); 191 192 CategoryDataset dataset2 = createDataset2(); 193 CategoryAxis domainAxis2 = new CategoryAxis("Class 2"); 194 BarRenderer renderer2 = new BarRenderer(); 195 renderer2.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); 196 CategoryPlot subplot2 = new CategoryPlot(dataset2, domainAxis2, null, renderer2); 197 subplot2.setDomainGridlinesVisible(true); 198 CategoryLabelPosition position2 = new CategoryLabelPosition( 199 RectangleAnchor.TOP, TextBlockAnchor.TOP_RIGHT, TextAnchor.TOP_RIGHT, -Math.PI / 8.0 200 ); 201 domainAxis2.setBottomCategoryLabelPosition(position2); 202 203 ValueAxis rangeAxis = new NumberAxis("Value"); 204 CombinedRangeCategoryPlot plot = new CombinedRangeCategoryPlot(rangeAxis); 205 plot.add(subplot1, 3); 206 plot.add(subplot2, 2); 207 208 JFreeChart result = new JFreeChart( 209 "Combined Range Category Plot Demo", 210 new Font ("SansSerif", Font.BOLD, 12), 211 plot, 212 true 213 ); 214 result.getLegend().setAnchor(Legend.SOUTH); 215 return result; 216 217 } 218 219 224 public static void main(String [] args) { 225 226 String title = "Combined Category Plot Demo 2"; 227 CombinedCategoryPlotDemo2 demo = new CombinedCategoryPlotDemo2(title); 228 demo.pack(); 229 RefineryUtilities.centerFrameOnScreen(demo); 230 demo.setVisible(true); 231 232 } 233 234 } 235 | Popular Tags |