最近想用柱状图表示oracle表空间的每日增长量,在网上搜索后从JFreeChat和JQuery插件之间选择了JFreeChart,终于实现,拿出与大家分享(并不完美)。
1.在web.xml中加入下面的内容:
2.将下面的3个jar加入Tomcat web项目WEB-INF/lib目录
gnujaxp.jar
jcommon-1.0.16.jar
jfreechart-1.0.13.jar
3.编写相关java类
package oak.chart;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.ui.TextAnchor;
/**
* 柱状图类 用于显示表空间每日增长量 JFreeChart版本:1.0.13 采用单例模式
*
* @author oak
*
*/
public class BarChart {
public static BarChart instance = new BarChart();
private BarChart() {
}
public static BarChart getInstance() {
return instance;
}
/**
* 取得生成图表的URL
*
* @param request
* HttpServletRequest对象
* @param session
* HttpSession对象
* @param dataset
* 用于绘制图表的CategoryDataset
* @param totalTitle
* 图表标题
* @param width
* 图表宽度
* @param height
* :图表高度
* @return:图表URL路径
* @throws IOException
*/
public String getChartURL(HttpServletRequest request, HttpSession session,
CategoryDataset dataset, String totalTitle, int width, int height)
throws IOException {
JFreeChart chart = ChartFactory.createBarChart(totalTitle, null, null,
dataset, PlotOrientation.VERTICAL, true, true, true);
// 设置属性
this.setAttribute(chart);
// 取得文件名
String filename = ServletUtilities.saveChartAsPNG(chart, width, height,
null, session);
String graphURL = request.getContextPath()
+ "/servlet/DisplayChart?filename=" + filename;
return graphURL;
}
/**
* 属性设置
*
* @param chart
*/
public void setAttribute(JFreeChart chart) {
// 设置背景颜色
chart.setBackgroundPaint(Color.white);
// 获得图表区域对象
CategoryPlot plot = chart.getCategoryPlot();
CategoryAxis domainAxis = plot.getDomainAxis();
// 显示X轴数据
domainAxis.setVisible(true);
plot.setDomainAxis(domainAxis);
ValueAxis rAxis = plot.getRangeAxis();
// 解决X轴注解乱码
Font font = new Font("宋体", Font.PLAIN, 15);
chart.getLegend().setItemFont(font);
// 设置标题文字属性
TextTitle textTitle = chart.getTitle();
textTitle.setFont(new Font("宋体", Font.PLAIN, 18));
// 设置X轴坐标文字属性
domainAxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 12));
// 设置X轴的标题文字
domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
// 设置Y轴坐标文字属性
rAxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 12));
// 设置Y轴的标题文字属性
rAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
// 设置网格背景颜色
plot.setBackgroundPaint(Color.white);
// 设置网格竖线颜色
plot.setDomainGridlinePaint(Color.decode("#799AE1"));
// 设置网格横线颜色
plot.setRangeGridlinePaint(Color.decode("#799AE1"));
plot.setBackgroundAlpha(0.9f);
// 显示每个柱的数值,并修改该数值的字体属性
BarRenderer renderer = new BarRenderer();
renderer
.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
renderer.setBaseFillPaint(Color.decode("#799AE1"));
// 设置柱状图的width
renderer.setMaximumBarWidth(0.02);
// 设置每一柱的颜色
renderer.setSeriesPaint(0, new Color(99, 150, 180));
// renderer.setSeriesPaint(1, new Color(153, 204, 255));
// renderer.setSeriesPaint(2, new Color(51, 204, 204));
// 设置数字显示属性,默认的数字显示在柱子中
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
ItemLabelAnchor.OUTSIDE12, TextAnchor.HALF_ASCENT_CENTER));
renderer.setItemLabelAnchorOffset(10D);
plot.setRenderer(renderer);
}
}
package oak.chart.dataset;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import oak.util.DBUtil;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
public class TablespaceDataset {
private static TablespaceDataset instance = new TablespaceDataset();
private TablespaceDataset() {
}
public static TablespaceDataset getInstacle() {
return instance;
}
public CategoryDataset getTablespaceDataset() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "with tablespace_temp as "
+ "("
+ "select c.ip, c.tablespace_name, c.megs_used, trunc(c.recorddate) recorddate "
+ "from checktablespace c "
+ "where c.recorddate >= "
+ "trunc(to_date('2010/05/01 00:00:00','yyyy/mm/dd hh24:mi:ss')-1) "
+ "and c.recorddate <= "
+ "trunc(to_date('2010/05/31 00:00:00','yyyy/mm/dd hh24:mi:ss')+1) "
+ "and c.ip = 'x.x.x.x' "
+ "and tablespace_name = 'tablespace_name'"
+ ")"
+ " select to_char(a.recorddate, 'dd') day ,a.megs_used - b.megs_used as increased "
+ "from tablespace_temp a, tablespace_temp b "
+ "where a.recorddate = (b.recorddate + 1) "
+ "and a.ip = b.ip "
+ "and a.tablespace_name = b.tablespace_name";
try {
conn = DBUtil.getConnection("10.x.x.x");
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
dataset
.addValue(rs.getInt("increased"),
"2010年5月 10.x.x.x tablespace_name", rs
.getString("day"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(rs);
DBUtil.close(pstmt);
DBUtil.close(conn);
}
//dataset非从数据库取得
// for(int i=0; i<30; i++) {
// dataset.addValue(i, "TBS_ERP表空间", ""+i);
// }
// for(int i=29; i>=0; i--) {
// dataset.addValue(i, "NDX_ERP", ""+i);
// }
return dataset;
}
}
4.JSP文件
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ page import="oak.chart.*"%>
<%@ page import="oak.chart.dataset.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%
String graphURL = BarChart.getInstance().getChartURL(request,
session,
TablespaceDataset.getInstacle().getTablespaceDataset(),
"表空间每日增长量(单位:M)", 850, 350);
//out.print(graphURL);
%>
<!--
-->
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/7419833/viewspace-665815/ ,如需转载,请注明出处,否则将追究法律责任。