How can i display pie chart in jsp page?

匿名 (未验证) 提交于 2019-12-03 00:46:02

问题:

             <span class="typ" bdsfid="153">Pie</span><span class="pln" bdsfid="154"> </span><span class="typ" bdsfid="155">Chart</span>

Output for this is a blank screen, It not thrown any exception..

How can i display pie chart in this page?

Thanks in advance.

回答1:

after creating the chart save the chart as follow:

 ChartUtilities.saveChartAsJPEG(new File(path/piechart.png"),chart,400, 300);

and then

use

**Other way is as discussed in ** How to display line graph using JFreeChart in jsp?

protected void processRequest(HttpServletRequest request, HttpServletResponse response)     throws ServletException, IOException {          response.setContentType("image/png");         ServletOutputStream os = response.getOutputStream();         ImageIO.write(getChart(request), "png", os);         os.close();     }  private RenderedImage getChart(HttpServletRequest request) {         String chart = request.getParameter("chart");         // also you can process other parameters like width or height here         if (chart.equals("myDesiredChart1")) {             JFreeChart chart = [create your chart here];             return chart.createBufferedImage(width, height)         }

and display as

see the answer of Martin Lazar here



回答2:

Finally i got the answer....

.....In servlet....

public void getPieChart() {          DefaultPieDataset pieDataset = new DefaultPieDataset();     pieDataset.setValue("JavaWorld", new Integer(75));     pieDataset.setValue("Other", new Integer(25));          JFreeChart chart = ChartFactory.createPieChart("Discounts Used by Category ", data, true, true, false);         //chart.setBackgroundPaint(new Color(222, 222, 255));             final PiePlot plot = (PiePlot) chart.getPlot();             plot.setBackgroundPaint(Color.white);             plot.setCircular(true);          try {              final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());             final File file1 = new File(getServletContext().getRealPath(".") + "/images/charts/piechart.png");              ChartUtilities.saveChartAsPNG(file1, chart, 600, 400, info);         } catch (Exception e) {             System.out.println(e);          }     }

.....in html page......

  <span class="typ" bdsfid="495">Pie</span><span class="pln" bdsfid="496"> </span><span class="typ" bdsfid="497">Chart</span>

............................................................................

......................Or using only jsp page........

             <span class="typ" bdsfid="506">Pie</span><span class="pln" bdsfid="507"> </span><span class="typ" bdsfid="508">Chart</span>


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!