These example its working on a portlet/jsp
I put the code because its dificult to find a source that works!!!
Have fun
on web.xml
<servlet>
<servlet-name>DisplayChart</servlet-name>
<display-name>DisplayChart</display-name>
<description>Servlet that create the chart</description>
<servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/servlet/DisplayChart</url-pattern>
</servlet-mapping>
on jsp
<%@page import="org.jfree.chart.ChartFactory"%>
<%@page import=" org.jfree.chart.JFreeChart"%>
<%@page import="org.jfree.chart.plot.PlotOrientation"%>
<%@page import="org.jfree.chart.servlet.*"%>
<%@page import="org.jfree.data.DefaultCategoryDataset"%>
or
<%@page import="org.jfree.data.category.DefaultCategoryDataset"%>
<%
String graphURL;
String filename;
try{
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.setValue(new Double(100.0),"","Machico");
dataset.setValue(new Double(80.0),"", "Caniço");
dataset.setValue(new Double(60.0),"", "Gaula");
dataset.setValue(new Double(40.0), "","Santana");
JFreeChart chart = ChartFactory.createBarChart3D("Chart",
"X","Y",dataset,PlotOrientation.HORIZONTAL,false,false,false); filename = ServletUtilities.saveChartAsJPEG(chart, 500, 300, null, request.getSession());
graphURL = request.getContextPath () + "/servlet/DisplayChart?filename="+ filename;
out.println("<IMG SRC='"+graphURL+"' width='500' height='300' border='0' usemap='#"+ filename+"'>");
}
}catch(Exception a){
System.out.println("Erro no JSP"+a.getMessage());
}
%>
need 2 jar files to run the aplication put these jar on web-inf/lib
jfreechart-1.0.0-rc1.jar
jcommon-1.0.0-rc1.jar
download the file on
http://www.jfree.org/jfreechart/index.php
download the file nad unzip inside exist the 2 jars
Chart Example That works
Erm... sorry if i made a mistake
there's no declaration or some sort for the "out"
there's no declaration or some sort for the "out"
Code: Select all
out.println("<IMG SRC='"+graphURL+"' width='500' height='300' border='0' usemap='#"+ filename+"'>");
couldn't get it work!!
I tried this code inside a IBM Portlet...but it doestn't show the image... I use this code inside the portlet view JSP. Any idea?
Try this
This code streams the image directly to the browser
Code: Select all
<%@page import="org.jfree.chart.ChartFactory"%>
<%@page import=" org.jfree.chart.JFreeChart"%>
<%@page import="org.jfree.chart.plot.PlotOrientation"%>
<%@page import="org.jfree.chart.servlet.*"%>
<%@page import="org.jfree.data.category.DefaultCategoryDataset"%>
<%@page import="org.jfree.chart.ChartUtilities"%>
<%
String graphURL = "";
String filename = "";
try{
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.setValue(new Double(100.0),"","Machico");
dataset.setValue(new Double(80.0),"", "Caniço");
dataset.setValue(new Double(60.0),"", "Gaula");
dataset.setValue(new Double(40.0), "","Santana");
JFreeChart chart = ChartFactory.createBarChart3D("Chart",
"X","Y",dataset,PlotOrientation.HORIZONTAL,false,false,false);
response.setContentType("image/jpeg");
ChartUtilities.writeBufferedImageAsJPEG(response.getOutputStream(), chart.createBufferedImage(500,300));
}catch(Exception a){
System.out.println("Erro no JSP"+a.getMessage());
}
%>