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>

この出力は空白の画面です。例外はスローされません。

このページに円グラフを表示するにはどうすればよいですか?

前もって感謝します。

4

2 に答える 2

1

やっと答えが出た……。

……サーブレットで……

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>
于 2012-06-15T05:41:35.273 に答える
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" ..>

ここでMartin Lazarの答えを見てください

于 2012-06-06T05:46:55.977 に答える