1 43 44 package org.jfree.chart.demo; 45 46 import java.awt.Color ; 47 import java.awt.Dimension ; 48 import java.awt.GradientPaint ; 49 50 import org.jfree.chart.ChartFactory; 51 import org.jfree.chart.ChartPanel; 52 import org.jfree.chart.JFreeChart; 53 import org.jfree.chart.axis.CategoryAxis; 54 import org.jfree.chart.axis.CategoryLabelPositions; 55 import org.jfree.chart.axis.NumberAxis; 56 import org.jfree.chart.plot.CategoryPlot; 57 import org.jfree.chart.plot.PlotOrientation; 58 import org.jfree.chart.renderer.category.BarRenderer; 59 import org.jfree.data.category.CategoryDataset; 60 import org.jfree.data.category.DefaultCategoryDataset; 61 import org.jfree.ui.ApplicationFrame; 62 import org.jfree.ui.RefineryUtilities; 63 64 67 public class BarChartDemo1 extends ApplicationFrame { 68 69 74 public BarChartDemo1(String title) { 75 super(title); 76 CategoryDataset dataset = createDataset(); 77 JFreeChart chart = createChart(dataset); 78 ChartPanel chartPanel = new ChartPanel(chart, false); 79 chartPanel.setPreferredSize(new Dimension (500, 270)); 80 setContentPane(chartPanel); 81 } 82 83 88 private static CategoryDataset createDataset() { 89 90 String series1 = "First"; 92 String series2 = "Second"; 93 String series3 = "Third"; 94 95 String category1 = "Category 1"; 97 String category2 = "Category 2"; 98 String category3 = "Category 3"; 99 String category4 = "Category 4"; 100 String category5 = "Category 5"; 101 102 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 104 105 dataset.addValue(1.0, series1, category1); 106 dataset.addValue(4.0, series1, category2); 107 dataset.addValue(3.0, series1, category3); 108 dataset.addValue(5.0, series1, category4); 109 dataset.addValue(5.0, series1, category5); 110 111 dataset.addValue(5.0, series2, category1); 112 dataset.addValue(7.0, series2, category2); 113 dataset.addValue(6.0, series2, category3); 114 dataset.addValue(8.0, series2, category4); 115 dataset.addValue(4.0, series2, category5); 116 117 dataset.addValue(4.0, series3, category1); 118 dataset.addValue(3.0, series3, category2); 119 dataset.addValue(2.0, series3, category3); 120 dataset.addValue(3.0, series3, category4); 121 dataset.addValue(6.0, series3, category5); 122 123 return dataset; 124 125 } 126 127 134 private static JFreeChart createChart(CategoryDataset dataset) { 135 136 JFreeChart chart = ChartFactory.createBarChart( 138 "Bar Chart Demo 1", "Category", "Value", dataset, PlotOrientation.VERTICAL, true, true, false ); 147 148 150 chart.setBackgroundPaint(Color.white); 152 153 CategoryPlot plot = chart.getCategoryPlot(); 155 plot.setBackgroundPaint(Color.lightGray); 156 plot.setDomainGridlinePaint(Color.white); 157 plot.setDomainGridlinesVisible(true); 158 plot.setRangeGridlinePaint(Color.white); 159 160 168 final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); 170 rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 171 172 BarRenderer renderer = (BarRenderer) plot.getRenderer(); 174 renderer.setDrawBarOutline(false); 175 176 GradientPaint gp0 = new GradientPaint (0.0f, 0.0f, Color.blue, 178 0.0f, 0.0f, new Color (0, 0, 64)); 179 GradientPaint gp1 = new GradientPaint (0.0f, 0.0f, Color.green, 180 0.0f, 0.0f, new Color (0, 64, 0)); 181 GradientPaint gp2 = new GradientPaint (0.0f, 0.0f, Color.red, 182 0.0f, 0.0f, new Color (64, 0, 0)); 183 renderer.setSeriesPaint(0, gp0); 184 renderer.setSeriesPaint(1, gp1); 185 renderer.setSeriesPaint(2, gp2); 186 187 CategoryAxis domainAxis = plot.getDomainAxis(); 188 domainAxis.setCategoryLabelPositions( 189 CategoryLabelPositions.createUpRotationLabelPositions( 190 Math.PI / 6.0)); 191 193 return chart; 194 195 } 196 197 202 public static void main(String [] args) { 203 BarChartDemo1 demo = new BarChartDemo1("Bar Chart Demo 1"); 204 demo.pack(); 205 RefineryUtilities.centerFrameOnScreen(demo); 206 demo.setVisible(true); 207 } 208 209 } 210 | Popular Tags |