📄 jfreechartcustombar.jsp
字号:
<!--
Fig. 8.2_03: JFreeChartCustomBar.jsp
功能: JFreeChart实例3:自定义直方图
-->
<%@ page language="java" contentType="image/png;charset=GB2312"
import="java.awt.*"
import="org.jfree.chart.ChartFactory"
import="org.jfree.chart.JFreeChart"
import="org.jfree.chart.axis.CategoryAxis"
import="org.jfree.chart.axis.CategoryLabelPositions"
import="org.jfree.chart.plot.CategoryPlot"
import="org.jfree.chart.plot.PlotOrientation"
import="org.jfree.chart.renderer.category.BarRenderer"
import="org.jfree.data.category.CategoryDataset"
import="org.jfree.data.category.DefaultCategoryDataset"
import="org.jfree.chart.servlet.ServletUtilities"
import="org.jfree.chart.ChartRenderingInfo"
import="org.jfree.chart.entity.StandardEntityCollection"
%>
<%!
String bookTitle[] = {"Python", "JAVA", "C#", "Perl", "PHP"};
String category[] = {"第1周", "第2周", "第3周", "第4周" };
double bookSales;
String chartTitle = "JFreeChart实例3:自定义直方图";
// 创建数据集
public CategoryDataset createDataset()
{
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int i=0; i < bookTitle.length; i++)
{
for (int j=0; j < category.length; j++ )
{
bookSales = 1 + (Math.random() * 100);
dataset.addValue(bookSales, bookTitle[i], category[j]);
}
}
return dataset;
}
public JFreeChart createChart(CategoryDataset dataset)
{
// 创建图表对象
JFreeChart chart = ChartFactory.createBarChart
(
chartTitle, // 图表标题
"销售时间:2005年2月", // 坐标标题
"销售量", // 坐标标题
dataset, // 定义绘制数据
PlotOrientation.VERTICAL, // 直方图的方向
true, // 定义图表是否包含图例
true, // 定义图表是否包含提示
false // 定义图表是否包含URL
);
return chart;
}
%>
<%
// 创建一个 500X375 的图像
int width=500, height=375;
CategoryDataset dataset = createDataset();
JFreeChart chart = createChart(dataset);
// 开始自定义图表绘制的相关属性
// 设置图表的背景颜色
chart.setBackgroundPaint(new Color(207, 225, 235));
// 获得图表对象的引用,用于设置更多的自定义绘制属性
CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(new Color(246, 208, 146));
plot.setDomainGridlinePaint(Color.BLACK);
plot.setDomainGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.RED);
// 定义是否绘制轮廓线
BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setDrawBarOutline(true);
// 设置直方图的绘制渐进色
Color color[] = new Color[bookTitle.length];
color[0] = new Color(99,99,0);
color[1] = new Color(255,169,66);
color[2] = new Color(33,255, 66);
color[3] = new Color(33,0,255);
color[4] = new Color(255,0,66);
for (int i = 0; i < color.length; i++)
{
GradientPaint gp = new GradientPaint(0, 0, color[i].brighter(),
0, height, color[i].darker());
renderer.setSeriesPaint(i, gp);
}
// 设置横轴标题文字的旋转方向
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(
CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)
);
// 结束自定义图表绘制的相关属性
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 + -