1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 使用jfreechart在jsp页面柱状图统计

使用jfreechart在jsp页面柱状图统计

时间:2020-02-29 20:41:27

相关推荐

使用jfreechart在jsp页面柱状图统计

最近做项目,做了一个报表统计柱状图。觉得挺好的,记录一下。

项目使用的strus1+spring+ibatis

具体代码如下:

以下Java代码被注释掉的是绘制柱形图。另一种是折线图,具体思路一样。

/**查询获得图表需要的横从坐标数据 数据采集 (被调用)*/ private static CategoryDataset createDataset() { DefaultCategoryDataset localDefaultCategoryDataset = new DefaultCategoryDataset(); List<Aceshi> jfreeList; jfreeList = aceshiFacade.getJfreeList(); for (int i = 0; i < jfreeList.size(); i++) { localDefaultCategoryDataset.addValue(jfreeList.get(i).getNum(), "省份协会数量分布数量统计图", jfreeList.get(i).getAreaName()); } return localDefaultCategoryDataset; } private static JFreeChart 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_45domainAxis.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(new StandardCategoryItemLabelGenerator()); lineandshaperenderer.setBaseItemLabelsVisible(true); return chart; /*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 public ActionForward createDemoPanel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { CategoryDataset database = createDataset(); JFreeChart localJFreeChart = createChart("省份协会分布统计图", "省份名称", "协会数量", database); ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 1.0f, localJFreeChart, 1000, 500, null); return null; }

jsp页面的代码如下:

<div style="height: 500px; background-color: #9f9fce;"><img src="<c:url value='/aceshi/view.do?op=createDemoPanel'/>"></div> </div>

具体各层代码不赘述了,运行过程:页面DIV图片标签请求方法 进入action层 执行createDemoPanel方法,在此方法里面依次执行数据源封装和柱状图生成的方法,最后返回一个图片地址在jsp页面显示。(本人新手,说的很笼统,只为记录一下。希望大家不要介意。有问题指出来,谢谢)

运行效果如下图:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。