package com.gamemag.web.player;
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.NumberAxis;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
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 com.gamemag.entity.player.SilverRetained;
publicclass SilverLineChart {
privatestatic CategoryDataset createDataset() {
DefaultCategoryDataset dateaset = new DefaultCategoryDataset();
dateaset.addValue(672, "苹果", "北京");
dateaset.addValue(766, "苹果", "上海");
dateaset.addValue(223, "苹果", "广州");
dateaset.addValue(540, "苹果", "成都");
dateaset.addValue(126, "苹果", "深圳");
dateaset.addValue(325, "梨子", "北京");
dateaset.addValue(521, "梨子", "上海");
dateaset.addValue(210, "梨子", "广州");
dateaset.addValue(340, "梨子", "成都");
dateaset.addValue(106, "梨子", "深圳");
dateaset.addValue(332, "葡萄", "北京");
dateaset.addValue(766, "葡萄", "上海");
dateaset.addValue(523, "葡萄", "广州");
dateaset.addValue(240, "葡萄", "成都");
dateaset.addValue(526, "葡萄", "深圳");
return dateaset;
}
// 创建JFreeChar对象
privatestatic JFreeChart createChart(CategoryDataset categoydataset) {
JFreeChart jfreechart = ChartFactory.createLineChart("", null, null,
categoydataset, PlotOrientation.VERTICAL, true, true, false);
jfreechart.setTextAntiAlias(false);
jfreechart.setBackgroundPaint(Color.WHITE);
// 设置图标题的字体重新设置title
Font font = new Font("线体", Font.BOLD, 25);
TextTitle title = jfreechart.getTitle();
title.setFont(font);
jfreechart.setTitle(title);
// 设置面板字体
Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);
jfreechart.setBackgroundPaint(Color.WHITE);
CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
categoryplot.setDomainGridlinesVisible(true);// x轴 // 分类轴网格是否可见
categoryplot.setRangeGridlinesVisible(true);// y轴 //数据轴网格是否可见
categoryplot.setRangeGridlinePaint(Color.WHITE);// 虚线色彩
categoryplot.setDomainGridlinePaint(Color.WHITE);// 虚线色彩
categoryplot.setBackgroundPaint(Color.lightGray);
jfreechart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 15));// 图例文字
jfreechart.getLegend().setPosition(RectangleEdge.RIGHT); // 图例居右
// 设置轴和面板之间的距离
// categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
CategoryAxis domainAxis = categoryplot.getDomainAxis();
domainAxis.setLabelFont(labelFont);// 轴标题
domainAxis.setTickLabelFont(labelFont);// 轴数值
// domainAxis.setAxisLineStroke(new BasicStroke(1.6f)); // 设置轴线粗细
// domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
// // 横轴上的 Lable ,45度倾斜
domainAxis.setLowerMargin(0.0);// 设置距离图片左端距离
domainAxis.setUpperMargin(0.0);// 设置距离图片右端距离
NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
numberaxis.setLabelFont(labelFont);
numberaxis.setTickLabelFont(labelFont);
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
numberaxis.setAutoRangeIncludesZero(true);
// numberaxis.setAxisLineStroke(new BasicStroke(1.6f)); // 设置轴线粗细
// 获得renderer 注意这里是下嗍造型到lineandshaperenderer!!
LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot
.getRenderer();
lineandshaperenderer.setBaseShapesVisible(true); // series 点(即数据点)可见
lineandshaperenderer.setBaseLinesVisible(true); // series 点(即数据点)间有连线可见
// 获取折线对象 ,设置折线粗细
LineAndShapeRenderer renderer = (LineAndShapeRenderer) categoryplot
.getRenderer();
BasicStroke realLine = new BasicStroke(2.8f); // 设置实线
// 设置虚线
float dashes[] = { 5.0f };
BasicStroke brokenLine = new BasicStroke(8.2f, // 线条粗细
BasicStroke.CAP_ROUND, // 端点风格
BasicStroke.JOIN_ROUND, // 折点风格
8f, dashes, 3.6f);
for (int i = 0; i < categoydataset.getRowCount(); i++) {
if (i % 2 == 0)
renderer.setSeriesStroke(i, realLine); // 利用实线绘制
else
renderer.setSeriesStroke(i, brokenLine); // 利用虚线绘制
}
// 显示折点数据
// lineandshaperenderer.setBaseItemLabelGenerator(new
// StandardCategoryItemLabelGenerator());
// lineandshaperenderer.setBaseItemLabelsVisible(true);
return jfreechart;
}
publicstatic String generateLineChart(HttpSession session, PrintWriter out) throws IOException {
String filename = null;
CategoryDataset categoryDateset = createDataset();
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;
}
}
调用方式:
SilverLineChart line = new SilverLineChart();
HttpSession session = ServletActionContext.getRequest().getSession();
HttpServletResponse response = ServletActionContext.getResponse();
PrintWriter out = response.getWriter();
filename = line.generateLineChart(session, out );
filename = "/DisplayChart?filename=" + filename;
<!-- jfreechar 配置web.xml -->
<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/DisplayChart</url-pattern>
</servlet-mapping>