使用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_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(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页面显示。(本人新手,说的很笼统,只为记录一下。希望大家不要介意。有问题指出来,谢谢)
运行效果如下图:
转载于:https://www.cnblogs.com/shitou5e/p/6123363.html
使用jfreechart在jsp页面柱状图统计相关推荐
- 用jfreechart在JSP页面显示点状分布图+分割线(转载:http://blog.csdn.net/ami121/article/details/2450945)
JSP页面 <%@ page language="java" pageEncoding="gbk"%> <% String path = r ...
- Web在jsp页面中生成柱状图,折线图,饼状图
一.前言 在实际开发过程中,柱状图,折线图,饼状图在一些OA,ERP中是非常常见的功能,特别是需求方是业务型,数据分析型公司,下面的例子简单实现了饼状图,柱状图,折线图在jsp中生成.(ps:新手上路 ...
- JFreeChart在Struts2中实现3D柱状图统计
在Struts2中,用JFreeChart实现3D柱状图统计 下面分别用两种方式来实现: 一种是以java应用程序的方式,一种是以web项目程序的方式 需要加入的jar包有: jcommon-1.0 ...
- java统计多个线程的请求次数_Web并发页面访问量统计实现
页面访问量统计,可能在上学的时候就讲过如何简单实现,例如在servletContext中保存一个页页面访问次数,然后每访问一次加1:或者每访问一次就把操作记录保存到数据库,这样的处理方式,做做实验就算 ...
- jsp页面实现打印 .
报表在信息系统中占据了极为重要的位置,广义上讲,报表主要以多样的格式呈现和打印动态数据,动态数据主要指保存在数据库中的数据,也可以是文本中的数据.XML数据.Hibernate数据.EJB数据.自定义 ...
- jsp空白页面传html代码,echarts在HTML里测试一般,在jsp页面不显示,而且还把整个页面变成空白...
echarts在HTML里测试正常,在jsp页面不显示,而且还把整个页面变成空白 echarts在HTML里测试正常,在jsp页面不显示,而且还把整个页面变成空白,请大神帮忙,急,在线等 Create ...
- 使用该JavaBean可以将数据在JSP页面中以表格的形式显示出来 并具有动态排序 动态生成查询 自动分页功能
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 使用该类 ...
- web页面jsp页面的打印
报表在信息系统中占据了极为重要的位置,广义上讲,报表主要以多样的格式呈现和打印动态数据,动态数据主要指保存在数据库中的数据,也可以是文本中的数据.XML数据.Hibernate数据.EJB数据.自定义 ...
- javabean+servlet+JSP页面做购物网站 (附效果图+源码)
一个简单的jsp购物网站 概要 需求 用到的技术 效果图 登录页 登陆结果 购物页 消费记录页 管理用户信息和商品信息 数据库格式 踩过的坑 jdbc进行数据库操作 java动作指令结合jsp脚本 j ...
最新文章
- xubuntu18.04安装Google拼音输入法
- 子组件调用父组件方法_vue父子组件通信以及非父子组件通信的方法
- Node.js- sublime搭建node的编译环境
- 2021甘肃高考艺考成绩查询,2021甘肃艺术统考/联考成绩查询时间及入口
- one or more of the ports are invalid. Open the server editor and correct the invalid ports
- python 模糊匹配 合并_Python Pandas模糊合并/匹配重复
- JQuery 操作 radio 被坑一例
- cisco最经典书籍 下载(全CCIE们的结晶)转载资料
- python中threading模块中的Join类
- led设备驱动(s3c_led.c)
- Java特点是支持引用类型_Java四种引用类型:强、软、弱、虚
- ACWING830 单调栈
- kmplayer音轨切换(换配音)
- Excel —— 相对引用录制宏(附视频)
- 阿里云服务器部署app服务器端-流程步骤
- excel表显示为数字但是却无法进行数值运算
- NICE的Verilog代码
- Win10系统程序图标上的盾牌怎么去除?
- Html与Css样式
- K8S中的cordon、uncordon和drain node标签 节点标签