I created a JSP that generates a Scalable Vector Graphics (SVG) chart and sends it directly to the browser. This code was tested using the Adobe (http://www.adobe.com/svg/) SVG Viewer 3.0.
I tested this code in Forte 3.0. I put it in a new directory called jsp in the JFreeChart demo directory. Forte automatically creates a web module (WEB-INF) for you. Make sure you have the JFreeChart jar files mounted. Then just Compile & Execute (Forte automatically configures tomcat and will automatically bring up your browser (except Netscape 6).
You also need to have the following Batik (http://xml.apache.org/batik/) jar files mounted.
batik-awt-util.jar
batik-dom.jar
batik-ext.jar
batik-svggen.jar
batik-util.jar
batik-xml.jar
Marcus
<%@page contentType="text/html"%>
<%@page import="com.jrefinery.data.DefaultPieDataset" %>
<%@page import="com.jrefinery.chart.ChartFactory" %>
<%@page import="com.jrefinery.chart.JFreeChart" %>
<%@page import="com.jrefinery.chart.JFreeChartFrame" %>
<%@page import="java.io.IOException" %>
<%@page import="java.io.OutputStream" %>
<%@page import="com.jrefinery.chart.*" %>
<%@page import="java.awt.Color" %>
<%@page import="java.awt.GradientPaint" %>
<%@ page import="java.awt.geom.Rectangle2D" %>
<%@ page import="java.io.*" %>
<%@ page import="org.apache.batik.svggen.SVGGraphics2D" %>
<%@ page import="org.apache.batik.dom.GenericDOMImplementation" %>
<%@ page import="org.w3c.dom.Document" %>
<%@ page import="org.w3c.dom.DOMImplementation" %>
<html>
<head><title>Scalable Vector Graphics (SVG) JFreeChart JSP (Java Server Page) Example</title></head>
<body>
<%-- The Chart below is sent directly to the browser as a SVG Document. --%>
<% createSVGWebChart(response); %>
<%!
/**
* createSVGWebChart
* @author Marcus Wilhoit, NiSUSTECH, 05.20.2002
* @param httpServletResponse HttpServletResponse
* @throws IOException catch standard IOException
*/
public void createSVGWebChart(HttpServletResponse httpServletResponse) throws IOException
{
// create a dataset...
DefaultPieDataset data = new DefaultPieDataset();
data.setValue("Category 1", new Double(43.2));
data.setValue("Category 2", new Double(27.9));
data.setValue("Category 3", new Double(79.5));
data.setValue("Category 4", new Double(66.5));
// create a chart...
JFreeChart chart = ChartFactory.createPieChart("Sample Pie Chart", data, true);
chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000, Color.orange));
PiePlot piePlot = (PiePlot)chart.getPlot();
//piePlot.setSectionLabelType(PiePlot.NAME_AND_PERCENT_LABELS);
piePlot.setSectionLabelType(PiePlot.VALUE_AND_PERCENT_LABELS);
piePlot.setCircular(false);
// make section 1 explode by 100%...
piePlot.setRadiusPercent(0.60);
piePlot.setExplodePercent(1, 1.00);
// THE FOLLOWING CODE BASED ON THE EXAMPLE IN THE BATIK DOCUMENTATION...
// Get a DOMImplementation
DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
// Create an instance of org.w3c.dom.Document
Document document = domImpl.createDocument(null, "svg", null);
// Create an instance of the SVG Generator
SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
// Ask the chart to render into the SVG Graphics2D implementation
chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, 400, 300), null);
// Finally, write out SVG
boolean useCSS = true; // we want to use CSS style attribute
httpServletResponse.setContentType("image/svg+xml");
Writer out = httpServletResponse.getWriter();
svgGenerator.stream(out, useCSS);
out.close();
}
%>
</body>
</html>
JSP (Java Server Page) SVG JFreeChart Example
Re: JSP (Java Server Page) SVG JFreeChart Example
Hi Marcus,
Thanks for posting your code.
Regards,
DG.
Thanks for posting your code.
Regards,
DG.