1 42 43 package org.jfree.chart.demo; 44 45 import java.awt.Color ; 46 import java.awt.GradientPaint ; 47 48 import org.jfree.chart.ChartFactory; 49 import org.jfree.chart.ChartPanel; 50 import org.jfree.chart.JFreeChart; 51 import org.jfree.chart.axis.NumberAxis; 52 import org.jfree.chart.plot.CategoryPlot; 53 import org.jfree.chart.plot.PlotOrientation; 54 import org.jfree.chart.renderer.BarRenderer; 55 import org.jfree.data.CategoryDataset; 56 import org.jfree.data.DefaultCategoryDataset; 57 import org.jfree.ui.ApplicationFrame; 58 import org.jfree.ui.RefineryUtilities; 59 60 65 public class BarChartDemo extends ApplicationFrame { 66 67 72 public BarChartDemo(String title) { 73 74 super(title); 75 76 CategoryDataset dataset = createDataset(); 77 JFreeChart chart = createChart(dataset); 78 79 ChartPanel chartPanel = new ChartPanel(chart); 81 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 82 setContentPane(chartPanel); 83 84 } 85 86 91 private CategoryDataset createDataset() { 92 93 String series1 = "First"; 95 String series2 = "Second"; 96 String series3 = "Third"; 97 98 String category1 = "Category 1"; 100 String category2 = "Category 2"; 101 String category3 = "Category 3"; 102 String category4 = "Category 4"; 103 String category5 = "Category 5"; 104 105 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 107 108 dataset.addValue(1.0, series1, category1); 109 dataset.addValue(4.0, series1, category2); 110 dataset.addValue(3.0, series1, category3); 111 dataset.addValue(5.0, series1, category4); 112 dataset.addValue(5.0, series1, category5); 113 114 dataset.addValue(5.0, series2, category1); 115 dataset.addValue(7.0, series2, category2); 116 dataset.addValue(6.0, series2, category3); 117 dataset.addValue(8.0, series2, category4); 118 dataset.addValue(4.0, series2, category5); 119 120 dataset.addValue(4.0, series3, category1); 121 dataset.addValue(3.0, series3, category2); 122 dataset.addValue(2.0, series3, category3); 123 dataset.addValue(3.0, series3, category4); 124 dataset.addValue(6.0, series3, category5); 125 126 return dataset; 127 128 } 129 130 137 private JFreeChart createChart(CategoryDataset dataset) { 138 139 JFreeChart chart = ChartFactory.createBarChart( 141 "Bar Chart Demo", "Category", "Value", dataset, PlotOrientation.VERTICAL, 146 true, true, false ); 150 151 153 chart.setBackgroundPaint(new Color (0xBBBBDD)); 155 156 CategoryPlot plot = chart.getCategoryPlot(); 158 159 NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); 161 rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 162 163 BarRenderer renderer = (BarRenderer) plot.getRenderer(); 165 renderer.setDrawBarOutline(false); 166 167 GradientPaint gp0 = new GradientPaint ( 169 0.0f, 0.0f, Color.blue, 170 0.0f, 0.0f, Color.lightGray 171 ); 172 GradientPaint gp1 = new GradientPaint ( 173 0.0f, 0.0f, Color.green, 174 0.0f, 0.0f, Color.lightGray 175 ); 176 GradientPaint gp2 = new GradientPaint ( 177 0.0f, 0.0f, Color.red, 178 0.0f, 0.0f, Color.lightGray 179 ); 180 renderer.setSeriesPaint(0, gp0); 181 renderer.setSeriesPaint(1, gp1); 182 renderer.setSeriesPaint(2, gp2); 183 184 186 return chart; 187 188 } 189 190 201 206 public static void main(String [] args) { 207 208 BarChartDemo demo = new BarChartDemo("Bar Chart Demo"); 209 demo.pack(); 210 RefineryUtilities.centerFrameOnScreen(demo); 211 demo.setVisible(true); 212 213 } 214 215 } 216 | Popular Tags |