1 package org.openi.chart; 2 3 import com.tonbeller.jpivot.olap.model.OlapException; 4 import com.tonbeller.jpivot.olap.model.OlapModel; 5 6 import junit.framework.TestCase; 7 8 import org.apache.log4j.Logger; 9 10 import org.jfree.chart.ChartUtilities; 11 import org.jfree.chart.JFreeChart; 12 import org.jfree.chart.plot.Plot; 13 14 import org.jfree.data.category.DefaultCategoryDataset; 15 16 import org.openi.analysis.Analysis; 17 18 import org.openi.test.NullOutputStream; 19 import org.openi.test.Util; 20 21 import org.openi.xml.BeanStorage; 22 23 import org.openi.xmla.DatasetAdapter; 24 import org.openi.xmla.XmlaConnector; 25 26 import org.xml.sax.SAXException ; 27 28 import java.io.FileNotFoundException ; 29 import java.io.FileOutputStream ; 30 import java.io.IOException ; 31 import java.io.OutputStream ; 32 import java.util.Locale ; 33 34 35 public class ChartTester extends TestCase { 36 private static Logger logger = Logger.getLogger(ChartTester.class); 37 private Analysis analysis; 38 39 private String xmlaUri = "http://localhost:8181/xmla/msxisapi.dll"; 40 private String catalog = "Foodmart 2000"; 41 42 protected void setUp() throws Exception { 43 super.setUp(); 44 Util.setupLog4j(); 45 this.analysis = restoreAnalysis("Sales_by_Store_and_Time.analysis"); 46 } 47 48 protected void tearDown() throws Exception { 49 super.tearDown(); 50 } 51 52 private OlapModel getDataset(String xmlaUri, String catalog, 53 Analysis analysis) throws IOException , OlapException, SAXException { 54 XmlaConnector connector = new XmlaConnector(); 55 String mdxQuery = analysis.getMdxQuery(); 56 57 OlapModel model = connector.query(xmlaUri, catalog, mdxQuery); 58 59 return model; 61 } 62 63 private Analysis restoreAnalysis(String analysisName) 64 throws IOException { 65 return (Analysis) new BeanStorage().restoreBeanFromFile(Util.findTestDirectory() + 66 "/projects/standard/public/" + analysisName); 67 } 68 69 public void testCreateThumbnail() throws Exception { 70 analysis.setChartType(1); 72 analysis.setBgColorR(255); 73 analysis.setBgColorG(255); 74 analysis.setBgColorB(255); 75 76 OlapModel olapModel = getDataset(xmlaUri, catalog, analysis); 77 OutputStream out = new FileOutputStream (Util.findTestDirectory() + 78 "/test.jpg"); 79 try{ 82 EnhancedChartFactory.createChart(out, analysis, olapModel, 950, 600, Locale.getDefault()); 83 }catch(Throwable e){ 84 logger.error(e); 85 } 86 } 88 89 90 public void AllChartTypes() throws Exception { 91 long start = System.currentTimeMillis(); 92 93 OlapModel olapModel = getDataset(xmlaUri, catalog, analysis); 95 96 for(int i=1; i < 19; i++){ 99 NullOutputStream out = new NullOutputStream(); 102 analysis.setChartType(i); 103 try{ 104 EnhancedChartFactory.createChart(out, analysis, olapModel, 320, 240, Locale.getDefault()); 105 }catch(Throwable e){ 106 logger.error(e); 107 } 108 assertTrue(out.length()>0); 109 } 110 111 long elapsed = System.currentTimeMillis() - start; 112 logger.debug("test complete in: " + elapsed + " ms"); 113 } 114 115 118 public void TimeSeriesChartUsingYearQuarter() throws Exception { 119 XmlaConnector connector = new XmlaConnector(); 120 String mdxQuery = analysis.getMdxQuery(); 121 122 OlapModel model = connector.query(xmlaUri, catalog, mdxQuery); 123 124 OutputStream out = new FileOutputStream (Util.findTestDirectory() + 125 "/time-series-quarter.jpg"); 126 JFreeChart chart = EnhancedChartFactory.createTimeChart(analysis.getAnalysisTitle(), 127 analysis.getHorizAxisLabel(), analysis.getVertAxisLabel(), 128 new DatasetAdapter(Locale.getDefault()).buildXYDataset(model)); 129 ChartUtilities.writeChartAsJPEG(out, 1.0f, chart, 640, 480); 130 out.flush(); 131 out.close(); 132 } 133 134 137 public void TimeSeriesChartYYYYMMM() throws Exception { 138 XmlaConnector connector = new XmlaConnector(); 139 String mdxQuery = analysis.getMdxQuery(); 140 141 OlapModel model = connector.query("http://olap02.intelligenceportal.colo/xmla/msxisapi.dll", 142 "Apple Weekly", mdxQuery); 143 144 OutputStream out = new FileOutputStream (Util.findTestDirectory() + 145 "/time-series-YYYY-MMM.jpg"); 146 JFreeChart chart = EnhancedChartFactory.createTimeChart(analysis.getAnalysisTitle(), 147 analysis.getHorizAxisLabel(), analysis.getVertAxisLabel(), 148 new DatasetAdapter(Locale.getDefault()).buildXYDataset(model)); 149 ChartUtilities.writeChartAsJPEG(out, 1.0f, chart, 640, 480); 150 out.flush(); 151 out.close(); 152 } 153 154 160 public void TimeSeriesChartYYYYMMMdd() throws IOException , OlapException, SAXException { 161 XmlaConnector connector = new XmlaConnector(); 162 String mdxQuery = analysis.getMdxQuery(); 163 164 OlapModel model = connector.query("http://olap02.intelligenceportal.colo/xmla/msxisapi.dll", 165 "Apple Weekly", mdxQuery); 166 167 OutputStream out = new FileOutputStream (Util.findTestDirectory() + 168 "/time-series-YYYY-MMM-dd.jpg"); 169 try{ 170 JFreeChart chart = EnhancedChartFactory.createTimeChart(analysis.getAnalysisTitle(), 171 analysis.getHorizAxisLabel(), analysis.getVertAxisLabel(), 172 new DatasetAdapter(Locale.getDefault()).buildXYDataset(model)); 173 ChartUtilities.writeChartAsJPEG(out, 1.0f, chart, 640, 480); 174 }finally{ 175 out.flush(); 176 out.close(); 177 } 178 } 179 } 180 181 | Popular Tags |