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