1 37 38 package org.jfree.chart.demo; 39 40 import java.awt.Color ; 41 import java.awt.GradientPaint ; 42 43 import org.jfree.chart.ChartFactory; 44 import org.jfree.chart.ChartPanel; 45 import org.jfree.chart.JFreeChart; 46 import org.jfree.chart.axis.NumberAxis; 47 import org.jfree.chart.plot.CategoryPlot; 48 import org.jfree.chart.plot.PlotOrientation; 49 import org.jfree.chart.renderer.BarRenderer; 50 import org.jfree.data.CategoryDataset; 51 import org.jfree.data.DefaultCategoryDataset; 52 import org.jfree.ui.ApplicationFrame; 53 import org.jfree.ui.RefineryUtilities; 54 55 61 public class BarChartDemo4 extends ApplicationFrame { 62 63 68 public BarChartDemo4(String title) { 69 70 super(title); 71 72 CategoryDataset dataset = createDataset(); 73 JFreeChart chart = createChart(dataset); 74 75 ChartPanel chartPanel = new ChartPanel(chart); 77 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 78 setContentPane(chartPanel); 79 80 } 81 82 87 private CategoryDataset createDataset() { 88 89 String series1 = "First"; 91 String series2 = "Second"; 92 93 String category1 = "Category 1"; 95 96 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 98 dataset.addValue(1.0, series1, category1); 99 dataset.addValue(5.0, series2, category1); 100 101 return dataset; 102 103 } 104 105 116 123 private JFreeChart createChart(CategoryDataset dataset) { 124 125 JFreeChart chart = ChartFactory.createBarChart( 127 "Bar Chart Demo", "Category", "Value", dataset, PlotOrientation.VERTICAL, 132 true, true, false ); 136 137 139 chart.setBackgroundPaint(new Color (0xBBBBDD)); 141 142 CategoryPlot plot = chart.getCategoryPlot(); 144 145 NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); 147 rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 148 149 BarRenderer renderer = (BarRenderer) plot.getRenderer(); 151 renderer.setDrawBarOutline(false); 152 renderer.setMaxBarWidth(0.10); 153 154 GradientPaint gp0 = new GradientPaint ( 156 0.0f, 0.0f, Color.blue, 157 0.0f, 0.0f, Color.lightGray 158 ); 159 GradientPaint gp1 = new GradientPaint ( 160 0.0f, 0.0f, Color.green, 161 0.0f, 0.0f, Color.lightGray 162 ); 163 renderer.setSeriesPaint(0, gp0); 164 renderer.setSeriesPaint(1, gp1); 165 166 168 return chart; 169 170 } 171 172 177 public static void main(String [] args) { 178 179 BarChartDemo4 demo = new BarChartDemo4("Bar Chart Demo 4"); 180 demo.pack(); 181 RefineryUtilities.centerFrameOnScreen(demo); 182 demo.setVisible(true); 183 184 } 185 186 } 187 | Popular Tags |