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.DatasetRenderingOrder; 51 import org.jfree.chart.plot.PlotOrientation; 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 DualAxisDemo extends ApplicationFrame { 65 66 71 public DualAxisDemo(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.VERTICAL, 84 true, true, false ); 88 89 chart.setBackgroundPaint(Color.white); 91 chart.getLegend().setAnchor(Legend.SOUTH); 92 93 CategoryPlot plot = chart.getCategoryPlot(); 95 plot.setBackgroundPaint(new Color (0xEE, 0xEE, 0xFF)); 96 plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT); 97 98 CategoryDataset dataset2 = createDataset2(); 99 plot.setSecondaryDataset(0, dataset2); 100 plot.mapSecondaryDatasetToRangeAxis(0, new Integer (0)); 101 102 ValueAxis axis2 = new NumberAxis("Secondary"); 103 plot.setSecondaryRangeAxis(0, axis2); 104 105 LineAndShapeRenderer renderer2 = new LineAndShapeRenderer(); 106 plot.setSecondaryRenderer(0, renderer2); 107 plot.setDatasetRenderingOrder(DatasetRenderingOrder.REVERSE); 108 110 ChartPanel chartPanel = new ChartPanel(chart); 112 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 113 setContentPane(chartPanel); 114 115 } 116 117 128 133 private CategoryDataset createDataset1() { 134 135 String series1 = "First"; 137 String series2 = "Second"; 138 String series3 = "Third"; 139 140 String category1 = "Category 1"; 142 String category2 = "Category 2"; 143 String category3 = "Category 3"; 144 String category4 = "Category 4"; 145 String category5 = "Category 5"; 146 String category6 = "Category 6"; 147 String category7 = "Category 7"; 148 String category8 = "Category 8"; 149 150 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 152 153 dataset.addValue(1.0, series1, category1); 154 dataset.addValue(4.0, series1, category2); 155 dataset.addValue(3.0, series1, category3); 156 dataset.addValue(5.0, series1, category4); 157 dataset.addValue(5.0, series1, category5); 158 dataset.addValue(7.0, series1, category6); 159 dataset.addValue(7.0, series1, category7); 160 dataset.addValue(8.0, series1, category8); 161 162 dataset.addValue(5.0, series2, category1); 163 dataset.addValue(7.0, series2, category2); 164 dataset.addValue(6.0, series2, category3); 165 dataset.addValue(8.0, series2, category4); 166 dataset.addValue(4.0, series2, category5); 167 dataset.addValue(4.0, series2, category6); 168 dataset.addValue(2.0, series2, category7); 169 dataset.addValue(1.0, series2, category8); 170 171 dataset.addValue(4.0, series3, category1); 172 dataset.addValue(3.0, series3, category2); 173 dataset.addValue(2.0, series3, category3); 174 dataset.addValue(3.0, series3, category4); 175 dataset.addValue(6.0, series3, category5); 176 dataset.addValue(3.0, series3, category6); 177 dataset.addValue(4.0, series3, category7); 178 dataset.addValue(3.0, series3, category8); 179 180 return dataset; 181 182 } 183 184 189 private CategoryDataset createDataset2() { 190 191 String series1 = "Fourth"; 193 194 String category1 = "Category 1"; 196 String category2 = "Category 2"; 197 String category3 = "Category 3"; 198 String category4 = "Category 4"; 199 String category5 = "Category 5"; 200 String category6 = "Category 6"; 201 String category7 = "Category 7"; 202 String category8 = "Category 8"; 203 204 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 206 207 dataset.addValue(15.0, series1, category1); 208 dataset.addValue(24.0, series1, category2); 209 dataset.addValue(31.0, series1, category3); 210 dataset.addValue(25.0, series1, category4); 211 dataset.addValue(56.0, series1, category5); 212 dataset.addValue(37.0, series1, category6); 213 dataset.addValue(77.0, series1, category7); 214 dataset.addValue(18.0, series1, category8); 215 216 return dataset; 217 218 } 219 220 225 public static void main(String [] args) { 226 227 DualAxisDemo demo = new DualAxisDemo("Dual Axis Demo"); 228 demo.pack(); 229 RefineryUtilities.centerFrameOnScreen(demo); 230 demo.setVisible(true); 231 232 } 233 234 } 235 | Popular Tags |