知方号

知方号

JavaWeb图形报表柱状图

JavaWeb图形报表柱状图

步骤说明:1,导入log4j.jar,jfreechart-0.9.18.jar,jcommon-0.9.3.jar三个jar包

2,在某个包中写入BarChart .java类

package com.mengya.util;

import java.awt.Color;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.axis.AxisLocation;import org.jfree.chart.axis.CategoryAxis;import org.jfree.chart.axis.ValueAxis;import org.jfree.chart.entity.StandardEntityCollection;import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;import org.jfree.chart.plot.CategoryPlot;import org.jfree.chart.plot.PlotOrientation;import org.jfree.chart.renderer.BarRenderer3D;import org.jfree.chart.servlet.ServletUtilities;import org.jfree.data.DefaultCategoryDataset;

public class BarChart {

 private DefaultCategoryDataset dataset = new DefaultCategoryDataset();

 public void setValue(int value, String rowKey, String columnKey) {  dataset.setValue(value, rowKey, columnKey); }

 public String generateBarChart(String title, HttpSession session,PrintWriter pw,int width,int heigth) {  String filename = null;  try {   JFreeChart chart = ChartFactory     .createBarChart3D(title, null, null, dataset,       PlotOrientation.VERTICAL, true, false, false);   chart.setBackgroundPaint(Color.WHITE);   CategoryPlot plot = chart.getCategoryPlot();

   CategoryAxis domainAxis = plot.getDomainAxis();   //domainAxis.setVerticalCategoryLabels(false);      plot.setDomainAxis(domainAxis);

   ValueAxis rangeAxis = plot.getRangeAxis();   // 设置最高的一个 Item 与图片顶端的距离   rangeAxis.setUpperMargin(0.15);   // 设置最低的一个 Item 与图片底端的距离   rangeAxis.setLowerMargin(0.15);   plot.setRangeAxis(rangeAxis);

   BarRenderer3D renderer = new BarRenderer3D();   renderer.setBaseOutlinePaint(Color.BLACK);   // 设置 Wall 的颜色   renderer.setWallPaint(Color.gray);   // 设置每种水果代表的柱的颜色   renderer.setSeriesPaint(0, new Color(0, 0, 255));   renderer.setSeriesPaint(1, new Color(0, 100, 255));   renderer.setSeriesPaint(2, Color.GREEN);   // 设置每个地区所包含的平行柱的之间距离   renderer.setItemMargin(0.1);   // 显示每个柱的数值,并修改该数值的字体属性   //renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());      renderer.setItemLabelsVisible(true);   plot.setRenderer(renderer);

   // 设置柱的透明度   plot.setForegroundAlpha(0.6f);   // 设置地区、销量的显示位置   plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);   plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);      ChartRenderingInfo info = new ChartRenderingInfo(     new StandardEntityCollection());   filename = ServletUtilities.saveChartAsPNG(chart, width, heigth,     info, session);   ChartUtilities.writeImageMap(pw, filename, info);   pw.flush();  } catch (Exception e) {   e.printStackTrace();  }  return filename; }}

3,配置web.xml,在web.xml中添加如下内空:   DisplayChart     org.jfree.chart.servlet.DisplayChart      DisplayChart  /DisplayChart 

4,在要显示图片的jsp中添加内容:     柱形图 

   

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至lizi9903@foxmail.com举报,一经查实,本站将立刻删除。

上一篇 没有了

下一篇没有了