1 39 40 package org.jfree.chart.demo; 41 42 import java.awt.Color ; 43 import java.awt.Paint ; 44 45 import org.jfree.chart.ChartFactory; 46 import org.jfree.chart.ChartPanel; 47 import org.jfree.chart.JFreeChart; 48 import org.jfree.chart.axis.NumberAxis; 49 import org.jfree.chart.axis.ValueAxis; 50 import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; 51 import org.jfree.chart.plot.CategoryPlot; 52 import org.jfree.chart.plot.PlotOrientation; 53 import org.jfree.chart.renderer.BarRenderer; 54 import org.jfree.chart.renderer.CategoryItemRenderer; 55 import org.jfree.chart.renderer.ItemLabelAnchor; 56 import org.jfree.chart.renderer.ItemLabelPosition; 57 import org.jfree.data.CategoryDataset; 58 import org.jfree.data.DatasetUtilities; 59 import org.jfree.ui.ApplicationFrame; 60 import org.jfree.ui.RefineryUtilities; 61 import org.jfree.ui.TextAnchor; 62 63 69 public class BarChartDemo3 extends ApplicationFrame { 70 71 74 class CustomRenderer extends BarRenderer { 75 76 77 private Paint [] colors; 78 79 84 public CustomRenderer(Paint [] colors) { 85 this.colors = colors; 86 } 87 88 97 public Paint getItemPaint(int row, int column) { 98 return colors[column % colors.length]; 99 } 100 } 101 102 107 public BarChartDemo3(String title) { 108 109 super(title); 110 111 double[][] data = new double[][] {{4.0, 3.0, -2.0, 3.0, 6.0, 3.0, 4.0, 3.0}}; 113 114 CategoryDataset dataset = DatasetUtilities.createCategoryDataset( 115 "Series ", 116 "Category ", 117 data 118 ); 119 120 JFreeChart chart = ChartFactory.createBarChart( 122 "Bar Chart Demo 3", "Category", "Value", dataset, PlotOrientation.VERTICAL, false, true, 129 false 130 ); 131 132 134 chart.setBackgroundPaint(Color.lightGray); 136 137 CategoryPlot plot = chart.getCategoryPlot(); 139 plot.setNoDataMessage("NO DATA!"); 140 142 CategoryItemRenderer renderer = new CustomRenderer( 143 new Paint [] {Color.red, Color.blue, Color.green, 144 Color.yellow, Color.orange, Color.cyan, 145 Color.magenta, Color.blue}); 146 renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator()); 147 renderer.setItemLabelsVisible(true); 148 ItemLabelPosition p = new ItemLabelPosition( 149 ItemLabelAnchor.CENTER, TextAnchor.CENTER, TextAnchor.CENTER, 45.0); 150 renderer.setPositiveItemLabelPosition(p); 151 plot.setRenderer(renderer); 152 153 154 ValueAxis rangeAxis = plot.getRangeAxis(); 156 rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 157 rangeAxis.setLowerMargin(0.15); 158 rangeAxis.setUpperMargin(0.15); 159 160 162 ChartPanel chartPanel = new ChartPanel(chart); 164 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 165 setContentPane(chartPanel); 166 167 } 168 169 180 185 public static void main(String [] args) { 186 187 BarChartDemo3 demo = new BarChartDemo3("Bar Chart Demo 3"); 188 demo.pack(); 189 RefineryUtilities.centerFrameOnScreen(demo); 190 demo.setVisible(true); 191 192 } 193 194 } 195 | Popular Tags |