I recently started using JFreeChart API for our web application.
The servlet response generates JPEG, but I need to add an html table on top or below of the image, on the same page. Has anybody done this before. Or at least I want to show data on the chart. I appreciate any suggestions on this.
Prashanth
HTML table and image on the same page
Re: HTML table and image on the same page
Prashanth,
I was able to do this using jsps. I created a bean to hold the data I was charting and then included andother jsp for the chart. In the chart jsp I got the data from the bean and chnged the content type to image/jpeg. Then, back in the main page I created an image link to the previous page.
So it looked something like this:
<index.jsp>
<jsp:useBean id="dataMatrix" scope="session" class="com.sun.datamatrix.beans.DataMatrix" />
// code to print out my html table
<img src="imageOut.jsp">
//end of file
<imageOut.jsp>
<%@page import="com.jrefinery.data.*" %>
<%@page import="com.jrefinery.chart.*" %>
<%@page import="com.sun.datamatrix.beans.*" %>
<%@page import="java.io.*" %>
<%@page import="java.awt.*" %>
<jsp:useBean id="dataMatrix" scope="session" class="com.sun.datamatrix.beans.DataMatrix" />
<%
DefaultCategoryDataset dataset = new DefaultCategoryDataset(dataMatrix.retreiveData()); // Method returns a String[][]
dataset.setCategories(dataMatrix.getHeaders(1,0)); //Method returns a String[]
dataset.setSeriesNames(dataMatrix.getLefters(0,1)); //Method returns a String[]
JFreeChart chart = ChartFactory.createVerticalBarChart3D("Area Business Unit Summary",
"Area",
"Est Contract Value",
dataset,
true);
chart.setBackgroundPaint(Color.white);
response.setContentType("image/jpeg");
OutputStream output = response.getOutputStream();
ChartUtilities.writeChartAsJPEG(output, chart, 600, 400);
out.close();
%>
Hope that helps. Took me a couple of days but it was worth it.
Jas
I was able to do this using jsps. I created a bean to hold the data I was charting and then included andother jsp for the chart. In the chart jsp I got the data from the bean and chnged the content type to image/jpeg. Then, back in the main page I created an image link to the previous page.
So it looked something like this:
<index.jsp>
<jsp:useBean id="dataMatrix" scope="session" class="com.sun.datamatrix.beans.DataMatrix" />
// code to print out my html table
<img src="imageOut.jsp">
//end of file
<imageOut.jsp>
<%@page import="com.jrefinery.data.*" %>
<%@page import="com.jrefinery.chart.*" %>
<%@page import="com.sun.datamatrix.beans.*" %>
<%@page import="java.io.*" %>
<%@page import="java.awt.*" %>
<jsp:useBean id="dataMatrix" scope="session" class="com.sun.datamatrix.beans.DataMatrix" />
<%
DefaultCategoryDataset dataset = new DefaultCategoryDataset(dataMatrix.retreiveData()); // Method returns a String[][]
dataset.setCategories(dataMatrix.getHeaders(1,0)); //Method returns a String[]
dataset.setSeriesNames(dataMatrix.getLefters(0,1)); //Method returns a String[]
JFreeChart chart = ChartFactory.createVerticalBarChart3D("Area Business Unit Summary",
"Area",
"Est Contract Value",
dataset,
true);
chart.setBackgroundPaint(Color.white);
response.setContentType("image/jpeg");
OutputStream output = response.getOutputStream();
ChartUtilities.writeChartAsJPEG(output, chart, 600, 400);
out.close();
%>
Hope that helps. Took me a couple of days but it was worth it.
Jas