java 饼图怎么画,一直弄不出来
来源:学生作业帮 编辑:灵鹊做题网作业帮 分类:综合作业 时间:2024/04/29 20:00:47
java 饼图怎么画,一直弄不出来
l Rent and Utilities 35%
l Transportation 15%
l Food 15%
l Education 25%
l Miscellaneous 10%
要求:饼图的每个部分要有不同的颜色.给每个部分设定一个标签,该标签出现在饼图的外围部分(使用fillarc方法画扇形图).
l Rent and Utilities 35%
l Transportation 15%
l Food 15%
l Education 25%
l Miscellaneous 10%
要求:饼图的每个部分要有不同的颜色.给每个部分设定一个标签,该标签出现在饼图的外围部分(使用fillarc方法画扇形图).
下面这个实在WEb中显示的图形,可能不太符合你的需求,那直接给你发给文件吧.这个很详细,看了看可行,就转给你了. 首先要导入所需要的包有四个
jcommon-0.9.3.jar
jdom.jar
jfreechart-0.9.18.jar
log4j.jar
第二步 配置web.xml 加入一下的代码
<servlet> <servlet-name>DisplayChart</servlet-name> <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class> </servlet> <servlet-mapping> <servlet-name>DisplayChart</servlet-name> <url-pattern>/DisplayChart</url-pattern> </servlet-mapping>
以上的配置 在做三种图的时候是一样的
第三部 代码 画饼状图
import java.awt.Color;import java.awt.Font;import java.awt.Insets;import java.io.PrintWriter;
import javax.servlet.http.HttpSession;
import org.jfree.chart.ChartFactory;import org.jfree.chart.ChartRenderingInfo;import org.jfree.chart.ChartUtilities;import org.jfree.chart.JFreeChart;import org.jfree.chart.entity.StandardEntityCollection;import org.jfree.chart.labels.StandardPieItemLabelGenerator;import org.jfree.chart.plot.PiePlot;import org.jfree.chart.servlet.ServletUtilities;import org.jfree.chart.title.TextTitle;import org.jfree.data.DefaultPieDataset;
public class Binzi{ private DefaultPieDataset data = new DefaultPieDataset(); public void setValue(String key,double value) { data.setValue(key,value);
} public String generatePieChart(String title, HttpSession session, PrintWriter pw,int wPhoto,int hPhoto) { String filename = null; try { JFreeChart chart = ChartFactory.createPieChart3D(title,data,true,true, false); //设置图片的背景色 chart.setBackgroundPaint( new java.awt.Color(189,200,255)); //设置图片标题的字体和大小 Font font = new Font("黑体", Font.CENTER_BASELINE, 20); TextTitle _title = new TextTitle(title); _title.setFont(font); chart.setTitle(_title); PiePlot plot = (PiePlot) chart.getPlot(); plot.setInsets(new Insets(5, 5, 5, 5)); //在统计图片上建连结 //plot.setURLGenerator(new StandardPieURLGenerator("link.jsp", "section")); plot.setStartAngle(270); //指定显示的饼图上圆形还椭圆形 plot.setCircular(false); //指定图片的透明度 plot.setForegroundAlpha(1.0f); // 没有数据时显示的消息 plot.setNoDataMessage("当天没有统计数据"); plot.setNoDataMessageFont(new Font("黑体", Font.CENTER_BASELINE, 20)); plot.setNoDataMessagePaint(Color.RED);
//plot.setRadius(0.70); //抽离一个 section 出来,不支持3D //plot.setExplodePercent(1, 1.00); plot.setSectionOutlinePaint(Color.BLACK);
// 显示百分比 plot.setLabelGenerator(new StandardPieItemLabelGenerator(StandardPieItemLabelGenerator.DEFAULT_TOOLTIP_FORMAT)); //设置背景的透明度 plot.setBackgroundAlpha(0.6f); //把生成的图片放到临时目录 ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection()); //500是图片长度,300是图片高度 filename = ServletUtilities.saveChartAsJPEG(chart, wPhoto, hPhoto, info,session); ChartUtilities.writeImageMap(pw, filename, info); pw.flush();
} catch (Exception e) { System.out.println("Exception - " + e.toString()); e.printStackTrace(System.out); filename = "public_error_500x300.png"; } return filename; }}
以上的代码可以直接诶复制用 不做改动 调用的时候修改参数就可以了
调用
<body> <%
Binzi bz=new Binzi();//调用上面的
List list=new ArrayList(); list=(ArrayList)request.getAttribute("list");//、、获取数据源 for(int i=0;i<list.size();i++) { Vbean vb=new Vbean(); vb=(Vbean)list.get(i); bz.setValue(vb.getVname(),vb.getMount());//根据数据作图第一个参数图例 第二个参数 具体数值需要改动的只有这里 其他的地方的都可以直接复制
} String filename = bz.generatePieChart("蔬菜品种例比图", session, new PrintWriter(out), 700, 450); String url = request.getContextPath() + "/DisplayChart?filename=" + filename; %> <table align="center" border="0"> <tr> <td> <P ALIGN="CENTER"> <img src="<%=url%>" width=700 height=450 border=1 usemap="#<%=filename%>"> </p> </td> </tr> </table> </body>
jcommon-0.9.3.jar
jdom.jar
jfreechart-0.9.18.jar
log4j.jar
第二步 配置web.xml 加入一下的代码
<servlet> <servlet-name>DisplayChart</servlet-name> <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class> </servlet> <servlet-mapping> <servlet-name>DisplayChart</servlet-name> <url-pattern>/DisplayChart</url-pattern> </servlet-mapping>
以上的配置 在做三种图的时候是一样的
第三部 代码 画饼状图
import java.awt.Color;import java.awt.Font;import java.awt.Insets;import java.io.PrintWriter;
import javax.servlet.http.HttpSession;
import org.jfree.chart.ChartFactory;import org.jfree.chart.ChartRenderingInfo;import org.jfree.chart.ChartUtilities;import org.jfree.chart.JFreeChart;import org.jfree.chart.entity.StandardEntityCollection;import org.jfree.chart.labels.StandardPieItemLabelGenerator;import org.jfree.chart.plot.PiePlot;import org.jfree.chart.servlet.ServletUtilities;import org.jfree.chart.title.TextTitle;import org.jfree.data.DefaultPieDataset;
public class Binzi{ private DefaultPieDataset data = new DefaultPieDataset(); public void setValue(String key,double value) { data.setValue(key,value);
} public String generatePieChart(String title, HttpSession session, PrintWriter pw,int wPhoto,int hPhoto) { String filename = null; try { JFreeChart chart = ChartFactory.createPieChart3D(title,data,true,true, false); //设置图片的背景色 chart.setBackgroundPaint( new java.awt.Color(189,200,255)); //设置图片标题的字体和大小 Font font = new Font("黑体", Font.CENTER_BASELINE, 20); TextTitle _title = new TextTitle(title); _title.setFont(font); chart.setTitle(_title); PiePlot plot = (PiePlot) chart.getPlot(); plot.setInsets(new Insets(5, 5, 5, 5)); //在统计图片上建连结 //plot.setURLGenerator(new StandardPieURLGenerator("link.jsp", "section")); plot.setStartAngle(270); //指定显示的饼图上圆形还椭圆形 plot.setCircular(false); //指定图片的透明度 plot.setForegroundAlpha(1.0f); // 没有数据时显示的消息 plot.setNoDataMessage("当天没有统计数据"); plot.setNoDataMessageFont(new Font("黑体", Font.CENTER_BASELINE, 20)); plot.setNoDataMessagePaint(Color.RED);
//plot.setRadius(0.70); //抽离一个 section 出来,不支持3D //plot.setExplodePercent(1, 1.00); plot.setSectionOutlinePaint(Color.BLACK);
// 显示百分比 plot.setLabelGenerator(new StandardPieItemLabelGenerator(StandardPieItemLabelGenerator.DEFAULT_TOOLTIP_FORMAT)); //设置背景的透明度 plot.setBackgroundAlpha(0.6f); //把生成的图片放到临时目录 ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection()); //500是图片长度,300是图片高度 filename = ServletUtilities.saveChartAsJPEG(chart, wPhoto, hPhoto, info,session); ChartUtilities.writeImageMap(pw, filename, info); pw.flush();
} catch (Exception e) { System.out.println("Exception - " + e.toString()); e.printStackTrace(System.out); filename = "public_error_500x300.png"; } return filename; }}
以上的代码可以直接诶复制用 不做改动 调用的时候修改参数就可以了
调用
<body> <%
Binzi bz=new Binzi();//调用上面的
List list=new ArrayList(); list=(ArrayList)request.getAttribute("list");//、、获取数据源 for(int i=0;i<list.size();i++) { Vbean vb=new Vbean(); vb=(Vbean)list.get(i); bz.setValue(vb.getVname(),vb.getMount());//根据数据作图第一个参数图例 第二个参数 具体数值需要改动的只有这里 其他的地方的都可以直接复制
} String filename = bz.generatePieChart("蔬菜品种例比图", session, new PrintWriter(out), 700, 450); String url = request.getContextPath() + "/DisplayChart?filename=" + filename; %> <table align="center" border="0"> <tr> <td> <P ALIGN="CENTER"> <img src="<%=url%>" width=700 height=450 border=1 usemap="#<%=filename%>"> </p> </td> </tr> </table> </body>