1 20 package org.openi.chart; 21 22 import com.tonbeller.jpivot.olap.model.OlapException; 23 import com.tonbeller.jpivot.olap.model.OlapModel; 24 25 import org.apache.log4j.Logger; 26 27 import org.jfree.chart.ChartFactory; 28 import org.jfree.chart.ChartPanel; 29 import org.jfree.chart.JFreeChart; 30 import org.jfree.chart.Legend; 31 import org.jfree.chart.StandardLegend; 32 import org.jfree.chart.axis.CategoryAxis; 33 import org.jfree.chart.axis.CategoryLabelPositions; 34 import org.jfree.chart.axis.DateAxis; 35 import org.jfree.chart.axis.NumberAxis; 36 import org.jfree.chart.plot.CategoryPlot; 37 import org.jfree.chart.plot.CombinedDomainCategoryPlot; 38 import org.jfree.chart.plot.PlotOrientation; 39 import org.jfree.chart.plot.XYPlot; 40 import org.jfree.chart.renderer.category.BarRenderer; 41 import org.jfree.chart.renderer.category.CategoryItemRenderer; 42 import org.jfree.chart.renderer.category.LineAndShapeRenderer; 43 import org.jfree.chart.renderer.xy.StandardXYItemRenderer; 44 import org.jfree.chart.renderer.xy.XYItemRenderer; 45 46 import org.jfree.data.category.CategoryDataset; 47 import org.jfree.data.category.DefaultCategoryDataset; 48 import org.jfree.data.xy.XYDataset; 49 50 import org.jfree.ui.ApplicationFrame; 51 import org.jfree.ui.RefineryUtilities; 52 import org.jfree.ui.Spacer; 53 54 import org.jfree.util.TableOrder; 55 56 import org.openi.xmla.DatasetAdapter; 57 import org.openi.xmla.XmlaConnector; 58 59 import org.xml.sax.SAXException ; 60 61 import java.awt.Color ; 62 import java.awt.Dimension ; 63 import java.awt.Font ; 64 import java.awt.Paint ; 65 66 import java.io.IOException ; 67 68 import java.text.SimpleDateFormat ; 69 70 import java.util.ArrayList ; 71 import java.util.Iterator ; 72 import java.util.List ; 73 import java.util.Locale ; 74 75 import javax.swing.JPanel ; 76 77 78 83 public class ChartCreator extends ApplicationFrame { 84 private static Logger logger = Logger.getLogger(ChartCreator.class); 85 86 91 public ChartCreator(String title) { 92 super(title); 93 org.openi.test.Util.setupLog4j(); 94 95 try { 96 OlapModel olapModel = queryXmla(); 98 99 DatasetAdapter adapter = new DatasetAdapter(Locale.getDefault()); 101 XYDataset dataset = adapter.buildXYDataset(olapModel); 102 JFreeChart chart = createTimeChart(dataset); 103 ChartPanel chartPanel = new ChartPanel(chart); 104 chartPanel.setPreferredSize(new Dimension (500, 270)); 105 setContentPane(chartPanel); 106 } catch (Exception e) { 107 e.printStackTrace(); 108 } 109 } 110 111 115 private static JFreeChart createTimeChart(XYDataset dataset) { 116 JFreeChart chart = org.jfree.chart.ChartFactory.createTimeSeriesChart("Legal & General Unit Trust Prices", 117 "Date", "Price Per Unit", dataset, true, true, false); 118 119 chart.setBackgroundPaint(Color.white); 120 121 StandardLegend sl = (StandardLegend) chart.getLegend(); 122 sl.setDisplaySeriesShapes(true); 123 124 XYPlot plot = chart.getXYPlot(); 125 plot.setBackgroundPaint(Color.lightGray); 126 plot.setDomainGridlinePaint(Color.white); 127 plot.setRangeGridlinePaint(Color.white); 128 plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0)); 129 plot.setDomainCrosshairVisible(false); 130 plot.setRangeCrosshairVisible(false); 131 132 XYItemRenderer renderer = plot.getRenderer(); 133 134 if (renderer instanceof StandardXYItemRenderer) { 135 StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer; 136 rr.setPlotShapes(true); 137 rr.setShapesFilled(true); 138 rr.setItemLabelsVisible(true); 139 } 140 141 DateAxis axis = (DateAxis) plot.getDomainAxis(); 142 axis.setDateFormatOverride(new SimpleDateFormat ("MMM-yyyy")); 143 144 return chart; 145 } 146 147 private OlapModel queryXmla() 148 throws SAXException , OlapException, IOException { 149 XmlaConnector connector = new XmlaConnector(); 150 DatasetAdapter adapter = new DatasetAdapter(Locale.getDefault()); 151 String url = ""; 152 String catalog = ""; 153 String apple = ""; 154 String mdx = apple; 155 156 return connector.query(url, catalog, mdx); 157 } 158 159 private JFreeChart createChart(CategoryDataset dataset) { 160 logger.debug("looking for date: 2004-06-01 00:00:00"); 162 163 SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); 164 165 try { 166 logger.debug(formatter.parse("2004-06-01 00:00:00")); 167 } catch (Exception e) { 168 e.printStackTrace(); 169 } 170 171 return createBarChart(dataset); 172 } 173 174 private JFreeChart createMultipleChart(CategoryDataset dataset) { 175 CategoryDataset dataset1 = createDataset(); 176 NumberAxis rangeAxis1 = new NumberAxis("Value"); 177 rangeAxis1.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 178 179 LineAndShapeRenderer renderer1 = new LineAndShapeRenderer(); 180 181 CategoryPlot subplot1 = new CategoryPlot(dataset1, null, rangeAxis1, 183 renderer1); 184 subplot1.setDomainGridlinesVisible(true); 185 186 CategoryDataset dataset2 = createDataset(); 187 NumberAxis rangeAxis2 = new NumberAxis("Value"); 188 rangeAxis2.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 189 190 BarRenderer renderer2 = new BarRenderer(); 191 192 CategoryPlot subplot2 = new CategoryPlot(dataset2, null, rangeAxis2, 194 renderer2); 195 subplot2.setDomainGridlinesVisible(true); 196 197 CategoryAxis domainAxis = new CategoryAxis("Category"); 198 CombinedDomainCategoryPlot plot = new CombinedDomainCategoryPlot(domainAxis); 199 plot.add(subplot1, 2); 200 plot.add(subplot2, 1); 201 202 JFreeChart result = new JFreeChart("Combined Domain Category Plot Demo", 203 new Font ("SansSerif", Font.BOLD, 12), plot, true); 204 result.getLegend().setAnchor(Legend.SOUTH); 205 206 return result; 207 } 208 209 214 private static CategoryDataset createDataset() { 215 String series1 = "First"; 217 String series2 = "Second"; 218 String series3 = "Third"; 219 220 String category1 = "Category 1"; 222 String category2 = "Category 2"; 223 String category3 = "Category 3"; 224 String category4 = "Category 4"; 225 String category5 = "Category 5"; 226 227 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 229 230 dataset.addValue(1.0, series1, category1); 231 dataset.addValue(4.0, series1, category2); 232 dataset.addValue(3.0, series1, category3); 233 234 dataset.addValue(5.0, series2, category4); 235 dataset.addValue(5.0, series3, category5); 236 237 dataset.addValue(5.0, series3, category1); 238 dataset.addValue(7.0, series3, category2); 239 dataset.addValue(6.0, series3, category3); 240 241 dataset.addValue(8.0, "series4", category4); 242 dataset.addValue(4.0, "series4", category5); 243 244 dataset.addValue(4.0, "series5", category1); 245 dataset.addValue(3.0, "series5", category2); 246 dataset.addValue(2.0, "series5", category3); 247 248 dataset.addValue(3.0, "series6", category4); 249 dataset.addValue(6.0, "series6", category5); 250 251 return dataset; 252 } 253 254 261 private static JFreeChart createBarChart(CategoryDataset dataset) { 262 JFreeChart chart = ChartFactory.createBarChart(ChartCreator.class.getName(), "Category", "Value", dataset, PlotOrientation.VERTICAL, true, true, false ); 272 273 chart.setBackgroundPaint(Color.white); 276 277 CategoryPlot plot = chart.getCategoryPlot(); 279 plot.setBackgroundPaint(Color.lightGray); 280 plot.setDomainGridlinePaint(Color.white); 281 plot.setDomainGridlinesVisible(true); 282 plot.setRangeGridlinePaint(Color.white); 283 284 final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); 286 rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 287 288 BarRenderer renderer = (BarRenderer) plot.getRenderer(); 290 renderer.setDrawBarOutline(true); 291 292 List paintList = new ArrayList (); 294 295 303 304 paintList.add(new Color (249, 219, 224)); 307 paintList.add(new Color (167, 70, 97)); 308 paintList.add(new Color (189, 117, 137)); 309 paintList.add(new Color (212, 163, 177)); 310 paintList.add(new Color (233, 209, 215)); 311 paintList.add(new Color (142, 60, 82)); 312 paintList.add(new Color (125, 53, 73)); 313 paintList.add(new Color (84, 35, 49)); 314 315 setupSeriesPaint(paintList, renderer); 316 317 CategoryAxis domainAxis = plot.getDomainAxis(); 319 domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions( 320 Math.PI / 6.0)); 321 322 return chart; 324 } 325 326 private static void setupSeriesPaint(List colorPalette, 327 CategoryItemRenderer renderer) { 328 Iterator palette = colorPalette.iterator(); 329 330 for (int i = 0; palette.hasNext(); i++) { 331 Paint current = (Paint ) palette.next(); 332 renderer.setSeriesPaint(i, current); 333 } 334 } 335 336 341 public static JPanel createDemoPanel() { 342 JFreeChart chart = createBarChart(createDataset()); 344 345 return new ChartPanel(chart); 346 } 347 348 352 private static JFreeChart createPieChart(CategoryDataset dataset) { 353 JFreeChart chart = ChartFactory.createMultiplePieChart(ChartCreator.class.getName(), 354 createDataset(), TableOrder.BY_COLUMN, true, true, true); 355 chart.setBackgroundPaint(Color.white); 356 357 return chart; 358 } 359 360 365 public static String getDemoDescription() { 366 return "A bar chart."; 367 } 368 369 374 public static void main(String [] args) { 375 ChartCreator demo = new ChartCreator("Bar Chart Demo"); 376 demo.pack(); 377 RefineryUtilities.centerFrameOnScreen(demo); 378 demo.setVisible(true); 379 } 380 } 381 | Popular Tags |