~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/OverlaidBarChart2.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.BasicStroke;
 
4
import java.awt.Font;
 
5
import java.awt.image.BufferedImage;
 
6
import java.io.PrintWriter;
 
7
 
 
8
import javax.servlet.http.HttpServletRequest;
 
9
import javax.servlet.http.HttpServletResponse;
 
10
import javax.servlet.http.HttpSession;
 
11
 
 
12
import org.jfree.chart.ChartRenderingInfo;
 
13
import org.jfree.chart.ChartUtilities;
 
14
import org.jfree.chart.JFreeChart;
 
15
import org.jfree.chart.axis.CategoryAxis;
 
16
import org.jfree.chart.axis.CategoryLabelPositions;
 
17
import org.jfree.chart.axis.NumberAxis;
 
18
import org.jfree.chart.entity.StandardEntityCollection;
 
19
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
 
20
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
 
21
import org.jfree.chart.plot.CategoryPlot;
 
22
import org.jfree.chart.plot.DatasetRenderingOrder;
 
23
import org.jfree.chart.plot.PlotOrientation;
 
24
import org.jfree.chart.renderer.category.BarRenderer;
 
25
import org.jfree.chart.renderer.category.CategoryItemRenderer;
 
26
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
 
27
import org.jfree.chart.title.TextTitle;
 
28
import org.jfree.data.category.CategoryDataset;
 
29
import org.jfree.data.category.DefaultCategoryDataset;
 
30
import org.jfree.ui.RectangleEdge;
 
31
import org.jfree.ui.VerticalAlignment;
 
32
 
 
33
public class OverlaidBarChart2 {
 
34
        //       Data Related Bar Chart
 
35
        //      Row Keys
 
36
        String[] series1;
 
37
        // Column Keys
 
38
        String[] categories1;
 
39
        // data...      
 
40
        double[][] data1;
 
41
        
 
42
        String chartTitle;
 
43
        String xAxis_Title;
 
44
        String yAxis_Title;
 
45
        // Data Related Line Chart
 
46
 
 
47
        /*
 
48
        String[] series2;
 
49
        // Column Keys
 
50
        String[] categories2;
 
51
        */
 
52
        // data...      
 
53
        double[][] data2;
 
54
        
 
55
        //      constructor
 
56
        public OverlaidBarChart2() { }
 
57
 
 
58
        // Function For Creating Dataset for Barchart
 
59
        private DefaultCategoryDataset  getDataset1() 
 
60
        {    
 
61
                
 
62
                //      create the dataset...
 
63
                DefaultCategoryDataset dataset = new DefaultCategoryDataset();
 
64
                                        
 
65
                for(int i=0;i<series1.length;i++)
 
66
                {
 
67
                        for(int j=0;j<categories1.length;j++)
 
68
                        {
 
69
                                dataset.addValue(data1[i][j], series1[i], categories1[j]);
 
70
                        }
 
71
                }
 
72
                
 
73
                return dataset;
 
74
        }
 
75
        
 
76
        // Function For Creating Dataset for Linechart
 
77
        private DefaultCategoryDataset  getDataset2() 
 
78
        {    
 
79
                
 
80
                //      create the dataset...
 
81
                DefaultCategoryDataset dataset = new DefaultCategoryDataset();
 
82
                                        
 
83
                for(int i=0;i<series1.length;i++)
 
84
                {
 
85
                        series1[i]+="(Target)";
 
86
                        for(int j=0;j<categories1.length;j++)
 
87
                        {
 
88
                                dataset.addValue(data2[i][j], series1[i], categories1[j]);
 
89
                        }
 
90
                }
 
91
                
 
92
                return dataset;
 
93
        }
 
94
public String getChartViewer(HttpServletRequest request, HttpServletResponse response) {
 
95
    
 
96
         final CategoryDataset dataset1 = getDataset1();
 
97
 
 
98
    // create the first plot...
 
99
    final CategoryItemRenderer renderer = new BarRenderer();
 
100
    renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
 
101
    renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());    
 
102
    renderer.setItemLabelsVisible(true); 
 
103
    
 
104
    final CategoryPlot plot = new CategoryPlot();
 
105
    plot.setDataset(dataset1);
 
106
    plot.setRenderer(renderer);
 
107
    
 
108
    plot.setDomainAxis(new CategoryAxis(xAxis_Title));
 
109
    plot.setRangeAxis(new NumberAxis(yAxis_Title));
 
110
    
 
111
    plot.setOrientation(PlotOrientation.HORIZONTAL);
 
112
    plot.setRangeGridlinesVisible(true);
 
113
    plot.setDomainGridlinesVisible(true);
 
114
    
 
115
    // customise the range axis...
 
116
    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
 
117
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
 
118
    rangeAxis.setAutoRangeIncludesZero(true);
 
119
    rangeAxis.setUpperMargin(0.15);
 
120
    rangeAxis.setLowerMargin(0.15);
 
121
    
 
122
    final CategoryDataset dataset2 = getDataset2();
 
123
         
 
124
    final CategoryItemRenderer renderer2 = new LineAndShapeRenderer();
 
125
    renderer2.setSeriesStroke(0, new BasicStroke(2.0f));
 
126
    renderer2.setSeriesStroke(1, new BasicStroke(2.0f));
 
127
    plot.setDataset(1, dataset2);
 
128
    plot.setRenderer(1, renderer2);
 
129
    plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
 
130
    
 
131
 //   plot.setBackgroundPaint(Color.lightGray);
 
132
 //   plot.setRangeGridlinePaint(Color.white);
 
133
    
 
134
    final JFreeChart chart = new JFreeChart(plot);
 
135
    //chart.setTitle("Overlaid Bar Chart");
 
136
//    chart.setLegend(new StandardLegend());
 
137
//    chart.setBackgroundPaint(Color.white);
 
138
 
 
139
    /* TITLE */
 
140
    final TextTitle mainTitle = new TextTitle("Overlaid Bar Chart");
 
141
    mainTitle.setFont(new Font("times", Font.BOLD, 13));
 
142
    mainTitle.setPosition(RectangleEdge.TOP);     
 
143
    mainTitle.setVerticalAlignment(VerticalAlignment.BOTTOM);
 
144
    chart.addSubtitle(mainTitle);
 
145
    
 
146
    /* SUB TITLE */     
 
147
    final TextTitle subtitle = new TextTitle(chartTitle);
 
148
    subtitle.setFont(new Font("times", Font.BOLD, 13));
 
149
    subtitle.setPosition(RectangleEdge.TOP);     
 
150
    subtitle.setVerticalAlignment(VerticalAlignment.BOTTOM);
 
151
    chart.addSubtitle(subtitle);
 
152
    
 
153
    final CategoryAxis domainAxis = plot.getDomainAxis();
 
154
    domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
 
155
    
 
156
    // OPTIONAL CUSTOMISATION COMPLETED.
 
157
 
 
158
 
 
159
  ChartRenderingInfo info = null;
 
160
  HttpSession session = request.getSession();
 
161
  try {
 
162
 
 
163
    //Create RenderingInfo object
 
164
    response.setContentType("text/html");
 
165
    info = new ChartRenderingInfo(new StandardEntityCollection());
 
166
    BufferedImage chartImage = chart.createBufferedImage(800, 500, info);
 
167
 
 
168
    // putting chart as BufferedImage in session, 
 
169
    // thus making it available for the image reading action Action.
 
170
    session.setAttribute("chartImage", chartImage);
 
171
 
 
172
    PrintWriter writer = new PrintWriter(response.getWriter());
 
173
    
 
174
    
 
175
    ChartUtilities.writeImageMap(writer,"imageMap",info,true);
 
176
    
 
177
    
 
178
    writer.flush();
 
179
  
 
180
  }
 
181
  catch (Exception e) {
 
182
     // handel your exception here
 
183
  }
 
184
 
 
185
  String pathInfo = "http://";
 
186
  pathInfo += request.getServerName();
 
187
  int port = request.getServerPort();
 
188
  pathInfo += ":"+String.valueOf(port);
 
189
  pathInfo += request.getContextPath();
 
190
  String chartViewer = pathInfo + "/servlet/ChartViewer";
 
191
  return chartViewer;
 
192
}
 
