WEB中实现JFreeChart

转载自:http://zl19830917.blog.163.com/blog/static/30200672007311457160/

<jsp:directive.page import="org.jfree.util.Rotation"/><DIV class=code>
<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.awt.Color, 
java.awt.Font,
org.jfree.chart.ChartFactory, 
org.jfree.chart.JFreeChart,
java.io.*, 
org.jfree.chart.*,
org.jfree.chart.plot.PlotOrientation, 
org.jfree.chart.servlet.ServletUtilities, 
org.jfree.data.general.DatasetUtilities, 
org.jfree.chart.plot.CategoryPlot, 
org.jfree.chart.axis.CategoryAxis, 
org.jfree.chart.axis.ValueAxis, 
org.jfree.chart.renderer.category.BarRenderer3D, 
org.jfree.chart.labels.StandardCategoryItemLabelGenerator,
org.jfree.data.category.CategoryDataset, 
org.jfree.chart.renderer.category.BarRenderer,
org.jfree.chart.entity.*,
org.jfree.chart.urls.*,
org.jfree.data.general.*,
org.jfree.chart.plot.*,
org.jfree.chart.labels.*,
org.jfree.ui.*,
java.util.*,
java.text.*,
org.jfree.chart.axis.AxisLocation"%>
你好,我的webJFreeChart练习!<br>
<%
/////////////////生成柱状显示图以及图的map
Random r=new Random();

double[][] data = new double[][] {
{672, 766, 223, 540, 126},
{325, 521, 210, 340, 106},
{r.nextInt(300), 256, 523, 240, 526}
};
String[] rowKeys = {"苹果","梨子","葡萄"};
String[] columnKeys = {"北京","上海","广州","成都","深圳"};
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);
JFreeChart chart = ChartFactory.createBarChart3D("水果销量图统计",null,null,dataset,
PlotOrientation.VERTICAL,true,true,true);

CategoryPlot plot = chart.getCategoryPlot();
BarRenderer3D renderer = new BarRenderer3D();

//设置URL链接
renderer.setItemURLGenerator(new StandardCategoryURLGenerator("xyChart.jsp","水果","城市")); 
renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator()); 
//显示柱状的值
renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setItemLabelFont(new Font("黑体",Font.PLAIN,10));
renderer.setItemLabelAnchorOffset(10.0D);
//这是数值字体颜色
renderer.setItemLabelPaint(Color.BLACK);
//设置正数值位于柱上方
renderer.setPositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE1,TextAnchor.HALF_ASCENT_RIGHT));
//设置负数值位于柱上方
renderer.setNegativeItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE2,TextAnchor.HALF_ASCENT_RIGHT));
renderer.setItemLabelsVisible(true);
plot.setRenderer(renderer);

ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
String filename = ServletUtilities.saveChartAsJPEG(chart, 500, 300, info, session); 
PrintWriter w = new PrintWriter(out);//输出MAP信息 
ChartUtilities.writeImageMap(w, "map", info, false);
String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
%>
<img src="<%= graphURL %>" width=500 height=300 border=0 usemap="#<%=filename %>"> 
<%
DefaultPieDataset data1 = new DefaultPieDataset(); 
data1.setValue("高中以下",370); 
data1.setValue("高中",1530); 
data1.setValue("大专",5700); 
data1.setValue("本科",8280); 
data1.setValue("硕士",4420); 
data1.setValue("博士",null); 
%>
<% 
/////////////////生成饼状显示图以及图的map(添加了引出标签以及图例的显示样式)
JFreeChart chart2 = ChartFactory.createPieChart3D(
"饼型图",  // chart title
data1,  // data
true, // include legend
true,
false
);
chart2.setTitle("程序员学历情况调查表");//可选,设置图片标题 

PiePlot plot2 = (PiePlot) chart2.getPlot();
plot2.setURLGenerator(new StandardPieURLGenerator("baofeng.jsp"));//设定链接 

plot2.setNoDataMessage("无对应的数据,请重新查询。");
plot2.setNoDataMessagePaint(Color.red);
//指定图片的透明度
plot2.setForegroundAlpha(0.6f);
//设置第一个 section 的开始位置,默认是12点钟方向
//plot2.setStartAngle(180.0);

//引出标签的字体
plot2.setLabelFont(new Font("黑体",Font.PLAIN,12));

//MAP中鼠标移上的显示格式
plot2.setToolTipGenerator(new StandardPieToolTipGenerator("{0}有{1}人", new DecimalFormat("0"), new DecimalFormat("0.00%"))); 

//忽略无值的分类
plot2.setIgnoreNullValues(true);   
//饼图的旋转方向
plot2.setDirection(Rotation.ANTICLOCKWISE);

//引出标签显示样式
plot2.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} = {2}", new DecimalFormat("0"), new DecimalFormat("0.00%")));

//图例显示样式
plot2.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}", new DecimalFormat("0"), new DecimalFormat("0.00%")));

ChartRenderingInfo info2 = new ChartRenderingInfo(new StandardEntityCollection());
String filename2 = ServletUtilities.saveChartAsPNG(chart2, 500, 300, info2, session);
PrintWriter pw2 = new PrintWriter(out);
ChartUtilities.writeImageMap(pw2, filename2, info2, false);
String graphURL2 = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename2;
%>
<img src="<%= graphURL2 %>" width=500 height=300 border=0 usemap="#<%=filename2 %>"> 

<% 
/////////////////生成饼状显示图以及图的map
PiePlot3D plot1 = new PiePlot3D(data1);//3D饼图 
plot1.setURLGenerator(new StandardPieURLGenerator("barview.jsp"));//设定链接 
JFreeChart chart1 = new JFreeChart("程序员学历情况调查表",JFreeChart.DEFAULT_TITLE_FONT, plot1, true); 
chart1.setBackgroundPaint(java.awt.Color.white);//可选,设置图片背景色 
StandardEntityCollection sec1 = new StandardEntityCollection(); 
ChartRenderingInfo info1 = new ChartRenderingInfo(sec1); 
PrintWriter w1 = new PrintWriter(out);//输出MAP信息 
//500是图片长度,300是图片高度 
String filename1 = ServletUtilities.saveChartAsPNG(chart1, 500, 300, info1, session); 
ChartUtilities.writeImageMap(w1, "map1", info1, false); 

/////////////////////////需要在web.xml中配置
String graphURL1 = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename1;

%>

<img src="<%= graphURL1 %>" width=500 height=300 border=0 usemap="#<%=filename1 %>"> 

</DIV> 





(在web.xml中加入
<servlet-name>DisplayChart</servlet-name> 
<servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class> 
</servlet> 
<servlet-mapping> 
<servlet-name>DisplayChart</servlet-name> 
<url-pattern>/servlet/DisplayChart</url-pattern> 
</servlet-mapping> ) 

posted on 2011-08-01 19:11  程雨轩  阅读(1399)  评论(0编辑  收藏  举报

导航