package charts;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
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.annotations.XYPointerAnnotation;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickMarkPosition;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.time.Day;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.Year;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.ui.TextAnchor;
public class PointerBarChart
{/**
* 构造报表的结果集
* @return
*/
private static IntervalXYDataset createDataset()
{
TimeSeriesCollection dataset = new TimeSeriesCollection();
TimeSeries series = new TimeSeries("客户数量",Month.class);
series.add(new Month(12,new Year(2006)), 30000);
series.add(new Month(6,new Year(2007)), 41940);
dataset.addSeries(series);
return dataset;
}
/**
* 根据结果集构造JFreechart报表对象
* @param xyDataSet
* @return
*/
private static JFreeChart createChart(IntervalXYDataset xyDataSet)
{
JFreeChart jfreechart = ChartFactory.createXYBarChart(
"工商银行现金管理客户数量", //报表标题
"时间", //x轴标签
true, //是否显示日期
"数量/户", //y轴标签
xyDataSet, //数据集
PlotOrientation.VERTICAL, //柱状图的方向
true, //是否显示图例
false, //是否有工具条提示
false //是否有链接
);
jfreechart.setBackgroundPaint(Color.white);
jfreechart.addSubtitle(new TextTitle("------《2008年银行业行业报告》",
new Font("Dialog", Font.ITALIC, 10)));
XYPlot plot = (XYPlot) jfreechart.getPlot();
XYBarRenderer renderer = (XYBarRenderer) plot.getRenderer();
renderer.setMargin(0.50);
XYPointerAnnotation pointer = new XYPointerAnnotation(
new DecimalFormat("0.0%").format(41940.0 / 30000.0 - 1),
new Day(5, 6, 2007).getFirstMillisecond(), 40000.0,
0.95 * Math.PI);
pointer.setBaseRadius(290.0);
pointer.setFont(new Font("SansSerif", Font.PLAIN, 9));
pointer.setPaint(Color.blue);
pointer.setTextAnchor(TextAnchor.HALF_ASCENT_RIGHT);
plot.addAnnotation(pointer);
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
axis.setLowerMargin(0.01);
axis.setUpperMargin(0.01);
return jfreechart;
}
/**
* 生成二维柱状图的图片,返回图片文件的名称
* @param session
* @param pw
* @return
*/
public static String generateBarChart(HttpSession session, PrintWriter pw)
{
String filename = null;
IntervalXYDataset xyDataset = createDataset();
JFreeChart chart = createChart(xyDataset);
ChartRenderingInfo info = new ChartRenderingInfo(
new StandardEntityCollection());
try
{
filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info,
session);
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
ChartUtilities.writeImageMap(pw, filename, info, false);
}
catch (IOException e)
{
e.printStackTrace();
}
pw.flush();
return filename;
}
}
最近下载更多
wusiyin LV14
2022年9月15日
微信网友_5957378031800320 LV3
2022年5月18日
xcj456 LV8
2020年9月12日
15838634741 LV18
2020年7月3日
peterLi LV3
2019年12月30日
zjjhzjb LV15
2019年12月7日
wl2301821 LV7
2019年11月19日
906396135 LV7
2019年10月31日
wanglong_wang LV13
2019年8月1日
丷夏空丷 LV4
2019年5月19日
最近浏览更多
Solowen
4月16日
暂无贡献等级
fesfefe LV13
2023年11月1日
pnews88 LV8
2023年6月30日
abc562311934 LV4
2022年10月8日
wusiyin LV14
2022年9月15日
杠上炮 LV6
2022年6月28日
微信网友_5957378031800320 LV3
2022年5月18日
huzxiong
2022年2月24日
暂无贡献等级
wxh1234567 LV4
2022年1月17日
nickshen111 LV8
2021年12月13日

