This tutorial discusses how to create bar chart servlet using jChart Java API. In the last example, we described how to create bar chart servlet using JFreeChart Java API. This explanation uses jChart API and we will provide a step by step guide on getting this done in jChart. We will be using jCharts-0.7.5 version of the library to create the chart, so make sure you download a copy of the required JAR file to work out this example. Detailed steps related to this example are documented below:
Step -1: Download required JAR / Library files
For this example to work, you will need the following files:
- jCharts-0.7.5.jar
- servlet-api.jar (comes with Apache TomCat)
Step-2: Generate Bar Chart Servlet Code using JChart and Servlet API
Next, we will use the downloaded jar files and create the bar chart generator - i.e. Servlet code. The complete Java code is provided below:
1 /* Java code to support JChart bar chart servlet */
2 /* Basic libraries required to support servlet code */
3 import java.io.IOException;
4 import java.io.OutputStream;
5 import javax.servlet.ServletException;
6 import javax.servlet.http.HttpServlet;
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9 /* libraries that needs to be imported for jChart */
10 import org.jCharts.chartData.DataSeries;
11 import org.jCharts.test.TestDataGenerator;
12 import org.jCharts.properties.ClusteredBarChartProperties;
13 import org.jCharts.chartData.AxisChartDataSet;
14 import org.jCharts.properties.AxisProperties;
15 import org.jCharts.properties.ChartProperties;
16 import org.jCharts.properties.LegendProperties;
17 import org.jCharts.axisChart.AxisChart;
18 import org.jCharts.encoders.PNGEncoder;
19 import org.jCharts.types.ChartType;
20 import java.awt.Paint;
21
22 /* Main servlet code starts here */
23 public class BarChartServletExample extends HttpServlet {
24 public BarChartServletExample() {
25 /* No code in the constructor for this demonstration */
26 }
27 public void doGet(HttpServletRequest request, HttpServletResponse response)
28 throws ServletException, IOException {
29 OutputStream out = response.getOutputStream(); /* Get the output stream from the response object */
30 try {
31 /* Step -1: Define the chart decorative parameters */
32 String[] xAxisLabels= { "Q1", "Q2", "Q3", "Q4" };
33 String xAxisTitle= "Quarter"; /* X - Axis label */
34 String yAxisTitle= "Sales Count"; /*Y-Axis label */
35 String title= "CountryVsSales - Bar Chart"; /* Chart title */
36 DataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, title ); /* Define the dataseries */
37
38 /* Step - 2: Define the data for the clustered bar chart */
39 double[][] data= new double[][]{ { 30,33,56,2 }, { 45,12,98,15} };
40 String[] legendLabels= { "Rome","Cairo" }; /* Define legend for bar chart */
41 Paint[] paints= TestDataGenerator.getRandomPaints(2);
42 ClusteredBarChartProperties clusteredBarChartProperties= new ClusteredBarChartProperties();
43 AxisChartDataSet axisChartDataSet= new AxisChartDataSet( data, legendLabels, paints, ChartType.BAR_CLUSTERED, clusteredBarChartProperties );
44 dataSeries.addIAxisPlotDataSet( axisChartDataSet );
45
46 /* Step -3 - Create the chart */
47 ChartProperties chartProperties= new ChartProperties(); /* Special chart properties, if any */
48 AxisProperties axis_Properties= new AxisProperties();
49 LegendProperties legend_Properties= new LegendProperties(); /* Dummy Axis and legend properties class */
50 AxisChart my_output_chart= new AxisChart( dataSeries, chartProperties, axis_Properties, legend_Properties, 640, 480 ); /* Create Chart object */
51
52
53 /* Step -3: Set the response type to PNG */
54 response.setContentType("image/png"); /* Set the HTTP Response Type */
55
56 /* Step -4: Write the chart output as PNG image file */
57 PNGEncoder.encode(my_output_chart,out); /* This class creates the output PNG file for the chart */
58
59 }
60 catch (Exception e) {
61 System.err.println(e.toString()); /* Throw exceptions to log files */
62 }
63 finally {
64 out.close();/* Close the output stream */
65 }
66 }
67 }
This code produces the following chart output as shown below:
JChart - Java Bar Chart Servlet Example |
This is a very basic example to get started with using JChart to create bar chart servlet. Hope this gives you an idea to get started with creating chart servlets with open source Java APIs. If you have any issues with this code, post it in the comments section.
No comments:
Post a Comment