~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to local/in/dhis-web-ga/WEB-INF/src/org/hisp/gtool/charts/DualAxisChart1.java

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hisp.gtool.charts;
 
2
 
 
3
import java.awt.Font;
 
4
import java.awt.image.BufferedImage;
 
5
import java.io.PrintWriter;
 
6
 
 
7
import javax.servlet.http.HttpServletRequest;
 
8
import javax.servlet.http.HttpServletResponse;
 
9
import javax.servlet.http.HttpSession;
 
10
 
 
11
import org.jfree.chart.ChartFactory;
 
12
import org.jfree.chart.ChartRenderingInfo;
 
13
import org.jfree.chart.ChartUtilities;
 
14
import org.jfree.chart.JFreeChart;
 
15
import org.jfree.chart.axis.AxisLocation;
 
16
import org.jfree.chart.axis.CategoryAxis;
 
17
import org.jfree.chart.axis.CategoryLabelPositions;
 
18
import org.jfree.chart.axis.NumberAxis;
 
19
import org.jfree.chart.entity.StandardEntityCollection;
 
20
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
 
21
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
 
22
import org.jfree.chart.plot.CategoryPlot;
 
23
import org.jfree.chart.plot.DatasetRenderingOrder;
 
24
import org.jfree.chart.plot.PlotOrientation;
 
25
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
 
26
import org.jfree.chart.title.TextTitle;
 
27
import org.jfree.data.category.CategoryDataset;
 
28
import org.jfree.data.category.DefaultCategoryDataset;
 
29
import org.jfree.ui.RectangleEdge;
 
30
import org.jfree.ui.VerticalAlignment;
 
31
 
 
32
public class DualAxisChart1 {
 
33
 
 
34
//       Data Related Bar Chart
 
35
        //      Row Keys
 
36
        String[] series1;
 
37
        // Column Keys
 
38
        String[] categories1;
 
39
        // data...      
 
40
        double[][] data1;
 
41
        
 
42
        
 
43
        String chartTitle;
 
44
        String xAxis_Title;
 
45
        String yAxis_Title;
 
46
        
 
47
        // Data Related Line Chart
 
48
        
 
49
        String[] series2;
 
50
        // Column Keys
 
51
        String[] categories2;
 
52
 
 
53
        // data...      
 
54
        double[][] data2;
 
55
        
 
56
        //      constructor
 
57
        public DualAxisChart1() { }
 
58
 
 
59
        // Function For Creating Dataset for Barchart
 
60
        private DefaultCategoryDataset  getDataset1() 
 
61
        {    
 
62
                
 
63
                //      create the dataset...
 
64
                DefaultCategoryDataset dataset = new DefaultCategoryDataset();
 
65
                                        
 
66
                for(int i=0;i<series1.length;i++)
 
67
                {
 
68
                        for(int j=0;j<categories1.length;j++)
 
69
                        {
 
70
                                dataset.addValue(data1[i][j], series1[i], categories1[j]);
 
71
                        }
 
72
                }
 
73
                
 
74
                return dataset;
 
75
        }
 
76
        
 
77
        // Function For Creating Dataset for Linechart
 
78
        private DefaultCategoryDataset  getDataset2() 
 
79
        {    
 
80
                
 
81
                //      create the dataset...
 
82
                DefaultCategoryDataset dataset = new DefaultCategoryDataset();
 
83
                                        
 
84
                for(int i=0;i<series2.length;i++)
 
85
                {
 
86
                        for(int j=0;j<categories2.length;j++)
 
87
                        {
 
88
                                dataset.addValue(data2[i][j], series2[i], categories2[j]);
 
89
                        }
 
90
                }
 
91
                
 
92
                return dataset;
 
93
        }
 
94
 public String getChartViewer(HttpServletRequest request, HttpServletResponse response) {
 
95
     final CategoryDataset dataset1 = getDataset1();
 
96
 
 
97
     // create the chart...
 
98
     final JFreeChart chart = ChartFactory.createBarChart(
 
99
         "",        // chart title
 
100
         "Category",               // domain axis label
 
101
         "Value",                  // range axis label
 
102
         dataset1,                 // data
 
103
         PlotOrientation.VERTICAL,
 
104
         true,                     // include legend
 
105
         true,                     // tooltips?
 
106
         false                     // URL generator?  Not required...
 
107
     );
 
108
 
 
109
     // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
 
110
 //    chart.setBackgroundPaint(Color.white);
 
111
//     chart.getLegend().setAnchor(Legend.SOUTH);
 
112
 
 
113
     /* TITLE */
 
114
     final TextTitle mainTitle = new TextTitle("Dual Axis Chart");
 
115
     mainTitle.setFont(new Font("times", Font.BOLD, 13));
 
116
     mainTitle.setPosition(RectangleEdge.TOP);     
 
117
     mainTitle.setVerticalAlignment(VerticalAlignment.BOTTOM);
 
118
     chart.addSubtitle(mainTitle);
 
119
     
 
120
     /* SUB TITLE */     
 
121
     final TextTitle subtitle = new TextTitle(chartTitle);
 
122
     subtitle.setFont(new Font("times", Font.BOLD, 13));
 
123
     subtitle.setPosition(RectangleEdge.TOP);     
 
124
     subtitle.setVerticalAlignment(VerticalAlignment.BOTTOM);
 
125
     chart.addSubtitle(subtitle);
 
126
     
 
127
     // get a reference to the plot for further customisation...
 
128
     final CategoryPlot plot = chart.getCategoryPlot();
 
129
 //    plot.setBackgroundPaint(new Color(0xEE, 0xEE, 0xFF));
 
130
     plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
 
131
 
 
132
     // customise the range axis...
 
133
     final NumberAxis rangeAxis1 = (NumberAxis) plot.getRangeAxis();
 
134
     rangeAxis1.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
 
135
     rangeAxis1.setAutoRangeIncludesZero(true);
 
136
     rangeAxis1.setUpperMargin(0.15);
 
137
     rangeAxis1.setLowerMargin(0.15);
 
138
     rangeAxis1.setLabel(yAxis_Title);
 
139
     
 
140
     final CategoryDataset dataset2 = getDataset2();
 
141
     plot.setDataset(1, dataset2);
 
142
     plot.mapDatasetToRangeAxis(1, 1);
 
143
     // customise the range axis...
 
144
     
 
145
     
 
146
     
 
147
     final CategoryAxis domainAxis = plot.getDomainAxis();
 
148
     domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
 
149
     domainAxis.setLabel(xAxis_Title);
 
150
     
 
151
     //final ValueAxis axis2 = new NumberAxis("Secondary");
 
152
     
 
153
     
 
154
     final NumberAxis rangeAxis2 = (NumberAxis) plot.getRangeAxis();
 
155
     rangeAxis2.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
 
156
     rangeAxis2.setAutoRangeIncludesZero(true);
 
157
     rangeAxis2.setUpperMargin(0.30);
 
158
     rangeAxis2.setLowerMargin(0.15);
 
159
     rangeAxis2.setLabel(yAxis_Title);
 
160
     plot.setRangeAxis(1, rangeAxis2);
 
161
     
 
162
     final LineAndShapeRenderer renderer2 = new LineAndShapeRenderer();
 
163
     renderer2.setToolTipGenerator(new StandardCategoryToolTipGenerator());
 
164
     renderer2.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());    
 
165
     renderer2.setItemLabelsVisible(true);
 
166
     
 
167
     plot.setRenderer(1, renderer2);
 
168
     plot.setDatasetRenderingOrder(DatasetRenderingOrder.REVERSE);
 
169
     
 
170
     
 
171
    
 
172
     // OPTIONAL CUSTOMISATION COMPLETED.
 
173
 
 
174
 
 
175
   ChartRenderingInfo info = null;
 
176
   HttpSession session = request.getSession();
 
177
   try {
 
178
 
 
179
     //Create RenderingInfo object
 
180
     response.setContentType("text/html");
 
181
     info = new ChartRenderingInfo(new StandardEntityCollection());
 
182
     BufferedImage chartImage = chart.createBufferedImage(800, 500, info);
 
183
 
 
184
     // putting chart as BufferedImage in session, 
 
185
     // thus making it available for the image reading action Action.
 
186
     session.setAttribute("chartImage", chartImage);
 
187
 
 
188
     PrintWriter writer = new PrintWriter(response.getWriter());
 
189
     
 
190
     
 
191
     ChartUtilities.writeImageMap(writer,"imageMap",info,true);
 
192
     
 
193
     
 
194
     writer.flush();
 
195
   
 
196
   }
 
197
   catch (Exception e) {
 
198
      // handel your exception here
 
199
   }
 
200
  
 
201
   String pathInfo = "http://";
 
202
   pathInfo += request.getServerName();
 
203
   int port = request.getServerPort();
 
204
   pathInfo += ":"+String.valueOf(port);
 
205
   pathInfo += request.getContextPath();
 
206
   String chartViewer = pathInfo + "/servlet/ChartViewer";
 
207
   return chartViewer;
 
208
 }
 
