package com.gamemag.web.statistics;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.StandardEntityCollection;
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.data.category.DefaultCategoryDataset;
import org.jfree.ui.RectangleEdge;
import com.gamemag.entity.player.Tasks;
publicclass TaskBarChart {
privatestatic CategoryDataset createDataset(List taskNum,
List<Tasks> tasksList) {
String s = "合计";
DefaultCategoryDataset defaultcategorydateaset = new DefaultCategoryDataset();
for (int i = 0; i < tasksList.size(); i++) {
String t_name = tasksList.get(i).getT_name();
String num = taskNum.get(i).toString();
defaultcategorydateaset.addValue(Integer.parseInt(num), s, t_name);
}
return defaultcategorydateaset;
}
// 创建JFreeChar对象
privatestatic JFreeChart createChart(CategoryDataset categoydataset) {
JFreeChart jfreechart = ChartFactory.createBarChart("合计", // 图表标题
null,// x轴的显示标签
null, // y轴的显示标签
categoydataset,// 数据集
PlotOrientation.VERTICAL,// 图表方向:水平垂直
true, // 是否生成图例
true,// 是否生成工具
false// 是否生成URL链接
);
// 解决标题乱码
CategoryPlot categoryplot = jfreechart.getCategoryPlot();
CategoryAxis domainAxis = categoryplot.getDomainAxis();// x轴
ValueAxis numberaxis = categoryplot.getRangeAxis(); // y轴
TextTitle t = jfreechart.getTitle();
t.setFont(new Font("宋体", Font.BOLD, 16));// 标题文字
domainAxis.setLabelFont(new Font("宋体", Font.BOLD, 20));// x轴标题文字
domainAxis.setTickLabelFont(new Font("黑体", Font.BOLD, 10));// x轴坐标上文字
domainAxis.setAxisLineStroke(new BasicStroke(1.6f)); // 设置轴线粗细
numberaxis.setLabelFont(new Font("宋体", Font.BOLD, 20));// y轴标题文字
numberaxis.setTickLabelFont(new Font("黑体", Font.BOLD, 10));// y轴坐标上文字
numberaxis.setAxisLineStroke(new BasicStroke(1.6f)); // 设置轴线粗细
numberaxis.setUpperMargin(0.1);//设置最高的一个柱与图片顶端的距离(最高柱的10%)
jfreechart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 10));// 图例文字
jfreechart.getLegend().setPosition(RectangleEdge.RIGHT); // 图例居右
//jfreechart.getLegend().setBackgroundPaint(Color.gray);// 图例背景色
jfreechart.setBorderStroke(new BasicStroke(1));// 设置边框宽度
jfreechart.setBackgroundPaint(Color.white); // 设置背景颜色
BarRenderer renderer = new BarRenderer();
//renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
//renderer.setBaseItemLabelsVisible(true);//显示每个柱的数值
renderer.setShadowVisible(false);//显示阴影
renderer.setMaximumBarWidth(0.03);// 设置柱子宽度
renderer.setSeriesPaint(0, Color.blue);// 设置柱子颜色
categoryplot.setRenderer(renderer);
categoryplot.setBackgroundPaint(Color.white);
categoryplot.setDomainGridlinePaint(Color.white);
categoryplot.setDomainGridlinesVisible(true);
categoryplot.setRangeGridlinePaint(Color.black);
categoryplot.setNoDataMessage("No data available");
// categoryplot.setDomainGridlinePaint(Color.black); //设置纵坐标网格线(平分柱子)
// categoryplot.setRangeGridlinePaint(Color.blue);//设置纵坐标网格线
CategoryAxis categoryaxis = categoryplot.getDomainAxis(); // 横轴上的 Lable
categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);// 45度倾斜
return jfreechart;
}
publicstatic String generateBarChart(HttpSession session, PrintWriter out,
List taskNum, List<Tasks> tasksList) throws IOException {
String filename = null;
CategoryDataset categoryDateset = createDataset(taskNum, tasksList);
JFreeChart chart = createChart(categoryDateset);
/**
* File directory = new File(".");
* System.out.println(directory.getCanonicalPath()+"=========="+img_path);
*
* FileOutputStream fos_jpg = null; try {
* fos_jpg = new FileOutputStream("fruit.jpg");
* ChartUtilities.writeChartAsJPEG(fos_jpg, 1.0f, chart, 400,300,null);
* } finally {
* try {
* fos_jpg.close();
* } catch (Exception e) {} }
**/
ChartRenderingInfo info = new ChartRenderingInfo(
new StandardEntityCollection());
try {
filename = ServletUtilities.saveChartAsPNG(chart, 600, 320, info,
session);
ChartUtilities.writeImageMap(out, filename, info, false);
} catch (IOException e) {
e.printStackTrace();
}
out.flush();
return filename;
}
}
<!-- jfreechar 配置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>/DisplayChart</url-pattern>
</servlet-mapping>
// 调用方法
TaskBarChart bar = new TaskBarChart();
HttpSession session = ServletActionContext.getRequest().getSession();
HttpServletResponse response = ServletActionContext.getResponse();
PrintWriter out = response.getWriter();
filename = bar.generateBarChart(session, out , taskNum , tasksList);
filename = "/DisplayChart?filename=" + filename;