Chart Example That works

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
olhinhoazul
Posts: 2
Joined: Thu Nov 24, 2005 3:48 pm

Chart Example That works

Post by olhinhoazul » Thu Nov 24, 2005 4:01 pm

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

Souma
Posts: 17
Joined: Sun Nov 20, 2005 4:02 am

Post by Souma » Fri Nov 25, 2005 9:45 am

Erm... sorry if i made a mistake

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+"'>");

mxrblind
Posts: 2
Joined: Wed Jul 04, 2007 5:45 pm

couldn't get it work!!

Post by mxrblind » Thu Jul 05, 2007 4:11 pm

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?

amans19
Posts: 2
Joined: Wed Jul 11, 2007 2:45 am

Try this

Post by amans19 » Wed Jul 11, 2007 3:36 pm

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());
}
%>

Locked