209
 
 
210
 
 
211
 
 
212
 
 
213
public String getChartTitle() {
 
214
        return chartTitle;
 
215
}
 
216
 
 
217
public void setChartTitle(String chartTitle) {
 
218
        this.chartTitle = chartTitle;
 
219
}
 
220
 
 
221
public String[] getCategories1() {
 
222
        return categories1;
 
223
}
 
224
 
 
225
public void setCategories1(String[] categories1) {
 
226
        this.categories1 = categories1;
 
227
}
 
228
 
 
229
public String[] getCategories2() {
 
230
        return categories2;
 
231
}
 
232
 
 
233
public void setCategories2(String[] categories2) {
 
234
        this.categories2 = categories2;
 
235
}
 
236
 
 
237
public double[][] getData1() {
 
238
        return data1;
 
239
}
 
240
 
 
241
public void setData1(double[][] data1) {
 
242
        this.data1 = data1;
 
243
}
 
244
 
 
245
public double[][] getData2() {
 
246
        return data2;
 
247
}
 
248
 
 
249
public void setData2(double[][] data2) {
 
250
        this.data2 = data2;
 
251
}
 
252
 
 
253
public String[] getSeries1() {
 
254
        return series1;
 
255
}
 
256
 
 
257
public void setSeries1(String[] series1) {
 
258
        this.series1 = series1;
 
259
}
 
260
 
 
261
public String[] getSeries2() {
 
262
        return series2;
 
263
}
 
264
 
 
265
public void setSeries2(String[] series2) {
 
266
        this.series2 = series2;
 
267
}
 
268
 
 
269
public String getXAxis_Title() {
 
270
        return xAxis_Title;
 
271
}
 
272
 
 
273
public void setXAxis_Title(String axis_Title) {
 
274
        xAxis_Title = axis_Title;
 
275
}
 
276
 
 
277
public String getYAxis_Title() {
 
278
        return yAxis_Title;
 
279
}
 
280
 
 
281
public void setYAxis_Title(String axis_Title) {
 
282
        yAxis_Title = axis_Title;
 
283
}
 
284
 
 
285
 
 
286
}