JfreeChart连接数据库数据显示柱状图
2013-01-04 17:50
495 查看
首先编写web.xml:
显示柱状图代码页:index.jsp:
显示结果:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>DisplayChart</servlet-name> <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class> </servlet> <servlet-mapping> <servlet-name>DisplayChart</servlet-name> <url-pattern>/DisplayChart</url-pattern> </servlet-mapping> </web-app>
显示柱状图代码页:index.jsp:
<%@ page contentType="text/html;charset=UTF-8" import="java.sql.*,xuyan.model.*"%> <%@ 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,org.jfree.chart.plot.*,org.jfree.chart.labels.*,org.jfree.chart.renderer.category.BarRenderer3D,java.awt.*,org.jfree.ui.*,org.jfree.chart.axis.AxisLocation,org.jfree.chart.title.TextTitle,org.jfree.chart.axis.CategoryAxis,org.jfree.chart.axis.NumberAxis"%> <%@ page import="org.jfree.chart.ChartFactory,org.jfree.chart.JFreeChart,org.jfree.chart.plot.PlotOrientation,org.jfree.chart.servlet.ServletUtilities,org.jfree.data.category.DefaultCategoryDataset"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% DefaultCategoryDataset dataset = new DefaultCategoryDataset(); Connection con = null; Statement st = null; ResultSet rs = null; //连接数据库 con = new ConnDB().GetConn(); st = con.createStatement(); rs = st.executeQuery("select * from AnimalNum "); while (rs.next()) { dataset.addValue(rs.getInt(3), "北京", rs.getString(2)); dataset.addValue(rs.getInt(5), "上海", rs.getString(4)); dataset.addValue(rs.getInt(7), "天津", rs.getString(6)); } JFreeChart chart = ChartFactory.createBarChart("统计图", "肉类", "销量", dataset, PlotOrientation.VERTICAL, true, true, false); //设置字体,不然会中文乱码的 Font font = new Font("宋体", Font.BOLD, 16); TextTitle title = new TextTitle("肉类销量统计图", font); chart.setTitle(title);//标题 CategoryPlot plot = chart.getCategoryPlot(); NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis(); CategoryAxis domainAxis = plot.getDomainAxis(); /*------设置X轴坐标上的文字-----------*/ domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11)); /*------设置X轴的标题文字------------*/ domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12)); /*------设置Y轴坐标上的文字-----------*/ numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12)); /*------设置Y轴的标题文字------------*/ numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12)); /*------这句代码解决了底部汉字乱码的问题-----------*/ chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12)); //由底部文字才加,不然会出错 // 以上为文字设置 //以下为背景设置 //设置网格背景颜色 plot.setBackgroundPaint(Color.white); //设置网格竖线颜色 plot.setDomainGridlinePaint(Color.pink); //设置网格横线颜色 plot.setRangeGridlinePaint(Color.pink); //显示每个柱的数值,并修改该数值的字体属性 BarRenderer3D renderer = new BarRenderer3D(); renderer .setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); renderer.setBaseItemLabelsVisible(true); //默认的数字显示在柱子中,通过如下两句可调整数字的显示 //注意:此句很关键,若无此句,那数字的显示会被覆盖,给人数字没有显示出来的问题 renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition( ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT)); renderer.setItemLabelAnchorOffset(10D); renderer.setItemLabelFont(new Font("宋体", Font.PLAIN, 12)); renderer.setItemLabelsVisible(true); //设置每个地区所包含的平行柱的之间距离 //renderer.setItemMargin(0.3); plot.setRenderer(renderer); //设置地区、销量的显示位置 //将下方的“肉类”放到上方 plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT); //将默认放在左边的“销量”放到右方 plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT); // String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session); String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename; %> <img src="<%=graphURL%>" width=600 height=600 border=0 usemap="#<%= filename %>"> </body> </html>
显示结果:
相关文章推荐
- 【数据库中间件】分布式组件 - 中间件客户端连接显示表和数据
- MFC项目连接数据库并显示读出数据
- java连接Postger数据库,并通过Jsp将数据库中数据表的在网页中显示
- Java连接HBASE数据库,创建一个表,删除一张表,修改表,输出插入,修改,数据删除,数据获取,显示表信息,过滤查询,分页查询,地理hash
- 将业务系统数据库的数据显示在页面上并且作WebPart的跨页面连接
- C#中使用GDI+实现饼状图和柱状图跟数据库联接显示数据的方法
- WPF连接数据库+显示数据到dataGrid
- Qt连接QSqlite数据库,使用QTableWidget显示数据总结
- 访问网站时只是显示主页(index.jsp),没有请求连接数据库读取数据。
- C# 数据库连接之显示数据
- C# 窗口连接数据库 显示数据
- Sharepoint 2007 BDC(业务数据目录)连接数据库,并绑定到业务数据列表上显示数据
- Java基于JDBC连接数据库及显示数据操作示例
- java 中如何在JTABLE中显示连接数据库表中的数据
- oracle 数据库的管理工具 PL/SQL_Developer 的简易使用 与Java 连接并查询显示出数据
- 使用Using System.Data.OleDb; 连接数据库,又一种动态绑定GridView显示数据。
- C#中使用GDI+实现饼状图和柱状图跟数据库联接显示数据的方法
- java连接数据库并显示数据
- 数据库的连接及数据读取,显示操作
- 訪问站点时仅仅是显示主页(index.jsp),没有请求连接数据库读取数据。