📄 xybar.jsp
字号:
<!--
Fig. 8.2_06: XYBar.jsp
功能: JFreeChart实例6: 基于XYDataset数据集的直方图
-->
<%@ page language="java" contentType="image/png;charset=GB2312"
import="java.awt.*"
import="java.text.*"
import="org.jfree.chart.*"
import="org.jfree.chart.axis.*"
import="org.jfree.chart.servlet.*"
import="org.jfree.chart.plot.*"
import="org.jfree.chart.renderer.*"
import="org.jfree.chart.renderer.category.*"
import="org.jfree.chart.axis.*"
import="org.jfree.chart.entity.*"
import="org.jfree.chart.labels.*"
import="org.jfree.chart.title.*"
import="org.jfree.chart.renderer.xy.*"
import="org.jfree.data.*"
import="org.jfree.data.category.*"
import="org.jfree.data.time.*"
import="org.jfree.data.xy.*"
import="org.jfree.text.TextUtilities"
import="org.jfree.ui.*"
import="org.jfree.util.*"
%>
<%!
String xTitle = "统计年份";
String yTitle = "城市居民消费价格指数";
String chartTitle = "JFreeChart实例6: 基于XYDataset数据集的直方图";
// 创建数据集
public IntervalXYDataset createDataset()
{
TimeSeries t1 = new TimeSeries(chartTitle, xTitle, yTitle, Year.class);
try {
t1.add(new Year(1978), 100.7);
t1.add(new Year(1980), 107.5);
t1.add(new Year(1985), 111.9);
t1.add(new Year(1989), 116.3);
t1.add(new Year(1990), 101.3);
t1.add(new Year(1991), 105.1);
t1.add(new Year(1992), 108.6);
t1.add(new Year(1993), 116.1);
t1.add(new Year(1994), 125.0);
t1.add(new Year(1995), 116.8);
t1.add(new Year(1996), 108.8);
t1.add(new Year(1997), 103.1);
t1.add(new Year(1998), 99.4);
t1.add(new Year(1999), 98.7);
t1.add(new Year(2000), 100.8);
t1.add(new Year(2001), 100.7);
t1.add(new Year(2002), 99.0);
}
catch (Exception e) {
System.err.println(e.getMessage());
}
TimeSeriesCollection tsc = new TimeSeriesCollection(t1);
tsc.setDomainIsPointsInTime(false);
return tsc;
}
public JFreeChart createChart(IntervalXYDataset dataset)
{
JFreeChart chart = ChartFactory.createXYBarChart
(
chartTitle,
xTitle,
true,
yTitle,
dataset,
PlotOrientation.VERTICAL,
true,
false,
false
);
return chart;
}
%>
<%
// 创建一个 500X375 的图像
int width=500, height=375;
IntervalXYDataset dataset = createDataset();
JFreeChart chart = createChart(dataset);
// 开始自定义图表绘制的相关属性
// 设置图表的背景颜色
GradientPaint bgGP = new GradientPaint(0, 0, Color.YELLOW,
0, height, Color.WHITE, false);
chart.setBackgroundPaint(bgGP);
// 添加子标题
chart.addSubtitle(new TextTitle("资料来源: 中国国家统计局 " +
" http://www.stats.gov.cn/tjsj/ndsj/yearbook2003_c.pdf"));
XYPlot plot = chart.getXYPlot();
plot.setBackgroundPaint(Color.WHITE);
plot.setRangeGridlinePaint(Color.BLUE);
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
axis.setLowerMargin(0.01);
axis.setUpperMargin(0.01);
// 结束自定义图表绘制的相关属性
ChartRenderingInfo info =
new ChartRenderingInfo(new StandardEntityCollection());
// 设置图片生成格式
String fileName =
ServletUtilities.saveChartAsPNG(chart, width, height, info, session);
// 设置图片生成路径
String graphURL =
request.getContextPath() + "/servlet/DisplayChart?filename=" + fileName;
%>
<HTML>
<HEAD>
<TITLE><%=chartTitle%></TITLE>
</HEAD>
<BODY>
<P ALIGN="CENTER">
<img src="<%=graphURL %>" border="1" >
</P>
</BODY>
</HTML>
<%
/**************************************************************************
* (C) Copyright 2004-2005 by Jingkui Zhong(钟京馗) and Huan Tang(唐桓). *
* All Rights Reserved. *
* *
* DISCLAIMER: The authors of this code have used their *
* best efforts in preparing the code. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these codes. The authors *
* shall not be liable in any event for incidental or consequential *
* damages in connection with, or arising out of, the furnishing, *
* performance, or use of these programs. *
**************************************************************************/
%>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -