这个模块的主要步骤就是:
前台通过struts调用后台,通过JFreeChart产生图片格式的图表,存储在某个位置,然后前台jsp再去调用图片。
来开工。
JFreeChart的简介大家请百度。
首先需要两个包,jcommon-1.0.16.jar和jfreechart-1.0.13.jar。
jfreechart(下面就简称jfc),有可能会出现中午乱码问题,我建议大家就找上面那两个包,至少不会出那些需要换包的问题。
先看看效果
生成的图片:
前台的显示效果:
后台的方法
public String statistic() {
//.....
//diagramType就是前台的那个下拉框
//可以是饼状图也可以是柱状图
if (diagramType.equals("barChart"))
getBarChart();
else
getPieChart();
//getExcel(countArray);
return SUCCESS;
}
/**
* 生成柱状图
*/
public void getBarChart() {
CategoryDataset dataset = getDataSet();// 获取数据结果集
JFreeChart chart = ChartFactory.createStackedBarChart3D("不同危险等级病人数分布", // 标题
"危险等级",// x轴显示
"病人数量(单位:位)", // y轴显示
dataset, // 数据源
PlotOrientation.VERTICAL, // 图表方向:水平,垂直
true, // 是否显示图例(对于简单的柱状图必须是false)
false,// 是否生成工具
false);// 是否生成URL链接
CategoryPlot plot = chart.getCategoryPlot();
CategoryAxis axis = plot.getDomainAxis(); // 获取X轴
ValueAxis numberAxis = plot.getRangeAxis();// 获取y轴
axis.setLowerMargin(0.06);// 设置距离图片左端距离此时为6%
axis.setUpperMargin(0.06); // 设置距离图片右端此时为6%
axis.setCategoryLabelPositionOffset(10);// 图表横轴与标签的距离(10像素)
axis.setCategoryMargin(0.2);// 横轴标签之间的距离20%
chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 12));// 设置底部中文乱码
axis.setTickLabelFont(new Font("黑体", Font.BOLD, 12));// 设置X轴坐标上的文字
axis.setLabelFont(new Font("黑体", Font.BOLD, 14));// 设置X轴的标题文字
numberAxis.setTickLabelFont(new Font("黑体", Font.BOLD, 12));// 设置y轴坐标上的文字
numberAxis.setLabelFont(new Font("黑体", Font.BOLD, 14));// 设置Y轴的标题文字
chart.getTitle().setFont(new Font("黑体", Font.BOLD, 18));// 设置标题文字
NumberAxis numberAxis1 = (NumberAxis)chart.getCategoryPlot().getRangeAxis();
numberAxis1.setAutoTickUnitSelection(false);
numberAxis1.setTickUnit(new NumberTickUnit(30D));//1为一个间隔单位
BarRenderer3D customBarRenderer = (BarRenderer3D) plot.getRenderer();
customBarRenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());//显示每个柱的数值
customBarRenderer.setBaseItemLabelsVisible(true); //柱子上数值可见
Date date = new Date();// 获取当前时间
chartTimeTag = Long.toString(date.getTime());// 获取当前时间的时间戳
String path = ServletActionContext.getServletContext().getRealPath("/")
+ "resources/chart/" + chartTimeTag + ".png";// 图片的存储路径
// 输出图片到文件
FileOutputStream fos_png = null;
try {
fos_png = new FileOutputStream(path);
ChartUtilities.writeChartAsPNG(fos_png, chart, 900, 500, null);// 存储为png图片,宽度为900,高为500
fos_png.close();
} catch (IOException e) {
e.printStackTrace();
logger.error(e.toString());
}
}
/**
* 得到生成柱状图的数据结果集,将方法tongJi()所得到的结果集中的数据依次添加到dataset中
*
* @return dataset 返回柱状图所需要的数据结果集
*/
public CategoryDataset getDataSet() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int i = 0; i < hosDegreeDataList.size(); i++) {
//hosDegreeDataList里面存放的就是数据
//hosDegreeDataList是一个list 每个element都是一组数据
//大家对比一下图片中的ss与mm 就知道addValue的三个参数的作用了
dataset.addValue(Integer.parseInt(hosDegreeDataList.get(i).get(2)
.toString()), hosDegreeDataList.get(i).get(0).toString()+"mm",
hosDegreeDataList.get(i).get(0).toString()+"ss"); //加上ss与mm 你就知道这两个参数的区别了
}
return dataset;
}
/**
* 生成饼状图
*/
public void getPieChart() {
System.out.println("create chart start!!!!!!!!!");
PieDataset dataset = getPieDataSet();// 获取数据结果集
JFreeChart chart = ChartFactory.createPieChart3D("不同危险等级病人数分布",// 图表标题
dataset,// 数据源
true,// 是否显示图例
true, true);// 是否生成工具,是否生成URL链接
PiePlot3D plot = (PiePlot3D) chart.getPlot();
// 图片中显示百分比:默认方式
plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
StandardPieToolTipGenerator.DEFAULT_TOOLTIP_FORMAT));
// 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值,
// {2} 表示所占比例 ,小数点后两位
plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
"{0}={1}({2})", NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));
// 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例
plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}"));
// 指定图片的透明度(0.0-1.0)
plot.setForegroundAlpha(1.0f);
// 指定显示的饼图上圆形(true)还椭圆形(false)
plot.setCircular(true);
// 设置图上分类标签label的字体,解决中文乱码
plot.setLabelFont(new Font("黑体", Font.PLAIN, 12));
// 设置图上分类标签label的最大宽度,相对与plot的百分比
plot.setMaximumLabelWidth(0.2);
// 设置3D饼图的Z轴高度(0.0~1.0)
plot.setDepthFactor(0.07);
// 设置起始角度,默认值为90,当为0时,起始值在3点钟方向
plot.setStartAngle(45);
// 设置图标题的字体,解决中文乱码
TextTitle textTitle = chart.getTitle();
textTitle.setFont(new Font("黑体", Font.PLAIN, 20));
// 设置背景色为白色
chart.setBackgroundPaint(Color.white);
// 设置Legend部分字体,解决中文乱码
chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
Date date = new Date();
chartTimeTag = Long.toString(date.getTime());// 获取当前的时间戳
String path = ServletActionContext.getServletContext().getRealPath("/")
+ "resources/chart/" + chartTimeTag + ".png";// 图片的存储路径
// 输出图片到文件
//+"resources/chart/abc.png";
System.out.println(path);
FileOutputStream fos_png = null;
try {
fos_png = new FileOutputStream(path);
ChartUtilities.writeChartAsPNG(fos_png, chart, 900, 500, null);
fos_png.close();
} catch (IOException e) {
e.printStackTrace();
logger.error(e.toString());
}
}
/**
* 得到生成饼状图的数据结果集:将方法tongJi()所得到的结果集中的数据依次添加到dataset中
*
* @return dataset 返回饼状图所需要的数据结果集
*/
public PieDataset getPieDataSet() {
DefaultPieDataset dataset = new DefaultPieDataset();
for (int i = 0; i < hosDegreeDataList.size(); i++) {
dataset.setValue(hosDegreeDataList.get(i).get(0).toString(), Double
.parseDouble(hosDegreeDataList.get(i).get(2).toString()));
}
return dataset;
}
返回的jsp页面部分代码如下:
<img src="../../../resources/chart/<s:property value = "chartTimeTag"/>.png"
/ width="500px">
<br>
<br>
<input type="button" class="btn_long" value="点击查看大图"
onclick="window.open('../../../resources/chart/<s:property value="chartTimeTag"/>.png')" />