193
 
 
194
 
 
195
 
 
196
 
 
197
public String getChartTitle() {
 
198
        return chartTitle;
 
199
}
 
200
 
 
201
public void setChartTitle(String chartTitle) {
 
202
        this.chartTitle = chartTitle;
 
203
}
 
204
 
 
205
public String[] getCategories1() {
 
206
        return categories1;
 
207
}
 
208
 
 
209
public void setCategories1(String[] categories1) {
 
210
        this.categories1 = categories1;
 
211
}
 
212
 
 
213
 
 
214
public double[][] getData1() {
 
215
        return data1;
 
216
}
 
217
 
 
218
public void setData1(double[][] data1) {
 
219
        this.data1 = data1;
 
220
}
 
221
 
 
222
public double[][] getData2() {
 
223
        return data2;
 
224
}
 
225
 
 
226
public void setData2(double[][] data2) {
 
227
        this.data2 = data2;
 
228
}
 
229
 
 
230
public String[] getSeries1() {
 
231
        return series1;
 
232
}
 
233
 
 
234
public void setSeries1(String[] series1) {
 
235
        this.series1 = series1;
 
236
}
 
237
 
 
238
public String getXAxis_Title() {
 
239
        return xAxis_Title;
 
240
}
 
241
 
 
242
public void setXAxis_Title(String axis_Title) {
 
243
        xAxis_Title = axis_Title;
 
244
}
 
245
 
 
246
public String getYAxis_Title() {
 
247
        return yAxis_Title;
 
248
}
 
249
 
 
250
public void setYAxis_Title(String axis_Title) {
 
251
        yAxis_Title = axis_Title;
 
252
}
 
253
 
 
254
}