Dear everyone,
I have problem using jfreechart with JSP. I use resin to be servlet engine. I try to use example program as below.
<%@page contentType="text/html"%>
<%@page import="java.awt.Paint" %>
<%@page import="java.awt.Color" %>
<%@page import="java.awt.Stroke" %>
<%@page import="com.jrefinery.data.TimeSeriesCollection" %>
<%@page import="com.jrefinery.data.Day" %>
<%@page import="com.jrefinery.data.BasicTimeSeries" %>
<%@page import="com.jrefinery.data.XYDataset" %>
<%@page import="com.jrefinery.data.XYSeriesCollection" %>
<%@page import="com.jrefinery.data.XYSeries" %>
<%@page import="com.jrefinery.ui.ApplicationFrame" %>
<%@page import="com.jrefinery.chart.JFreeChart" %>
<%@page import="com.jrefinery.chart.ChartFactory" %>
<%@page import="com.jrefinery.chart.ChartPanel" %>
<%@page import="com.jrefinery.chart.XYPlot" %>
<%@page import="com.jrefinery.chart.NumberAxis" %>
<%@page import="com.jrefinery.chart.TickUnits" %>
<%@page import="java.io.IOException" %>
<%@page import="java.io.OutputStream" %>
<%@page import="com.jrefinery.chart.*" %>
<%@page import="java.awt.GradientPaint" %>
<%@page import="java.util.*" %>
<%@page import=" com.jrefinery.data.Hour" %>
<%@page import=" com.jrefinery.data.Day" %>
<%@page import=" com.jrefinery.data.Minute" %>
<html>
<head>
<title>Test Time-Series Graphs</title>
</head>
<body>
<table border="3">
<tr>
<td>Graphical Examples</td>
</tr>
<tr>
<td>
<%-- The Chart below is sent directly to the browser as a JPEG Output Stream. --%>
<% createWebChart(response); %>
</td>
</tr>
</table>
</body>
</html>
<%!
public void createWebChart(HttpServletResponse httpServletResponse) throws IOException
{
// Create a time series data set for the chart
TimeSeriesCollection dataSet = new TimeSeriesCollection();
BasicTimeSeries series = new BasicTimeSeries("Per Minute Data", Minute.class);
Day today = new Day(17, 6, 2002);
Day today2 = new Day (18, 6, 2002);
series.add(new Minute( 53, new Hour(11, today)), 0);
series.add(new Minute( 02, new Hour(12, today)), 12.5);
series.add(new Minute(23, new Hour(16, today)), 25);
series.add(new Minute(28, new Hour(18, today)), 37.5);
series.add(new Minute(05, new Hour(20, today)), 25);
series.add(new Minute(19, new Hour(20, today)), 12.5);
series.add(new Minute(32, new Hour(10, today2)), 25);
series.add(new Minute(33, new Hour(11, today2)), 12.5);
series.add(new Minute(41, new Hour(12, today2)), 0);
series.add(new Minute(53, new Hour(12, today2)), 12.5);
series.add(new Minute(20, new Hour(14, today2)), 25);
series.add(new Minute(05, new Hour(17, today2)), 12.5);
dataSet.addSeries(series);
// Create the chart
JFreeChart chart = ChartFactory.createTimeSeriesChart("Test Chart", "Date", "Values", dataSet, true);
XYPlot plot = chart.getXYPlot();
Paint p = new GradientPaint (0, 0, Color.white, 1000, 0, Color.blue);
chart.setBackgroundPaint(p);
//httpServletResponse.setContentType("image/jpeg");
OutputStream out = httpServletResponse.getOutputStream();
int width = 300; //400
int height = 150; //300
ChartUtilities.writeChartAsJPEG(out, chart, width, height);
out.close();
}
%>
I don't have any errors when I call it. However, I can't see output in chart, but I get the result in strange format as below:
Graphical Examples
ÿØÿàJFIFÿÛCÿÛCÿÀ,?"ÿÄ ÿĵ}!1AQa"q2?‘¡#B±ÁRÑð$3br‚ %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖרÙÚáâãäåæçèéêñòóôõö÷øùúÿÄ ÿĵw!1AQaq"2?B‘¡±Á #3RðbrÑ $4á%ñ&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz‚ƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖרÙÚâãäåæçèéêòóôõö÷øùúÿÚ ?þ€(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢Š(¢¼ÿâ§Å_‡_>ø«â¿ÅŸèþøwà?8j¾(ñ^½;A¦iVos”O*9®.n¯oî4í7O²‚çPÔõ+ËM;Oµ¹¾º··?Ð(¯˜þ þØß³ÇÇÿëß>xÏZ“â'†¼1¦xÛWð޾|TøCãh¼Ii•ã ? ü]ðO?µý[·Òjz[ø‹HÓï´w}BÁ~Ú í¨—éÊ(¢Š(¯øƒûE|øYñOà§Á_xÃû âgíyãkƒžÿ„Å:Ÿü&´?3\ñ”_Û:6‰¨øÃÿØú^³¦Ýyž)ÕtHõ´ùSß\C<1{]WÍk߀³WŠ~xâ߉üS§ø×ã|þ.µøQá|&ø¿ñwÅ7¹ð%¶‡{âË}#@øAà?ê¢}ÏÄzEܱ]Ú[=ÅÄ÷KsŸ¨½¦wßÛOövø¿àËoü+ñOŒ>"xzOŠÏðOT‹Á¿>6ø‡ÄÞ ø• È÷ÚįiŸî|oð®ÇIDG×|Sñ#ÃÞðŽ€—zskZí‚êšqºú¢Š+Æþ5þÐ??ghÞ&øÁã(<)aâ_iž 𥅾“âø§Æ^0ÖLƒJð§‚|àý'_ñ—ŒüI~!™í´/ è:¶©,PÍ2ÚùQHêì”WËýµ¿f_|5øÏñgÃÞý?l|U¨|s·Õ¼ãÿ øãáT Ñ5/x’/ü,ñ?…tŠ-ýŽ?£j·piW>]SUm:ò×H³¿¼‚Ku÷_‡?|!ñgáÿ?þ)|?Õÿ·ü ñ#Â>ñׂõß°jzWöÏ…|Y¤Zk¾Õ³5»-7YӾߥ_ZÝ}‡UÓìu_7Ƚ³¶¹I!@ÊŠ+Ì>*üiøWðCJðÖµñcÇ?
So, I need help from everyone. Please, suggest me solving this problem.
Thanks
pkh
Can't show chart
Re: Can't show chart
You have to set the response's content type to "image/jpeg". This is out commented in our code. It should then work...
Robert
Robert
Re: Can't show chart
Ooops! it should read in YOUR code, not in our code. Sorry!
Re: Can't show chart
You can try to do somethings like:
A jsp:
<html>
<head>
<title>Test Time-Series Graphs</title>
</head>
<body>
<table border="3">
<tr>
<td>Graphical Examples</td>
</tr>
<tr>
<td>
<img src="Graphics()">
</td>
</tr>
</table>
</body>
</html>
where Graphics() is a servlets that in the service methods you can build the chart with your method "createWebChart()"
Inma.
A jsp:
<html>
<head>
<title>Test Time-Series Graphs</title>
</head>
<body>
<table border="3">
<tr>
<td>Graphical Examples</td>
</tr>
<tr>
<td>
<img src="Graphics()">
</td>
</tr>
</table>
</body>
</html>
where Graphics() is a servlets that in the service methods you can build the chart with your method "createWebChart()"
Inma.
Re: Can't show chart
Hi Inma,
Thanks for your advice. However, it's not work. It's still strange result. Do you mind explaining me how to config jfreechart with JSP? I will compare my config with yours. I use resin servelet in windows 2000.
Thanks
pkh
Thanks for your advice. However, it's not work. It's still strange result. Do you mind explaining me how to config jfreechart with JSP? I will compare my config with yours. I use resin servelet in windows 2000.
Thanks
pkh