1 37 38 package org.jfree.chart.demo; 39 40 import java.awt.Color ; 41 import java.text.DecimalFormat ; 42 43 import org.jfree.chart.ChartPanel; 44 import org.jfree.chart.JFreeChart; 45 import org.jfree.chart.Marker; 46 import org.jfree.chart.axis.CategoryAxis; 47 import org.jfree.chart.axis.NumberAxis; 48 import org.jfree.chart.axis.NumberTickUnit; 49 import org.jfree.chart.axis.TickUnits; 50 import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; 51 import org.jfree.chart.plot.CategoryPlot; 52 import org.jfree.chart.renderer.WaterfallBarRenderer; 53 import org.jfree.data.CategoryDataset; 54 import org.jfree.data.DefaultCategoryDataset; 55 import org.jfree.ui.ApplicationFrame; 56 import org.jfree.ui.Layer; 57 import org.jfree.ui.RefineryUtilities; 58 59 62 public class WaterfallChartDemo2 extends ApplicationFrame { 63 64 69 public WaterfallChartDemo2(String title) { 70 71 super(title); 72 73 CategoryDataset dataset = createDataset(); 74 JFreeChart chart = createChart(dataset); 75 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 87 private CategoryDataset createDataset() { 88 89 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 90 dataset.addValue(-890.76, "S1", "PY OM"); 91 dataset.addValue(-3021.51, "S1", "Vol CM"); 92 dataset.addValue(-218.32, "S1", "Price"); 93 dataset.addValue(221.75, "S1", "Mat Inf"); 94 dataset.addValue(-1504.1, "S1", "Oth Inf"); 95 dataset.addValue(7103.8, "S1", "VCP"); 96 dataset.addValue(3503.3, "S1", "CM FX"); 97 dataset.addValue(-6561.97, "S1", "Base"); 98 dataset.addValue(-824.25, "S1", "OM"); 99 100 return dataset; 101 102 } 103 104 111 private JFreeChart createChart(CategoryDataset dataset) { 112 113 CategoryAxis xAxis = new CategoryAxis("Category"); 114 NumberAxis yAxis = new NumberAxis("$ in Thousands"); 115 yAxis.setLowerMargin(0.10); 116 yAxis.setUpperMargin(0.10); 117 118 DecimalFormat formatter = new DecimalFormat ("##,###"); 120 formatter.setNegativePrefix("("); 121 formatter.setNegativeSuffix(")"); 122 TickUnits standardUnits = new TickUnits(); 123 standardUnits.add(new NumberTickUnit(200, formatter)); 124 standardUnits.add(new NumberTickUnit(500, formatter)); 125 standardUnits.add(new NumberTickUnit(1000, formatter)); 126 standardUnits.add(new NumberTickUnit(2000, formatter)); 127 standardUnits.add(new NumberTickUnit(5000, formatter)); 128 129 yAxis.setStandardTickUnits(standardUnits); 130 131 142 DecimalFormat labelFormatter = new DecimalFormat ("##,###"); 143 labelFormatter.setNegativePrefix("("); 144 labelFormatter.setNegativeSuffix(")"); 145 146 WaterfallBarRenderer renderer = new WaterfallBarRenderer(); 147 renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator(labelFormatter)); 148 renderer.setItemLabelsVisible(Boolean.TRUE); 149 150 CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer); 151 plot.setBackgroundPaint(Color.lightGray); 152 plot.setRangeGridlinePaint(Color.white); 153 plot.setRangeGridlinesVisible(true); 154 Marker baseline = new Marker(0.0, 155 Color.blue, 156 new java.awt.BasicStroke (1.1f), 157 Color.blue, 158 1.0f); 159 plot.addRangeMarker(baseline, Layer.FOREGROUND); 160 161 JFreeChart chart = new JFreeChart("OM WaterFall Chart", 162 JFreeChart.DEFAULT_TITLE_FONT, plot, false); 163 chart.setBackgroundPaint(Color.white); 164 return chart; 165 } 166 167 172 public static void main(String [] args) { 173 WaterfallChartDemo2 demo = new WaterfallChartDemo2("Waterfall Chart Demo 2"); 174 demo.pack(); 175 RefineryUtilities.centerFrameOnScreen(demo); 176 demo.setVisible(true); 177 } 178 } 179 | Popular Tags |