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.NumberAxis3D; 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.CategoryItemRenderer; 53 import org.jfree.chart.renderer.LineAndShapeRenderer; 54 import org.jfree.data.CategoryDataset; 55 import org.jfree.data.DefaultCategoryDataset; 56 import org.jfree.ui.ApplicationFrame; 57 import org.jfree.ui.RefineryUtilities; 58 59 65 public class DualAxisDemo4 extends ApplicationFrame { 66 67 72 public DualAxisDemo4(String title) { 73 74 super(title); 75 76 CategoryDataset dataset1 = createDataset1(); 77 78 JFreeChart chart = ChartFactory.createBarChart3D( 80 "Dual Axis Chart", "Category", "Value", dataset1, PlotOrientation.VERTICAL, 85 true, true, 87 false 88 ); 89 90 92 chart.setBackgroundPaint(new Color (0xCC, 0xFF, 0xCC)); 94 chart.getLegend().setAnchor(Legend.SOUTH); 95 96 CategoryPlot plot = chart.getCategoryPlot(); 98 plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT); 99 plot.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT); 100 CategoryItemRenderer renderer1 = plot.getRenderer(); 101 renderer1.setSeriesPaint(0, Color.red); 102 renderer1.setSeriesPaint(1, Color.yellow); 103 renderer1.setSeriesPaint(2, Color.green); 104 CategoryDataset dataset2 = createDataset2(); 105 ValueAxis axis2 = new NumberAxis3D("Secondary"); 106 plot.setSecondaryRangeAxis(0, axis2); 107 plot.setSecondaryDataset(0, dataset2); 108 plot.mapSecondaryDatasetToRangeAxis(0, new Integer (0)); 109 CategoryItemRenderer renderer2 = new LineAndShapeRenderer(); 110 renderer2.setSeriesPaint(0, Color.blue); 111 plot.setSecondaryRenderer(0, renderer2); 112 113 plot.setDatasetRenderingOrder(DatasetRenderingOrder.REVERSE); 114 116 ChartPanel chartPanel = new ChartPanel(chart); 118 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 119 setContentPane(chartPanel); 120 121 } 122 123 134 139 private CategoryDataset createDataset1() { 140 141 String series1 = "First"; 143 String series2 = "Second"; 144 String series3 = "Third"; 145 146 String category1 = "Category 1"; 148 String category2 = "Category 2"; 149 String category3 = "Category 3"; 150 String category4 = "Category 4"; 151 String category5 = "Category 5"; 152 153 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 155 156 dataset.addValue(1.0, series1, category1); 157 dataset.addValue(4.0, series1, category2); 158 dataset.addValue(3.0, series1, category3); 159 dataset.addValue(5.0, series1, category4); 160 dataset.addValue(5.0, series1, category5); 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 168 dataset.addValue(4.0, series3, category1); 169 dataset.addValue(3.0, series3, category2); 170 dataset.addValue(2.0, series3, category3); 171 dataset.addValue(3.0, series3, category4); 172 dataset.addValue(6.0, series3, category5); 173 174 return dataset; 175 176 } 177 178 183 private CategoryDataset createDataset2() { 184 185 String series1 = "Fourth"; 187 188 String category1 = "Category 1"; 190 String category2 = "Category 2"; 191 String category3 = "Category 3"; 192 String category4 = "Category 4"; 193 String category5 = "Category 5"; 194 195 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 197 198 dataset.addValue(15.0, series1, category1); 199 dataset.addValue(24.0, series1, category2); 200 dataset.addValue(31.0, series1, category3); 201 dataset.addValue(25.0, series1, category4); 202 dataset.addValue(56.0, series1, category5); 203 204 return dataset; 205 206 } 207 208 213 public static void main(String [] args) { 214 215 DualAxisDemo4 demo = new DualAxisDemo4("Dual Axis Demo 4"); 216 demo.pack(); 217 RefineryUtilities.centerFrameOnScreen(demo); 218 demo.setVisible(true); 219 220 } 221 222 } 223 | Popular Tags |