十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
首先要有jfreechart.jar和jcommon-1.0.12.jar两个包然后在web.xml配置
创新互联建站是一家朝气蓬勃的网站建设公司。公司专注于为企业提供信息化建设解决方案。从事网站开发,网站制作,网站设计,网站模板,微信公众号开发,软件开发,小程序设计,十余年建站对塔吊租赁等多个行业,拥有丰富设计经验。
servlet
servlet-nameDisplayChart/servlet-name
servlet-classorg.jfree.chart.servlet.DisplayChart/servlet-class
/servlet
servlet-mapping
servlet-nameDisplayChart/servlet-name
url-pattern/DisplayChart/url-pattern
/servlet-mapping
最后是jsp代码:
%@ page contentType="text/html;charset=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.CategoryDataset,
org.jfree.data.general.DatasetUtilities"%
%
double[][] data = new double[][] {{1310}, {720}, {1130}, {440}};
String[] rowKeys = {"猪肉", "牛肉","鸡肉", "鱼肉"};
String[] columnKeys = {""};
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);
JFreeChart chart = ChartFactory.createBarChart3D("广州肉类销量统计图", "肉类",
"销量",
dataset,
PlotOrientation.VERTICAL,
true,
false,
false);
String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session);
String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;
%
img src="%= graphURL %"width=500 height=300 border=0 usemap="#%= filename %"
是要生成excel还是柱状图,如果是单纯的生成柱状图,建议使用插件,推荐highcharts,类似这种的。
如果要生成柱状图并且还要导出到excel,用POI或者利用Jacob技术。POI里边应该有相关的类,参考这篇
可以使用java第三方JFreeChart的api:具体
import java.awt.Color;
import java.awt.GradientPaint;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DatasetUtilities;
/**
* 柱状图测试
* @author xjh
*
*/
public class BarChartTest {
/**
* 得到2D柱状图的数据集合
*
* @return
*/
private CategoryDataset getDataset2D() {
String str1 = "First";
String str2 = "Second";
String str3 = "Third";
String str4 = "Category 1";
String str5 = "Category 2";
String str6 = "Category 3";
String str7 = "Category 4";
String str8 = "Category 5";
DefaultCategoryDataset localDefaultCategoryDataset = new DefaultCategoryDataset();
localDefaultCategoryDataset.addValue(1.0D, str1, str4);
localDefaultCategoryDataset.addValue(4.0D, str1, str5);
localDefaultCategoryDataset.addValue(3.0D, str1, str6);
localDefaultCategoryDataset.addValue(5.0D, str1, str7);
localDefaultCategoryDataset.addValue(5.0D, str1, str8);
localDefaultCategoryDataset.addValue(5.0D, str2, str4);
localDefaultCategoryDataset.addValue(7.0D, str2, str5);
localDefaultCategoryDataset.addValue(6.0D, str2, str6);
localDefaultCategoryDataset.addValue(8.0D, str2, str7);
localDefaultCategoryDataset.addValue(4.0D, str2, str8);
localDefaultCategoryDataset.addValue(4.0D, str3, str4);
localDefaultCategoryDataset.addValue(3.0D, str3, str5);
localDefaultCategoryDataset.addValue(2.0D, str3, str6);
localDefaultCategoryDataset.addValue(3.0D, str3, str7);
localDefaultCategoryDataset.addValue(6.0D, str3, str8);
return localDefaultCategoryDataset;
}
/**
* 创建2D柱状图
*
* @param paramCategoryDataset
* @return
*/
public JFreeChart createBarChart2D() {
//得到2D柱状图的数据集合
CategoryDataset paramCategoryDataset = getDataset2D();
//创建柱状图
JFreeChart localJFreeChart = ChartFactory.createBarChart(
"Bar Chart Demo", "Category", "Value", paramCategoryDataset,
PlotOrientation.VERTICAL, true, true, false);
//设置柱状图属性
localJFreeChart.setBackgroundPaint(Color.white);
//获取柱状图的标注点集合
CategoryPlot localCategoryPlot = localJFreeChart.getCategoryPlot();
//设置标注点的背景色
localCategoryPlot.setBackgroundPaint(Color.lightGray);
//设置绘制区域的方格线的颜色
localCategoryPlot.setDomainGridlinePaint(Color.white);
//是方格线可见
localCategoryPlot.setDomainGridlinesVisible(true);
//设置方格线变动绘制颜色
localCategoryPlot.setRangeGridlinePaint(Color.white);
//得到数值坐标轴
NumberAxis localNumberAxis = (NumberAxis) localCategoryPlot
.getRangeAxis();
//设置坐标轴单元
localNumberAxis.setStandardTickUnits(NumberAxis
.createIntegerTickUnits());
//得到柱状图渲染器
BarRenderer localBarRenderer = (BarRenderer) localCategoryPlot
.getRenderer();
//如果超出绘制区域边界将不可见
localBarRenderer.setDrawBarOutline(false);
//创建倾斜度对象
GradientPaint localGradientPaint1 = new GradientPaint(0.0F, 0.0F,
Color.blue, 0.0F, 0.0F, new Color(0, 0, 64));
GradientPaint localGradientPaint2 = new GradientPaint(0.0F, 0.0F,
Color.green, 0.0F, 0.0F, new Color(0, 64, 0));
GradientPaint localGradientPaint3 = new GradientPaint(0.0F, 0.0F,
Color.red, 0.0F, 0.0F, new Color(64, 0, 0));
//为渲染器设置倾斜度
localBarRenderer.setSeriesPaint(0, localGradientPaint1);
localBarRenderer.setSeriesPaint(1, localGradientPaint2);
localBarRenderer.setSeriesPaint(2, localGradientPaint3);
//旋转坐标轴
CategoryAxis localCategoryAxis = localCategoryPlot.getDomainAxis();
localCategoryAxis.setCategoryLabelPositions(CategoryLabelPositions
.createUpRotationLabelPositions(0.5235987755982988D));
return localJFreeChart;
}
/**
* 创建3D柱状图
*
* @return
*/
public JFreeChart createBarChart3D() {
// 设置坐标点
double[][] arrayOfDouble = { { 10.0D, 4.0D, 15.0D, 14.0D },
{ -5.0D, -7.0D, 14.0D, -3.0D }, { 6.0D, 17.0D, -12.0D, 7.0D },
{ 7.0D, 15.0D, 11.0D, 0.0D }, { -8.0D, -6.0D, 10.0D, -9.0D },
{ 9.0D, 8.0D, 0.0D, 6.0D }, { -10.0D, 9.0D, 7.0D, 7.0D },
{ 11.0D, 13.0D, 9.0D, 9.0D }, { -3.0D, 7.0D, 11.0D, -10.0D } };
// 创建图表的数据集合,第一个参数:横坐标的文字说明;第二个参数:纵坐标的文字说明;第三个参数:坐标数组
CategoryDataset paramCategoryDataset = DatasetUtilities
.createCategoryDataset("Series ", "Category ", arrayOfDouble);
// 创建3D图表
JFreeChart localJFreeChart = ChartFactory.createBarChart3D(
"3D Bar Chart Demo", "Category", "Value", paramCategoryDataset,
PlotOrientation.VERTICAL, true, true, false);
// 得到横坐标点集合对象
CategoryPlot localCategoryPlot = localJFreeChart.getCategoryPlot();
// 设置方格可见
localCategoryPlot.setDomainGridlinesVisible(true);
// 得到坐标轴
CategoryAxis localCategoryAxis = localCategoryPlot.getDomainAxis();
// 设置标注点位置
localCategoryAxis.setCategoryLabelPositions(CategoryLabelPositions
.createUpRotationLabelPositions(0.3926990816987241D));
// 创建3D柱状图渲染对象
BarRenderer3D localBarRenderer3D = (BarRenderer3D) localCategoryPlot
.getRenderer();
// 设置超出边界不可见
localBarRenderer3D.setDrawBarOutline(false);
// 返回图表对象
return localJFreeChart;
}
}
JFreeChart是JAVA平台上的一个开放的图表绘制类库。它完全使用JAVA语言编写,是为applications, applets, servlets 以及JSP等使用所设计。JFreeChart可生成饼图(pie charts)、柱状图(bar charts)、散点图(scatter plots)、时序图(time series)、甘特图(Gantt charts)等等多种图表,并且可以产生PNG和JPEG格式的输出,还可以与PDF和EXCEL关联。
JFreeChart的主页地址为:
在这里可以找到最新版本的JFreeChart的相关信息,如说明文档、下载连接以及示例图表等。
JFreeChart目前是最好的java图形解决方案,基本能够解决目前的图形方面的需求。
IBM文档:
Javaeye社区: