Hii Sir, I made a pie chart from database using jfreechart.Now I need to display the values and their percentage in the sections of the piechart.kindly help me out as i am in need of this in my live project. thnx in advance......
import java.io.IOException; import java.io.OutputStream; import javax.servlet.*; import javax.servlet.http.*; import java.awt.Color; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.data.general.DefaultPieDataset; import java.sql.; import java.io.; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PiePlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.*; import org.jfree.data.jdbc.JDBCCategoryDataset; import org.jfree.data.jdbc.JDBCPieDataset;
public class piechart extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{ doTestbarChart(request, response); } // End Method/** * This method creates a test pie chart using internally * generated data. This method can be used to test the * basic JFreeChart setup and the basic servlet * configuration. * * @param request The HttpServletRequest request, * which contains lots of useful * information about the request. * * @param response The HttpServletResponse object, * which allows the output being sent * back to be tailored. */
protected void doTestbarChart(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { OutputStream out = response.getOutputStream();
try { String query = "SELECT * from satya"; JDBCPieDataset dataset = new JDBCPieDataset( "jdbc:mysql://localhost:3306/db_jasper","com.mysql.jdbc.Driver", "root", "password");
dataset.executeQuery(query); JFreeChart chart = ChartFactory.createPieChart("CallRecord Report", dataset,
true, true,true); PiePlot plot = (PiePlot) chart.getPlot(); plot.setSimpleLabels(true);try {
ChartUtilities.saveChartAsJPEG(new File("C:/piechart.jpg"), chart, 400, 300); } catch (IOException e) { System.out.println("Problem in creating chart."); } // No, no URLs
chart.setBackgroundPaint(Color.white);//response.setHeader("Content-Disposition", "attachment;filename=piechart.jpg"); response.setContentType("image/png"); //response.setContentType("application/octet-stream"); ChartUtilities.writeChartAsPNG(out, chart, 500, 400); } catch (Exception e) { System.out.println(e.toString()); } finally { out.close(); }
} // End Method
} // End Class
The given code is in core java. You can use it in Servlet. It display the pie chart values with percentage.
import java.awt.*; import java.awt.event.*; import java.awt.event.*; import java.text.*; import javax.swing.*; import org.jfree.chart.*; import org.jfree.chart.labels.StandardPieSectionLabelGenerator; import org.jfree.chart.plot.PiePlot; import org.jfree.data.general.*; import org.jfree.ui.*; import org.jfree.util.*; public class PieChart extends ApplicationFrame { public PieChart(final String title) { super(title); final PieDataset dataset = createDataset(14); final JFreeChart chart = ChartFactory.createPieChart( "Pie Chart Demo ", dataset, false, true, false ); chart.setBackgroundPaint(new Color(222, 222, 255)); final PiePlot plot = (PiePlot) chart.getPlot(); plot.setBackgroundPaint(Color.white); plot.setLabelGenerator(new StandardPieSectionLabelGenerator(" {2}", NumberFormat.getNumberInstance(), NumberFormat.getPercentInstance())); final ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new java.awt.Dimension(500, 270)); setContentPane(chartPanel); } private PieDataset createDataset(final int sections) { final DefaultPieDataset result = new DefaultPieDataset(); for (int i = 0; i < sections; i++) { final double value = 100.0 * Math.random(); result.setValue("" + i, value); } return result; } public static void main(final String[] args) { Log.getInstance().addTarget(new PrintStreamLogTarget()); final PieChart demo = new PieChart("Pie Chart Demo"); demo.pack(); RefineryUtilities.centerFrameOnScreen(demo); demo.setVisible(true); } }
Ads