1 38 39 package org.jfree.chart.demo; 40 41 import java.awt.Color ; 42 import java.text.DecimalFormat ; 43 44 import org.jfree.chart.ChartFactory; 45 import org.jfree.chart.ChartPanel; 46 import org.jfree.chart.JFreeChart; 47 import org.jfree.chart.Spacer; 48 import org.jfree.chart.axis.NumberTickUnit; 49 import org.jfree.chart.axis.TickUnits; 50 import org.jfree.chart.axis.ValueAxis; 51 import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; 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 63 public class WaterfallChartDemo extends ApplicationFrame { 64 65 70 public WaterfallChartDemo(String title) { 71 72 super(title); 73 74 CategoryDataset dataset = createDataset(); 75 JFreeChart chart = createChart(dataset); 76 ChartPanel chartPanel = new ChartPanel(chart); 77 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 78 chartPanel.setEnforceFileExtensions(false); 79 setContentPane(chartPanel); 80 81 } 82 83 88 private CategoryDataset createDataset() { 89 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 90 dataset.addValue(15.76, "Product 1", "Labour"); 91 dataset.addValue(8.66, "Product 1", "Administration"); 92 dataset.addValue(4.71, "Product 1", "Marketing"); 93 dataset.addValue(3.51, "Product 1", "Distribution"); 94 dataset.addValue(32.64, "Product 1", "Total Expense"); 95 return dataset; 96 } 97 98 109 116 private JFreeChart createChart(CategoryDataset dataset) { 117 118 JFreeChart chart = ChartFactory.createWaterfallChart( 119 "Product Cost Breakdown", 120 "Expense Category", 121 "Cost Per Unit", 122 dataset, 123 PlotOrientation.VERTICAL, 124 true, 125 true, 126 false 127 ); 128 chart.setBackgroundPaint(Color.white); 129 130 CategoryPlot plot = chart.getCategoryPlot(); 131 plot.setBackgroundPaint(Color.lightGray); 132 plot.setRangeGridlinePaint(Color.white); 133 plot.setRangeGridlinesVisible(true); 134 plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0)); 135 136 ValueAxis rangeAxis = plot.getRangeAxis(); 137 138 DecimalFormat formatter = new DecimalFormat ("##,###"); 140 formatter.setNegativePrefix("("); 141 formatter.setNegativeSuffix(")"); 142 TickUnits standardUnits = new TickUnits(); 143 standardUnits.add(new NumberTickUnit(5, formatter)); 144 standardUnits.add(new NumberTickUnit(10, formatter)); 145 standardUnits.add(new NumberTickUnit(20, formatter)); 146 standardUnits.add(new NumberTickUnit(50, formatter)); 147 standardUnits.add(new NumberTickUnit(100, formatter)); 148 standardUnits.add(new NumberTickUnit(200, formatter)); 149 standardUnits.add(new NumberTickUnit(500, formatter)); 150 standardUnits.add(new NumberTickUnit(1000, formatter)); 151 standardUnits.add(new NumberTickUnit(2000, formatter)); 152 standardUnits.add(new NumberTickUnit(5000, formatter)); 153 rangeAxis.setStandardTickUnits(standardUnits); 154 155 BarRenderer renderer = (BarRenderer) plot.getRenderer(); 156 renderer.setDrawBarOutline(false); 157 158 DecimalFormat labelFormatter = new DecimalFormat ("$##,###.00"); 159 labelFormatter.setNegativePrefix("("); 160 labelFormatter.setNegativeSuffix(")"); 161 renderer.setItemLabelGenerator( 162 new StandardCategoryItemLabelGenerator(labelFormatter, false) 163 ); 164 renderer.setItemLabelsVisible(true); 165 166 return chart; 167 } 168 169 174 public static void main(String [] args) { 175 WaterfallChartDemo demo = new WaterfallChartDemo("Waterfall Chart Demo"); 176 demo.pack(); 177 RefineryUtilities.centerFrameOnScreen(demo); 178 demo.setVisible(true); 179 } 180 181 } 182 | Popular Tags |