~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/BarChart1.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.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.plot.CategoryPlot;
 
21
import org.jfree.chart.plot.PlotOrientation;
 
22
import org.jfree.chart.renderer.category.CategoryItemRenderer;
 
23
import org.jfree.chart.title.TextTitle;
 
24
import org.jfree.data.category.DefaultCategoryDataset;
 
25
import org.jfree.ui.RectangleEdge;
 
26
import org.jfree.ui.VerticalAlignment;
 
27
 
 
28
public class BarChart1 
 
29
{
 
30
//      Row Keys
 
31
        String[] series;
 
32
        // Column Keys
 
33
        String[] categories;
 
34
        // data...      
 
35
        double[][] data;
 
36
        
 
37
        String chartTitle;
 
38
        String xAxis_Title;
 
39
        String yAxis_Title;
 
40
        
 
41
        
 
42
        public String getXAxis_Title() {
 
43
                return xAxis_Title;
 
44
        }
 
45
 
 
46
 
 
47
        public void setXAxis_Title(String axis_Title) {
 
48
                xAxis_Title = axis_Title;
 
49
        }
 
50
 
 
51
 
 
52
        public String getYAxis_Title() {
 
53
                return yAxis_Title;
 
54
        }
 
55
 
 
56
 
 
57
        public void setYAxis_Title(String axis_Title) {
 
58
                yAxis_Title = axis_Title;
 
59
        }
 
60
 
 
61
 
 
62
        public String getChartTitle() {
 
63
                return chartTitle;
 
64
        }
 
65
 
 
66
 
 
67
        public void setChartTitle(String chartTitle) {
 
68
                this.chartTitle = chartTitle;
 
69
        }
 
70
 
 
71
 
 
72
        //      constructor
 
73
        public BarChart1() { }
 
74
 
 
75
        
 
76
        private DefaultCategoryDataset  getDataset() 
 
77
        {    
 
78
                
 
79
                //      create the dataset...
 
80
                DefaultCategoryDataset dataset = new DefaultCategoryDataset();
 
81
                                        
 
82
                for(int i=0;i<series.length;i++)
 
83
                {
 
84
                        for(int j=0;j<categories.length;j++)
 
85
                        {
 
86
                                dataset.addValue(data[i][j], series[i], categories[j]);
 
87
                        }                                       
 
88
                }                               
 
89
                
 
90
                return dataset;
 
91
        }
 
92
 
 
93
 public String getChartViewer(HttpServletRequest request, HttpServletResponse response) {
 
94
         DefaultCategoryDataset dataset = getDataset();
 
95
     // create the chart...
 
96
     final JFreeChart chart = ChartFactory.createBarChart(
 
97
         "",       // chart title
 
98
         "Category",               // domain axis label
 
99
         "Value",                  // range axis label
 
100
         dataset,                  // data
 
101
         PlotOrientation.VERTICAL, // orientation
 
102
         true,                    // include legend
 
103
         true,                     // tooltips?
 
104
         false                     // URLs?
 
105
     );
 
106
 
 
107
     // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
 
108
 
 
109
     // set the background color for the chart...
 
110
     //chart.setBackgroundPaint(Color.lightGray);
 
111
     
 
112
     /* TITLE */
 
113
     final TextTitle mainTitle = new TextTitle("BarChart");
 
114
     mainTitle.setFont(new Font("times", Font.BOLD, 13));
 
115
     mainTitle.setPosition(RectangleEdge.TOP);     
 
116
     mainTitle.setVerticalAlignment(VerticalAlignment.BOTTOM);
 
117
     chart.addSubtitle(mainTitle);
 
118
     
 
119
     /* SUB TITLE */     
 
120
     final TextTitle subtitle = new TextTitle(chartTitle);
 
121
     subtitle.setFont(new Font("times", Font.BOLD, 13));
 
122
     subtitle.setPosition(RectangleEdge.TOP);     
 
123
     subtitle.setVerticalAlignment(VerticalAlignment.BOTTOM);
 
124
     chart.addSubtitle(subtitle);
 
125
     
 
126
 
 
127
     // get a reference to the plot for further customisation...
 
128
     final CategoryPlot plot = chart.getCategoryPlot();
 
129
    // plot.setBackgroundPaint(Color.white);
 
130
    // plot.setDomainGridlinePaint(Color.lightGray);
 
131
    // plot.setRangeGridlinePaint(Color.lightGray);
 
132
     
 
133
     // set the range axis to display integers only...
 
134
     final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
 
135
     rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
 
136
     rangeAxis.setUpperMargin(0.15);
 
137
     rangeAxis.setLowerMargin(0.15);
 
138
     rangeAxis.setLabel(yAxis_Title);
 
139
     
 
140
     // disable bar outlines...
 
141
     final CategoryItemRenderer renderer = plot.getRenderer();
 
142
     renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
 
143
     renderer.setItemLabelsVisible(true); 
 
144
     
 
145
     final CategoryAxis domainAxis = plot.getDomainAxis();
 
146
     domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
 
147
     domainAxis.setLabel(xAxis_Title);  
 
148
     
 
149
     
 
150
     // OPTIONAL CUSTOMISATION COMPLETED.
 
151
 
 
152
 
 
153
 
 
154
   ChartRenderingInfo info = null;
 
155
   HttpSession session = request.getSession();
 
156
   try {
 
157
 
 
158
     //Create RenderingInfo object
 
159
     response.setContentType("text/html");
 
160
     info = new ChartRenderingInfo(new StandardEntityCollection());
 
161
     BufferedImage chartImage = chart.createBufferedImage(800, 500, info);
 
162
 
 
163
     // putting chart as BufferedImage in session, 
 
164
     // thus making it available for the image reading action Action.
 
165
     session.setAttribute("chartImage", chartImage);
 
166
 
 
167
     PrintWriter writer = new PrintWriter(response.getWriter());
 
168
     
 
169
     
 
170
     ChartUtilities.writeImageMap(writer,"imageMap",info,true);
 
171
     
 
172
     
 
173
     writer.flush();
 
174
   
 
175
   }
 
176
   catch (Exception e) {
 
177
      // handel your exception here
 
178
   }
 
179
  
 
180
   String pathInfo = "http://";
 
181
   pathInfo += request.getServerName();
 
182
   int port = request.getServerPort();
 
183
   pathInfo += ":"+String.valueOf(port);
 
184
   pathInfo += request.getContextPath();
 
185
   String chartViewer = pathInfo + "/servlet/ChartViewer";
 
186
   return chartViewer;
 
187
 }
 
188
 
 
189
 
 
190
public String[] getCategories() {
 
191
        return categories;
 
192
}
 
193
 
 
194
 
 
195
public void setCategories(String[] categories) {
 
196
        this.categories = categories;
 
197
}
 
198
 
 
199
 
 
200
public double[][] getData() {
 
201
        return data;
 
202
}
 
203
 
 
204
 
 
205
public void setData(double[][] data) {
 
206
        this.data = data;
 
207
}
 
208
 
 
209
 
 
210
public String[] getSeries() {
 
211
        return series;
 
212
}
 
213
 
 
214
 
 
215
public void setSeries(String[] series) {
 
216
        this.series = series;
 
217
}
 
218
        
 
219
 
 
220
}