现在的位置: 首页 > 综合 > 正文

初级柱状图例子

2014年02月22日 ⁄ 综合 ⁄ 共 2135字 ⁄ 字号 评论关闭

 

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="org.jfree.chart.ChartFactory,org.jfree.chart.JFreeChart,org.jfree.chart.plot.PlotOrientation,org.jfree.chart.servlet.ServletUtilities,org.jfree.data.category.Catego ryDataset,org.jfree.data.general.DatasetUtilities,org.jfree.chart.plot.*,org.jfree.chart.labels.*,org.jfree.chart.renderer.category.BarRenderer3D,org.jfree.ui.*,org.jfree.chart. axis.AxisLocation,java.awt.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>Insert title here</title>
<head>
<%
double[][] data=new double[][]{{1310,1220,1110,1000},{720,700,680,640},{1130,1020,980,800},{440,400,360,300}};
String[] rowKeys={"猪肉","牛肉","鸡肉","鱼肉"};
String[] columnKeys={"广州","深圳","东莞","佛山"};
CategoryDataset dataset=DatasetUtilities.createCategoryDataset(rowKeys,columnKeys,data);
JFreeChart chart=ChartFactory.createBarChart3D("肉类销售统计图", //图表标题

"肉类", //目录轴的显示标签

"销售", //数值轴的显示标签

dataset, //数据集

PlotOrientation.VERTICAL, //图表方向

true, //是否显示图例

true, //是否生成工具

false); //是否生成URL链接

CategoryPlot plot=chart.getCategoryPlot(); //获得图表区域对象

//设置网格背景颜色
plot.setBackgroundPaint(Color.white);

//设置网格竖线颜色
plot.setDomainGridlinePaint(Color.pink);

//设置网格横线颜色
plot.setRangeGridlinePaint(Color.pink);

//显示每个柱的数值,并修改该数值的字体属性
BarRenderer3D renderer=new BarRenderer3D();
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);

//默认的数字显示在柱子中,通过如下两句可调整数字的显示
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12,TextAnchor.BASELINE_LEFT));
renderer.setItemLabelAnchorOffset(10D);

//设置每个地区所包含的平行柱的之间距离
//renderer.setItemMargin(0.3);
plot.setRenderer(renderer);

//设置地区、销售显示位置
//将下方的肉类放到上方
plot.setRangeAxisLocation(AxisLocation.TOP_OR_RIGHT);

String filename=ServletUtilities.saveChartAsPNG(chart,700,400,null,session);
String graphURL=request.getContextPath()+"/DisplayChart?filename="+filename;
%>

</head>
<body>
<img src="<%=graphURL %>" width=700 height=400 border="0" usemap="#<%=filename %>">

</body>
</html>

图片展示:

抱歉!评论已关闭.