Hello,
I have a stacked bar in a JSP and I want the drill down option, however for some reason drill is not working.
Here is my code. Kindly correct me if I am wrong somewhere.
Thanks
***
CategoryAxis categoryAxis = new CategoryAxis("Site");
ValueAxis valueAxis = new NumberAxis("Value");
StackedBarRenderer3D rend = new StackedBarRenderer3D();
rend.setItemURLGenerator(new StandardCategoryURLGenerator("xy_chart.jsp","Site","Value"));
rend.setToolTipGenerator(new StandardCategoryToolTipGenerator());
Plot plot = new CategoryPlot(data, categoryAxis, valueAxis, rend);
JFreeChart chart = new JFreeChart("Test", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
chart.setBackgroundPaint(java.awt.Color.white);
chart.setTitle("Test");
File imagen = new File("D:\\charts\\chartTemp.png");
ChartUtilities.saveChartAsPNG(imagen, chart, 400, 300);
%>
<html>
<body>
<head>
<img src="D:\\charts\\chartTemp.png" border=2 usemap="">
</head>
</body>
</html>
Stacked Bar Drill
Hi,
first of all: An URL never starts with an Windows Drive-letter. I would be surprised, if your users, who may access your service over the net and even may use non-windows machines, will be able to find a file at the location 'D:\\charts\\..." on their machines.
Next, drill down usually works by having an active area (a.k.a., a link), which brings the user to a detail view. With images, this means to have an imagemap assigned to the chart. The 'usemap' attribute of the 'img' must contain the name of the image map to be used. Your HTML code should contain the image map. An image-map can be generated using the ImageMapUtilities class of JFreeChart:
http://www.jfree.org/jfreechart/javadoc ... ities.html
Regards,
Thomas
first of all: An URL never starts with an Windows Drive-letter. I would be surprised, if your users, who may access your service over the net and even may use non-windows machines, will be able to find a file at the location 'D:\\charts\\..." on their machines.
Next, drill down usually works by having an active area (a.k.a., a link), which brings the user to a detail view. With images, this means to have an imagemap assigned to the chart. The 'usemap' attribute of the 'img' must contain the name of the image map to be used. Your HTML code should contain the image map. An image-map can be generated using the ImageMapUtilities class of JFreeChart:
http://www.jfree.org/jfreechart/javadoc ... ities.html
Regards,
Thomas
What I've done in the code:
* Changed the invalid Windows absolute path of the temporary file to something valid. For real-world systems, you have to take care of concurrent access and the security implications of having a writable Web-App directory.
* Used a ChartRenderingInfo to collect some information needed to build an imagemap
* An image map is generated using the ChartRenderingInfo and some utility methods provided by JFreeChart.
* The generated imagemap is referenced by its name in the <img> tag
* The invalid HTML has been corrected.
* Changed the invalid Windows absolute path of the temporary file to something valid. For real-world systems, you have to take care of concurrent access and the security implications of having a writable Web-App directory.
* Used a ChartRenderingInfo to collect some information needed to build an imagemap
* An image map is generated using the ChartRenderingInfo and some utility methods provided by JFreeChart.
* The generated imagemap is referenced by its name in the <img> tag
* The invalid HTML has been corrected.
Code: Select all
<%
CategoryAxis categoryAxis = new CategoryAxis("Site");
ValueAxis valueAxis = new NumberAxis("Value");
StackedBarRenderer3D rend = new StackedBarRenderer3D();
rend.setItemURLGenerator(new StandardCategoryURLGenerator("xy_chart.jsp","Site","Value"));
rend.setToolTipGenerator(new StandardCategoryToolTipGenerator());
Plot plot = new CategoryPlot(data, categoryAxis, valueAxis, rend);
JFreeChart chart = new JFreeChart("Test", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
chart.setBackgroundPaint(java.awt.Color.white);
chart.setTitle("Test");
ServletContext context = getServletConfig().getServletContext();
File imagen = new File(context.getRealPath("/") + "/chartTemp.png");
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
ChartUtilities.saveChartAsPNG(imagen, chart, 400, 300, info);
%>
<html>
<head>
<title>Image Map sample</title>
</head>
<body>
<%
ChartUtilities.writeImageMap(out, "chartmap", info);
%>
<img src="chartTemp.png" border=2 usemap="chartmap">
</body>
</html>
Thanks a lot for your reply.
I tried with that, I am getting the following error. I have added PrintWriter as well in the code!
java.lang.IllegalStateException: OutputStream already retrieved
**
File imagen = new File(context.getRealPath("/") + "/chartTemp.png");
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
ChartUtilities.saveChartAsPNG(imagen, chart, 400, 300, info);
response.setContentType("image/png");
PrintWriter writer = new PrintWriter(response.getWriter());
writer.flush();
%>
<html>
<head>
<title>Image Map sample</title>
</head>
<body>
<%
ChartUtilities.writeImageMap(writer,"chartmap",info,true);
I tried with that, I am getting the following error. I have added PrintWriter as well in the code!
java.lang.IllegalStateException: OutputStream already retrieved
**
File imagen = new File(context.getRealPath("/") + "/chartTemp.png");
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
ChartUtilities.saveChartAsPNG(imagen, chart, 400, 300, info);
response.setContentType("image/png");
PrintWriter writer = new PrintWriter(response.getWriter());
writer.flush();
%>
<html>
<head>
<title>Image Map sample</title>
</head>
<body>
<%
ChartUtilities.writeImageMap(writer,"chartmap",info,true);
JSP files define some global variables, one of them, the JspWriter out, can be used to write content. The ServletResponse's writer cannot be used (as you already experienced).
Details on the available predefined variables can be found in the JSP-specifications available at http://java.sun.com/products/jsp/
JSP-Files should not be used to serve PNG data and cannot possibly serve image data and HTML code in one request - your code should write the image into a temporary file and send the HTML to the client. Any book on Servlet/JSP programming should give you an overview over the used technique and how the HTTP protocol is used to serve pages to the client.
Regards,
Thomas
Details on the available predefined variables can be found in the JSP-specifications available at http://java.sun.com/products/jsp/
JSP-Files should not be used to serve PNG data and cannot possibly serve image data and HTML code in one request - your code should write the image into a temporary file and send the HTML to the client. Any book on Servlet/JSP programming should give you an overview over the used technique and how the HTTP protocol is used to serve pages to the client.
Regards,
Thomas
Hello,
Thanks again, I re wrote my JSP to complaint with to send data to html.
But still I am having problems, when it generates the graph, I could see the code it generates with the following code pasted below, I am still not able to understnd why it's not displaying tooltips or drill down. I am pasting below my JSP as well
*** JSP***
File imagen = new File(context.getRealPath("/") + "/chartTemp.png");
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
String filename1 = ServletUtilities.saveChartAsPNG(chart,500, 300, info, session);
chart.setTitle("Test");
OutputStream ostream = response.getOutputStream();
%>
<html>
<head>
<title>Image Map sample</title>
</head>
<body>
<%
String str = ChartUtilities.getImageMap("chartmap",info);
String graphURL1 = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename1;
%>
<img src="<%= imagen %>" usemap="#<%=str %>"></img>
</body>
</html>
****html code generated for the graph****
<html>
<head>
<title>Image Map sample</title>
</head>
<body>
<img src="Temp.png" border=2 usemap="#<map id="chartmap" name="chartmap">
<area shape="rect" coords="317,45,363,127" title="(NON_EMP, Total) = 17" alt="" href="index.html?series=NON_MP&category=Total"/>
<area shape="rect" coords="317,127,363,228" title="(Total) = 21" alt="" href="index.html?series=EMP&category=Total"/>
<area shape="rect" coords="254,180,300,208" title="(NON_EMP, Development) = 6" alt="" href="index.html?series=NON_EMP&category=+Development"/>
<area shape="rect" coords="254,209,300,228" title="(EMP, Development) = 4" alt="" href="index.html?series=EMP&category=Development"/>
</map>">
</body>
</html>
Thanks again, I re wrote my JSP to complaint with to send data to html.
But still I am having problems, when it generates the graph, I could see the code it generates with the following code pasted below, I am still not able to understnd why it's not displaying tooltips or drill down. I am pasting below my JSP as well
*** JSP***
File imagen = new File(context.getRealPath("/") + "/chartTemp.png");
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
String filename1 = ServletUtilities.saveChartAsPNG(chart,500, 300, info, session);
chart.setTitle("Test");
OutputStream ostream = response.getOutputStream();
%>
<html>
<head>
<title>Image Map sample</title>
</head>
<body>
<%
String str = ChartUtilities.getImageMap("chartmap",info);
String graphURL1 = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename1;
%>
<img src="<%= imagen %>" usemap="#<%=str %>"></img>
</body>
</html>
****html code generated for the graph****
<html>
<head>
<title>Image Map sample</title>
</head>
<body>
<img src="Temp.png" border=2 usemap="#<map id="chartmap" name="chartmap">
<area shape="rect" coords="317,45,363,127" title="(NON_EMP, Total) = 17" alt="" href="index.html?series=NON_MP&category=Total"/>
<area shape="rect" coords="317,127,363,228" title="(Total) = 21" alt="" href="index.html?series=EMP&category=Total"/>
<area shape="rect" coords="254,180,300,208" title="(NON_EMP, Development) = 6" alt="" href="index.html?series=NON_EMP&category=+Development"/>
<area shape="rect" coords="254,209,300,228" title="(EMP, Development) = 4" alt="" href="index.html?series=EMP&category=Development"/>
</map>">
</body>
</html>
Last edited by ponic on Mon Oct 17, 2005 6:02 am, edited 1 time in total.