gpt4 book ai didi

java - 如何在jsp页面中显示饼图?

转载 作者:行者123 更新时间:2023-12-01 15:21:09 26 4
gpt4 key购买 nike

<%@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>

输出是一个空白屏幕,它没有抛出任何异常..

如何在此页面显示饼图?

提前致谢。

最佳答案

创建图表后保存图表如下:

 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 的回答 here

关于java - 如何在jsp页面中显示饼图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10908696/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com