2012-06-06 60 views
0
<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> 

<%@ 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.*"%> 

<% 
    DefaultPieDataset pieDataset = new DefaultPieDataset(); 
    pieDataset.setValue("JavaWorld", new Integer(75)); 
    pieDataset.setValue("Other", new Integer(25)); 
    JFreeChart chart = ChartFactory.createPieChart("Sample Pie Chart",pieDataset,true,true,false); 
%> 

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Pie Chart</title> 
</head> 
<body> 
    <IMG SRC="piechart.png" WIDTH="600" HEIGHT="400" BORDER="0" 
     USEMAP="#chart"> 
</body> 
</html> 

輸出的,這是一個空白屏幕,它不是拋出的任何異常..如何在jsp頁面中顯示餅圖?

我怎麼能在這個頁面顯示餅圖?

在此先感謝。

+0

可能重複[在jsp中如何使用jFreechart實現餅圖?](http://stackoverflow.com/questions/ 10908270/how-can-implenent-a-pie-chart-using-jfreechart-in-jsp) –

+1

這與你詢問的其他問題有什麼不同? –

+0

這是一個java的問題?這對我來說看起來像HTML。 – gobernador

回答

0

最後的答案我得到了答案....

.....在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); 

     } 
    } 

.....在html頁面......

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Pie Chart</title> 
</head> 
<body> 
    <IMG SRC="piechart.png" WIDTH="600" HEIGHT="400" BORDER="0" 
     USEMAP="#chart"> 
</body> 
</html> 

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

.................. ....或者只使用jsp頁面........

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> 

<%@ 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.*"%> 

<% 

      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); 

      } 
     } 
%> 

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Pie Chart</title> 
</head> 
<body> 
    <IMG SRC="piechart.png" WIDTH="600" HEIGHT="400" BORDER="0" 
     USEMAP="#chart"> 
</body> 
</html> 
1

創建圖表保存圖表如下之後:

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

然後

使用

<IMG SRC=path/"piechart.png" WIDTH="600" HEIGHT="400" BORDER="0" 
     USEMAP="#chart"> 

**另一種方式是在** 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) 
     } 
討論

而顯示爲

<img src="/ChartDrawerServlet?chart=myDesiredChart1&width=..and other processed parameters" ..> 

看到馬丁拉扎爾here