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.CategoryItemRenderer; 52 import org.jfree.chart.renderer.LineAndShapeRenderer; 53 import org.jfree.data.CategoryDataset; 54 import org.jfree.data.DefaultCategoryDataset; 55 import org.jfree.ui.ApplicationFrame; 56 import org.jfree.ui.RefineryUtilities; 57 58 64 public class DualAxisDemo3 extends ApplicationFrame { 65 66 71 public DualAxisDemo3(String title) { 72 73 super(title); 74 75 CategoryDataset dataset1 = createDataset1(); 76 77 JFreeChart chart = ChartFactory.createBarChart( 79 "Dual Axis Chart", "Category", "Value", dataset1, PlotOrientation.HORIZONTAL, 84 true, true, 86 false 87 ); 88 89 91 chart.setBackgroundPaint(new Color (0xCC, 0xFF, 0xCC)); 93 chart.getLegend().setAnchor(Legend.WEST); 94 95 CategoryPlot plot = chart.getCategoryPlot(); 97 plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT); 98 CategoryDataset dataset2 = createDataset2(); 99 ValueAxis axis2 = new NumberAxis("Secondary"); 100 plot.setSecondaryRangeAxis(0, axis2); 101 plot.setSecondaryDataset(0, dataset2); 102 plot.mapSecondaryDatasetToRangeAxis(0, new Integer (0)); 103 CategoryItemRenderer renderer2 = new LineAndShapeRenderer(); 104 plot.setSecondaryRenderer(0, renderer2); 105 106 108 ChartPanel chartPanel = new ChartPanel(chart); 110 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 111 setContentPane(chartPanel); 112 113 } 114 115 120 private CategoryDataset createDataset1() { 121 122 String series1 = "First"; 124 String series2 = "Second"; 125 String series3 = "Third"; 126 127 String category1 = "Category 1"; 129 String category2 = "Category 2"; 130 String category3 = "Category 3"; 131 String category4 = "Category 4"; 132 String category5 = "Category 5"; 133 String category6 = "Category 6"; 134 String category7 = "Category 7"; 135 String category8 = "Category 8"; 136 137 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 139 140 dataset.addValue(1.0, series1, category1); 141 dataset.addValue(4.0, series1, category2); 142 dataset.addValue(3.0, series1, category3); 143 dataset.addValue(5.0, series1, category4); 144 dataset.addValue(5.0, series1, category5); 145 dataset.addValue(7.0, series1, category6); 146 dataset.addValue(7.0, series1, category7); 147 dataset.addValue(8.0, series1, category8); 148 149 dataset.addValue(5.0, series2, category1); 150 dataset.addValue(7.0, series2, category2); 151 dataset.addValue(6.0, series2, category3); 152 dataset.addValue(8.0, series2, category4); 153 dataset.addValue(4.0, series2, category5); 154 dataset.addValue(4.0, series2, category6); 155 dataset.addValue(2.0, series2, category7); 156 dataset.addValue(1.0, series2, category8); 157 158 dataset.addValue(4.0, series3, category1); 159 dataset.addValue(3.0, series3, category2); 160 dataset.addValue(2.0, series3, category3); 161 dataset.addValue(3.0, series3, category4); 162 dataset.addValue(6.0, series3, category5); 163 dataset.addValue(3.0, series3, category6); 164 dataset.addValue(4.0, series3, category7); 165 dataset.addValue(3.0, series3, category8); 166 167 return dataset; 168 169 } 170 171 176 private CategoryDataset createDataset2() { 177 178 String series1 = "Fourth"; 180 181 String category1 = "Category 1"; 183 String category2 = "Category 2"; 184 String category3 = "Category 3"; 185 String category4 = "Category 4"; 186 String category5 = "Category 5"; 187 String category6 = "Category 6"; 188 String category7 = "Category 7"; 189 String category8 = "Category 8"; 190 191 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 193 194 dataset.addValue(15.0, series1, category1); 195 dataset.addValue(24.0, series1, category2); 196 dataset.addValue(31.0, series1, category3); 197 dataset.addValue(25.0, series1, category4); 198 dataset.addValue(56.0, series1, category5); 199 dataset.addValue(37.0, series1, category6); 200 dataset.addValue(77.0, series1, category7); 201 dataset.addValue(18.0, series1, category8); 202 203 return dataset; 204 205 } 206 207 218 223 public static void main(String [] args) { 224 225 DualAxisDemo3 demo = new DualAxisDemo3("Dual Axis Demo 3"); 226 demo.pack(); 227 RefineryUtilities.centerFrameOnScreen(demo); 228 demo.setVisible(true); 229 230 } 231 232 } 233 | Popular Tags |