How to save the Dynamic Piechart in jsp
This is the continuation of previous tutorial about creating dynamic chart in jsp,One Difference is here we create and save the chart in localdisk and show it on the page see the code below
<%@ page import="java.awt.*" %>
<%@ page import="java.io.*" %>
<%@ page import="org.jfree.chart.*" %>
<%@ page import="org.jfree.chart.entity.*" %>
<%@ page import ="org.jfree.data.general.*"%>
<%
final DefaultPieDataset data = new DefaultPieDataset();
data.setValue("A", new Double(10.0));
data.setValue("B", new Double(20.0));
data.setValue("C", new Double(30.0));
JFreeChart chart = ChartFactory.createPieChart
("Pie Chart ", data, true, true, false);
try {
final ChartRenderingInfo info = new
ChartRenderingInfo(new StandardEntityCollection());
final File file1 = new File("c://piechart.png");
ChartUtilities.saveChartAsPNG(file1, chart, 600, 400, info);
} catch (Exception e) {
out.println(e);
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<IMG SRC="c:/piechart.png" WIDTH="600" HEIGHT="400"
BORDER="0" USEMAP="#chart">
</body>
</html>
Here we create the file as “final File file1 = new File(”c://piechart.png”);”
and use the save method as ChartUtilities.saveChartAsPNG(file1, chart, 600, 400, info);
and finally we show the image in the page using <IMG SRC=”c:/piechart.png” WIDTH=”600″ HEIGHT=”400″
BORDER=”0″ USEMAP=”#chart”> that’s it you have mastered in creating chart in jsp any doubt ask us Happy coding.



