首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

jFreechart学习-柱状图

2012-11-05 
jFreechart学习--柱状图后台的代码和前台的jsp页面结合起来,然后在web页面中显示出柱状图片后台代码:publi

jFreechart学习--柱状图

后台的代码和前台的jsp页面结合起来,然后在web页面中显示出柱状图片
后台代码:
public static JFreeChart createChart() {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);// 解决中文乱码问题CategoryPlot plot = chart.getCategoryPlot();CategoryAxis domainAxis = plot.getDomainAxis();NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();TextTitle textTitle = chart.getTitle();textTitle.setFont(new Font("黑体", Font.PLAIN, 20));domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));numberAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));numberAxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));// 设置网格背景颜色plot.setBackgroundPaint(Color.white);// 设置网格横线颜色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.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);// 默认放在左边的“销量”放到右边plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);return chart;}

?前台代码

<%@page import="easy.WebHistogram"%><%@page import="org.jfree.chart.servlet.ServletUtilities"%><%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Histogram</title></head><body><%String filename = ServletUtilities.saveChartAsJPEG(WebHistogram.createChart(), 800, 600, session);String graphURL = request.getContextPath() + "/Histogram?filename="+ filename;%><div align="center"><img alt="jFreechart学习-柱状图" src="<%=graphURL%>"></div></body></html>

?

还有web.xml 的配置

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"><display-name>Histogram</display-name><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list><servlet><servlet-name>Histogram</servlet-name><servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class></servlet><servlet-mapping><servlet-name>Histogram</servlet-name><url-pattern>/Histogram</url-pattern></servlet-mapping></web-app>

?

最后的效果


jFreechart学习-柱状图
?

热点排行