1 37 38 package org.jfree.chart.demo; 39 40 import java.awt.Color ; 41 42 import org.jfree.chart.ChartFactory; 43 import org.jfree.chart.ChartPanel; 44 import org.jfree.chart.JFreeChart; 45 import org.jfree.chart.Legend; 46 import org.jfree.chart.axis.AxisLocation; 47 import org.jfree.chart.axis.NumberAxis; 48 import org.jfree.chart.axis.ValueAxis; 49 import org.jfree.chart.plot.CategoryPlot; 50 import org.jfree.chart.plot.PlotOrientation; 51 import org.jfree.chart.renderer.BarRenderer; 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 63 public class DualAxisDemo5 extends ApplicationFrame { 64 65 70 public DualAxisDemo5(String title) { 71 72 super(title); 73 74 CategoryDataset dataset1 = createDataset1(); 75 76 JFreeChart chart = ChartFactory.createBarChart( 78 "Dual Axis Chart", "Category", "Value", dataset1, PlotOrientation.VERTICAL, 83 true, true, false ); 87 88 chart.setBackgroundPaint(Color.white); 90 chart.getLegend().setAnchor(Legend.SOUTH); 91 92 CategoryPlot plot = chart.getCategoryPlot(); 94 plot.setBackgroundPaint(new Color (0xEE, 0xEE, 0xFF)); 95 plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT); 96 97 CategoryDataset dataset2 = createDataset2(); 98 plot.setSecondaryDataset(0, dataset2); 99 plot.mapSecondaryDatasetToRangeAxis(0, new Integer (0)); 100 101 ValueAxis axis2 = new NumberAxis("Secondary"); 102 plot.setSecondaryRangeAxis(0, axis2); 103 104 BarRenderer renderer2 = new BarRenderer(); 105 plot.setSecondaryRenderer(0, renderer2); 106 108 ChartPanel chartPanel = new ChartPanel(chart); 110 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 111 setContentPane(chartPanel); 112 113 } 114 115 126 131 private CategoryDataset createDataset1() { 132 133 String series1 = "First"; 135 String series2 = "Second"; 136 137 String category1 = "Category 1"; 139 String category2 = "Category 2"; 140 String category3 = "Category 3"; 141 String category4 = "Category 4"; 142 String category5 = "Category 5"; 143 String category6 = "Category 6"; 144 String category7 = "Category 7"; 145 String category8 = "Category 8"; 146 147 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 149 150 dataset.addValue(1.0, series1, category1); 151 dataset.addValue(4.0, series1, category2); 152 dataset.addValue(3.0, series1, category3); 153 dataset.addValue(5.0, series1, category4); 154 dataset.addValue(5.0, series1, category5); 155 dataset.addValue(7.0, series1, category6); 156 dataset.addValue(7.0, series1, category7); 157 dataset.addValue(8.0, series1, category8); 158 159 dataset.addValue(null, series2, category1); 160 dataset.addValue(null, series2, category2); 161 dataset.addValue(null, series2, category3); 162 dataset.addValue(null, series2, category4); 163 dataset.addValue(null, series2, category5); 164 dataset.addValue(null, series2, category6); 165 dataset.addValue(null, series2, category7); 166 dataset.addValue(null, series2, category8); 167 168 return dataset; 169 170 } 171 172 177 private CategoryDataset createDataset2() { 178 179 String series1 = "Third"; 181 String series2 = "Fourth"; 182 183 String category1 = "Category 1"; 185 String category2 = "Category 2"; 186 String category3 = "Category 3"; 187 String category4 = "Category 4"; 188 String category5 = "Category 5"; 189 String category6 = "Category 6"; 190 String category7 = "Category 7"; 191 String category8 = "Category 8"; 192 193 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 195 196 dataset.addValue(null, series1, category1); 197 dataset.addValue(null, series1, category2); 198 dataset.addValue(null, series1, category3); 199 dataset.addValue(null, series1, category4); 200 dataset.addValue(null, series1, category5); 201 dataset.addValue(null, series1, category6); 202 dataset.addValue(null, series1, category7); 203 dataset.addValue(null, series1, category8); 204 205 dataset.addValue(75.0, series2, category1); 206 dataset.addValue(87.0, series2, category2); 207 dataset.addValue(96.0, series2, category3); 208 dataset.addValue(68.0, series2, category4); 209 dataset.addValue(84.0, series2, category5); 210 dataset.addValue(94.0, series2, category6); 211 dataset.addValue(82.0, series2, category7); 212 dataset.addValue(71.0, series2, category8); 213 214 return dataset; 215 216 } 217 218 223 public static void main(String [] args) { 224 225 DualAxisDemo5 demo = new DualAxisDemo5("Dual Axis Demo 5"); 226 demo.pack(); 227 RefineryUtilities.centerFrameOnScreen(demo); 228 demo.setVisible(true); 229 230 } 231 232 } 233 | Popular Tags |