多列柱状图生成JfreeChart

来源:互联网 发布:淘宝刷机 编辑:程序博客网 时间:2024/04/29 16:29

package com.my;import java.awt.Color;import java.awt.Font;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.jfree.chart.ChartFactory;import org.jfree.chart.JFreeChart;import org.jfree.chart.StandardChartTheme;import org.jfree.chart.axis.AxisLocation;import org.jfree.chart.labels.ItemLabelAnchor;import org.jfree.chart.labels.ItemLabelPosition;import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;import org.jfree.chart.plot.CategoryPlot;import org.jfree.chart.plot.PlotOrientation;import org.jfree.chart.renderer.category.BarRenderer3D;import org.jfree.chart.servlet.ServletUtilities;import org.jfree.data.category.CategoryDataset;import org.jfree.data.category.DefaultCategoryDataset;import org.jfree.data.general.DatasetUtilities;import org.jfree.ui.TextAnchor;public class Demo extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");HttpSession session = request.getSession();/****************** 处理乱码代码开始 *************************/// 处理汉字乱码StandardChartTheme standardChartTheme = new StandardChartTheme("gbk");// 设置标题字体standardChartTheme.setExtraLargeFont(new Font("宋书", Font.PLAIN, 15));// 设置图例的字体standardChartTheme.setRegularFont(new Font("宋书", Font.PLAIN, 15));// 设置轴向的字体standardChartTheme.setLargeFont(new Font("宋书", Font.PLAIN, 15));// 应用主题样式ChartFactory.setChartTheme(standardChartTheme);/****************** 处理乱码代码结束 *************************//* * DefaultCategoryDataset dataset = new DefaultCategoryDataset(); * dataset.addValue(100, "H1", "一月份"); dataset.addValue(10, "H2", * "二月份"); dataset.addValue(50, "H6", "三月份"); dataset.addValue(60, "H9", * "四月份"); */// ************单个柱子***************/* * ChartFactory.setChartTheme(standardChartTheme); JFreeChart chart = * ChartFactory.createBarChart3D("汽车表", "月份", "车型", dataset, * PlotOrientation.VERTICAL, false, false, false); String fileName = * ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session); */// *************多个一组类型的**************************// 初始化一个二维数组填充数据,结果会显示四列。每一列都有四组double[][] data2 = new double[][] { { 1310, 1220, 1110, 1000 },{ 720, 700, 680, 640 }, { 1130, 1020, 980, 800 },{ 440, 400, 360, 300 } };// 初始化横纵轴显示名称String[] rowKeys = { "猪肉", "牛肉", "鸡肉", "鱼肉" };String[] columnKeys = { "广州", "深圳", "北京", "上海" };// 创建datasetCategoryDataset dataset2 = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data2);JFreeChart chart2 = ChartFactory.createBarChart3D("肉类销量统计图", "类型","数量", dataset2, PlotOrientation.VERTICAL, true, true, false);CategoryPlot plot = chart2.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.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);// 将默认放在左边的“销量”放到右方plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);String filename = ServletUtilities.saveChartAsPNG(chart2, 700, 400,null, session);String graphURL = request.getContextPath()+ "/servlet/DisplayChart?filename=" + filename; // 将路径放到request对象中// 注意这里的"/servlet/DisplayChart?filename="// 是jfreechart的虚拟的存储路径。一定要与在web.xml中配置的servlet一致request.setAttribute("graphURL", graphURL); // 页面转发到result.jsprequest.getRequestDispatcher("show.jsp").forward(request, response);}}
在jsp页面:<img src="${graphURL}" width=500 height=300 border=0 />

0 0
原创粉丝点击