I'm trying to do something similar - and also having problems...
Is it possible to have a bean (stored in the Request object) that creates the chart one time and stores it, and then provides methods to retrieve the Chart or Image Map information? This bean could be referenced from within a JSP to get the chart, and then a servlet could be used to actually stream the chart image. Something like:
Java Bean:
Code: Select all
public class ChartBean
{
private JFreeChart myChart = null;
public JFreeChart getMyChart()
{
if (myChart == null)
{
createMyChart();
} // end if
return myChart ;
}
public void createMyChart()
{
// Do some stuff to build the chart...
// Create tooltip and URL generators
StandardXYItemLabelGenerator ttg = new StandardXYItemLabelGenerator(blah blah);
TimeSeriesURLGenerator urlg = new TimeSeriesURLGenerator(blah blah blah blah);
// Create the axes
ValueAxis timeAxis = new DateAxis(timeAxisLabel);
NumberAxis valueAxis = new NumberAxis(valueAxisLabel);
valueAxis.setAutoRangeIncludesZero(false); // override default
// Create the renderer
StandardXYItemRenderer renderer = new StandardXYItemRenderer(StandardXYItemRenderer.LINES, ttg, urlg);
renderer.setShapesFilled(true);
// Create the plot
XYPlot plot = new XYPlot(dataset, timeAxis, valueAxis, renderer);
// Create the chart
myChart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
} // end createNumberOfUsersChartTitle()
public String getMyChartImageMap()
{
// Not sure what to do here...
String map = "";
String name = "myChart";
// Create the chart rendering Info
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
// Create the image map
map = ChartUtilities.getImageMap(name, info);
return map;
}
}
JSP:
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<%@ page
language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"
%>
<jsp:useBean
id="chartBean"
class="com.hewitt.ispsoft.chart.TestChartBean"
scope="request"/>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM WebSphere Studio">
<TITLE>TestPage.jsp</TITLE>
</HEAD>
<BODY>
<P>My Chart</P>
<img name="chartNumberOfUsers" src="http://localhost:9080/JFree/TestServlet"/>
<!-- Begin Image Map Info -->
<%= chartBean.getMyChartImageMap() %>
<!-- End Image Map Info -->
</BODY>
</HTML>
Servlet:
Code: Select all
public class TestServlet extends HttpServlet implements Servlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
// Create the Chart Bean
ChartBean chartBean = new ChartBean();
// Get the chart object
JFreeChart jfc = chartBean.getMyChart();
// Add the bean to the request object
request.setAttribute("chartBean", chartBean);
// set the reponse content type
response.setContentType("text/png");
// send the response
ChartUtilities.writeChartAsPNG( response.getOutputStream(), jfc, 640, 480);
// close the output stream
response.getOutputStream().close();
} // end doPost()
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
doPost(req, resp);
} // end doGet()
}
It seems like there should be a way to do this, but I can't figure out how the Image Map information gets generated by the ChartUtilities class - when you call getImageMap() or writeImageMap(). You pass in a ChartRenderingInfo object, but its usually created using an empty default constructor:
Code: Select all
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
So, I don't see how it "knows" anything about the specific chart in order to generate the ToolTips...
Does the ChartRenderingInfo somehow get magically populated with some information when the chart gets rendered when you call the method writeChartAsPNG()?
I also tried using a method similar to the examples by essentially generating the chart twice (and saving it to a temp file), but I'd like to avoid that if possible... It seems like there should be a better way...
Am I missing something?
Thanks in advance...
Ted