KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openi > chart > ChartTester


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 JavaDoc;
27
28 import java.io.FileNotFoundException JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.OutputStream JavaDoc;
32 import java.util.Locale JavaDoc;
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 JavaDoc xmlaUri = "http://localhost:8181/xmla/msxisapi.dll";
40     private String JavaDoc catalog = "Foodmart 2000";
41
42     protected void setUp() throws Exception JavaDoc {
43         super.setUp();
44         Util.setupLog4j();
45         this.analysis = restoreAnalysis("Sales_by_Store_and_Time.analysis");
46     }
47
48     protected void tearDown() throws Exception JavaDoc {
49         super.tearDown();
50     }
51
52     private OlapModel getDataset(String JavaDoc xmlaUri, String JavaDoc catalog,
53         Analysis analysis) throws IOException JavaDoc, OlapException, SAXException JavaDoc {
54         XmlaConnector connector = new XmlaConnector();
55         String JavaDoc mdxQuery = analysis.getMdxQuery();
56
57         OlapModel model = connector.query(xmlaUri, catalog, mdxQuery);
58
59         // return new DatasetAdapter().buildCategoryDataset(model);
60
return model;
61     }
62
63     private Analysis restoreAnalysis(String JavaDoc analysisName)
64         throws IOException JavaDoc {
65         return (Analysis) new BeanStorage().restoreBeanFromFile(Util.findTestDirectory() +
66             "/projects/standard/public/" + analysisName);
67     }
68     
69     public void testCreateThumbnail() throws Exception JavaDoc {
70         // DefaultCategoryDataset dataset = getDataset(xmlaUri, catalog, analysis);
71
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 JavaDoc(Util.findTestDirectory() +
78         "/test.jpg");
79     // NullOutputStream out = new NullOutputStream();
80
// analysis.setChartType(19);
81
try{
82             EnhancedChartFactory.createChart(out, analysis, olapModel, 950, 600, Locale.getDefault());
83         }catch(Throwable JavaDoc e){
84             logger.error(e);
85         }
86         // assertTrue(out.length()>0);
87
}
88
89
90     public void AllChartTypes() throws Exception JavaDoc {
91         long start = System.currentTimeMillis();
92
93         // DefaultCategoryDataset dataset = getDataset(xmlaUri, catalog, analysis);
94
OlapModel olapModel = getDataset(xmlaUri, catalog, analysis);
95
96         // NullOutputStream out = new NullOutputStream();
97
// try 1 - 18
98
for(int i=1; i < 19; i++){
99             // OutputStream out = new FileOutputStream(Util.findTestDirectory() +
100
// "/test" + i + ".jpg");
101
NullOutputStream out = new NullOutputStream();
102             analysis.setChartType(i);
103             try{
104                 EnhancedChartFactory.createChart(out, analysis, olapModel, 320, 240, Locale.getDefault());
105             }catch(Throwable JavaDoc 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     /**
116      * [2006].[Q1]
117      */

118     public void TimeSeriesChartUsingYearQuarter() throws Exception JavaDoc{
119         XmlaConnector connector = new XmlaConnector();
120         String JavaDoc mdxQuery = analysis.getMdxQuery();
121         
122         OlapModel model = connector.query(xmlaUri, catalog, mdxQuery);
123         
124         OutputStream out = new FileOutputStream JavaDoc(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     /**
135      * [2006].[January]
136      */

137     public void TimeSeriesChartYYYYMMM() throws Exception JavaDoc{
138         XmlaConnector connector = new XmlaConnector();
139         String JavaDoc 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 JavaDoc(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     /**
155      * [2006].[January].[1]
156      * @throws SAXException
157      * @throws OlapException
158      * @throws IOException
159      */

160     public void TimeSeriesChartYYYYMMMdd() throws IOException JavaDoc, OlapException, SAXException JavaDoc {
161         XmlaConnector connector = new XmlaConnector();
162         String JavaDoc 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 JavaDoc(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