1
package org.hisp.gtool.charts;
4
import java.awt.image.BufferedImage;
5
import java.io.PrintWriter;
7
import javax.servlet.http.HttpServletRequest;
8
import javax.servlet.http.HttpServletResponse;
9
import javax.servlet.http.HttpSession;
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;
28
public class BarChart1
42
public String getXAxis_Title() {
47
public void setXAxis_Title(String axis_Title) {
48
xAxis_Title = axis_Title;
52
public String getYAxis_Title() {
57
public void setYAxis_Title(String axis_Title) {
58
yAxis_Title = axis_Title;
62
public String getChartTitle() {
67
public void setChartTitle(String chartTitle) {
68
this.chartTitle = chartTitle;
73
public BarChart1() { }
76
private DefaultCategoryDataset getDataset()
79
// create the dataset...
80
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
82
for(int i=0;i<series.length;i++)
84
for(int j=0;j<categories.length;j++)
86
dataset.addValue(data[i][j], series[i], categories[j]);
93
public String getChartViewer(HttpServletRequest request, HttpServletResponse response) {
94
DefaultCategoryDataset dataset = getDataset();
95
// create the chart...
96
final JFreeChart chart = ChartFactory.createBarChart(
98
"Category", // domain axis label
99
"Value", // range axis label
101
PlotOrientation.VERTICAL, // orientation
102
true, // include legend
107
// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
109
// set the background color for the chart...
110
//chart.setBackgroundPaint(Color.lightGray);
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);
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);
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);
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);
140
// disable bar outlines...
141
final CategoryItemRenderer renderer = plot.getRenderer();
142
renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
143
renderer.setItemLabelsVisible(true);
145
final CategoryAxis domainAxis = plot.getDomainAxis();
146
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
147
domainAxis.setLabel(xAxis_Title);
150
// OPTIONAL CUSTOMISATION COMPLETED.
154
ChartRenderingInfo info = null;
155
HttpSession session = request.getSession();
158
//Create RenderingInfo object
159
response.setContentType("text/html");
160
info = new ChartRenderingInfo(new StandardEntityCollection());
161
BufferedImage chartImage = chart.createBufferedImage(800, 500, info);
163
// putting chart as BufferedImage in session,
164
// thus making it available for the image reading action Action.
165
session.setAttribute("chartImage", chartImage);
167
PrintWriter writer = new PrintWriter(response.getWriter());
170
ChartUtilities.writeImageMap(writer,"imageMap",info,true);
176
catch (Exception e) {
177
// handel your exception here
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";
190
public String[] getCategories() {
195
public void setCategories(String[] categories) {
196
this.categories = categories;
200
public double[][] getData() {
205
public void setData(double[][] data) {
210
public String[] getSeries() {
215
public void setSeries(String[] series) {
216
this.series = series;