jsp怎么做柱状图_使用jfreechart在jsp页面柱状图统计

本文介绍了如何在JSP页面中使用JFreeChart库创建柱状图。首先通过`createDataset()`方法获取数据,然后利用`createChart()`方法构建柱状图,包括设置图表标题、轴标签、网格线、颜色等细节,最后将图表输出为JPEG图像。
摘要由CSDN通过智能技术生成

/**查询获得图表需要的横从坐标数据

数据采集 (被调用)*/

private staticCategoryDataset createDataset() {

DefaultCategoryDataset localDefaultCategoryDataset= newDefaultCategoryDataset();

ListjfreeList;

jfreeList=aceshiFacade.getJfreeList();for (int i = 0; i < jfreeList.size(); i++) {

localDefaultCategoryDataset.addValue(jfreeList.get(i).getNum(),"省份协会数量分布数量统计图", jfreeList.get(i).getAreaName());

}returnlocalDefaultCategoryDataset;

}private staticJFreeChart createChart(String chartTitle, String x,

String y, CategoryDataset dataset) {//构建一个chart

JFreeChart chart = ChartFactory.createLineChart(chartTitle,//图的标题

x, //x轴标题

y, //y轴标题

dataset,//数据点

PlotOrientation.VERTICAL, true, true, false);//字体清晰

chart.setTextAntiAlias(false);//设置背景颜色

chart.setBackgroundPaint(Color.LIGHT_GRAY);//设置图标题的字体

Font font = new Font("微软雅黑", Font.BOLD, 25);

chart.getTitle().setFont(font);//设置面板字体

Font labelFont = new Font("宋体", Font.TRUETYPE_FONT, 12);//设置图示的字体

chart.getLegend().setItemFont(labelFont);

CategoryPlot categoryplot=(CategoryPlot) chart.getPlot();//x轴//分类轴网格是否可见

categoryplot.setDomainGridlinesVisible(true);//y轴//数据轴网格是否可见

categoryplot.setRangeGridlinesVisible(true);

categoryplot.setRangeGridlinePaint(Color.WHITE);//虚线色彩

categoryplot.setDomainGridlinePaint(Color.WHITE);//虚线色彩

categoryplot.setBackgroundPaint(Color.BLACK);//折线图的背景颜色//设置轴和面板之间的距离//categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));//横轴 x

CategoryAxis domainAxis =categoryplot.getDomainAxis();

domainAxis.setLabelFont(labelFont);//轴标题

domainAxis.setTickLabelFont(labelFont);//轴数值//domainAxis.setLabelPaint(Color.BLUE);//轴标题的颜色//domainAxis.setTickLabelPaint(Color.BLUE);//轴数值的颜色//横轴 lable 的位置 横轴上的 Lable 45度倾斜 DOWN_45

domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);//设置距离图片左端距离

domainAxis.setLowerMargin(0.0);//设置距离图片右端距离

domainAxis.setUpperMargin(0.0);//纵轴 y

NumberAxis numberaxis =(NumberAxis) categoryplot.getRangeAxis();

numberaxis.setLabelFont(labelFont);

numberaxis.setTickLabelFont(labelFont);

numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

numberaxis.setAutoRangeIncludesZero(true);//获得renderer 注意这里是下嗍造型到lineandshaperenderer!!

LineAndShapeRenderer lineandshaperenderer =(LineAndShapeRenderer) categoryplot

.getRenderer();

lineandshaperenderer.setBaseShapesVisible(true); //series 点(即数据点)可见

lineandshaperenderer.setBaseLinesVisible(true); //series 点(即数据点)间有连线可见//显示折点数据

lineandshaperenderer

.setBaseItemLabelGenerator(newStandardCategoryItemLabelGenerator());

lineandshaperenderer.setBaseItemLabelsVisible(true);returnchart;/*JFreeChart chart = ChartFactory.createBarChart("省份协会分布统计图",// 标题

"省份名称",// x轴

"协会数量",// y轴

dataset,// 数据

PlotOrientation.VERTICAL,// 定位,VERTICAL:垂直

true,// 是否显示图例注释(对于简单的柱状图必须是false)

false,// 是否生成工具//没用过

false);// 是否生成URL链接//没用过

//字体清晰

chart.setTextAntiAlias(false);

// 周围的背景色

chart.setBackgroundPaint(Color.lightGray);

// 设置字体,否则会显示乱码

chart.getLegend().setItemFont(new Font("宋体",Font.PLAIN,20));

Font font = new Font("幼圆", 15, 15);

Font lablefont = new Font("黑体", 25, 25);

TextTitle title = chart.getTitle();

// 设置标题字体

title.setFont(font);

CategoryPlot plot = (CategoryPlot) chart.getPlot();

// 生成图片的背景色

plot.setBackgroundPaint(Color.black);

// 行线的颜色

plot.setRangeGridlinePaint(Color.BLACK);

// 刻度字体

plot.getDomainAxis().setTickLabelFont(font);

// X轴名称字体

plot.getDomainAxis().setLabelFont(lablefont);

// LayeredBarRenderer lbr = new LayeredBarRenderer();//(BarRenderer)类:

// //void setSeriesBarWidth(int series,double width)

// 设定每个分类的宽度(注意设置不要使某分类被覆盖)

// lbr.setSeriesBarWidth(1,0.1);

// 设置显示整数

NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();

rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

// 设置上部空白

rangeAxis.setUpperMargin(0.15);

// 设置y轴名称字体

rangeAxis.setLabelFont(font);

CategoryItemRenderer renderer = plot.getRenderer();

// renderer.setDrawOutlines(true);

//是否折线数据点根据不同数据使用不同的形状

// renderer.setSeriesShapesVisible(0, true);

renderer.setSeriesItemLabelsVisible(0, Boolean.TRUE);

CategoryAxis domainAxis = plot.getDomainAxis();

domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);// 倾斜

45度

BarRenderer renderer1 = new BarRenderer();// 设置柱子的相关属性

// 设置柱子宽度

// renderer1.setMaximumBarWidth(0.9);

// renderer1.setMaximumBarWidth(0.10000000000000001D); //宽度

// 设置柱子高度

renderer1.setMinimumBarLength(0.5);

// 设置柱子边框颜色

//renderer1.setBaseOutlinePaint(Color.BLACK);

// 设置柱子边框可见

// renderer1.setDrawBarOutline(true);

// 设置每个地区所包含的平行柱的之间距离,数值越大则间隔越大,图片大小一定的

情况下会影响柱子的宽度,可以为负数

renderer1.setItemMargin(0.1);

// 是否显示阴影

renderer1.setShadowVisible(false);

// 阴影颜色

// renderer1.setShadowPaint(Color.white);

plot.setRenderer(renderer1);

plot.setBackgroundAlpha((float) 0.5); // 数据区的背景透明度(0.0~1.0)

// 设置柱的透明度

// plot.setForegroundAlpha(1.0f);

// 设置图形的宽度

// 设置图形右边的空白

// caxis.setUpperMargin(0.2);

// 设置左边的空白

// caxis.setLowerMargin(0.2);

return chart;*/}//获取JFreeChart

publicActionForward createDemoPanel(ActionMapping mapping,

ActionForm form, HttpServletRequest request,

HttpServletResponse response)throwsException {

CategoryDataset database=createDataset();

JFreeChart localJFreeChart= createChart("省份协会分布统计图", "省份名称", "协会数量",

database);

ChartUtilities.writeChartAsJPEG(response.getOutputStream(),1.0f,

localJFreeChart,1000, 500, null);return null;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值