Dynamic PieChart in Jsp
Today We are going to learn how to create a dynamic pie-chart in jsp page.
It is a easy way to create chart in java using jfree chart to more about that see previous posts about charts lets create the chart see the example 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.*"%>
<%@ page import = "org.jfree.chart.plot.PiePlot"%>
<%@ page import ="java.io.OutputStream"%>
<%
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());
OutputStream out1 = response.getOutputStream();
PiePlot plot = (PiePlot) chart.getPlot();
plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
plot.setNoDataMessage("No data available");
plot.setCircular(false);
plot.setLabelGap(0.02);
ChartUtilities.writeChartAsPNG(out1, chart, 600, 400);
} catch (Exception e) {
out.println(e);
}
%>
In this example first you have to create an object for DefaultPieDataset and set the values you want to show in chart then use JFreeChart chart = ChartFactory.createPieChart
(”Pie Chart “, data, true, true, false); to create piechart,use rendering if you want to set your own color,finally ChartUtilities.writeChartAsPNG(out1, chart, 600, 400); is used to write the chart in the jsp page. Any Doubt Ask us Happy Coding.





