package org.ems.core.utils;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.OutputStream;
import java.text.DecimalFormat;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DialShape;
import org.jfree.chart.plot.MeterInterval;
import org.jfree.chart.plot.MeterPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.plot.dial.DialBackground;
import org.jfree.chart.plot.dial.DialCap;
import org.jfree.chart.plot.dial.DialPlot;
import org.jfree.chart.plot.dial.StandardDialFrame;
import org.jfree.chart.plot.dial.StandardDialRange;
import org.jfree.chart.plot.dial.StandardDialScale;
import org.jfree.chart.plot.dial.DialPointer.Pointer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.Range;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DefaultValueDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.GradientPaintTransformType;
import org.jfree.ui.StandardGradientPaintTransformer;
/**
* Util class to generate report chart.
*
* @Class: JFreeChartUtil.java
* @author: wangxiang
* @create date: 2012-01-04
*
*/
public class JFreeChartUtil {
private static Font font_12 = new Font("宋体", Font.PLAIN, 12);
private static Font font_16 = new Font("宋体", Font.PLAIN, 16);
/**
* Generate line shape chart
*
* @param os
* @param width
* @param height
* @param title
* @param xAxisLabel
* @param yAxisLabel
* @param dataset
* @param legend
* @param tooltips
* @param urls
* @throws Exception
*/
public static void generateLineShapeChart(OutputStream os, int width,
int height, String title, String xAxisLabel, String yAxisLabel,
XYDataset dataset, boolean legend, boolean tooltips, boolean urls,
double rangeTick, double startValue) throws Exception {
JFreeChart chart = ChartFactory.createXYLineChart(title, xAxisLabel,
yAxisLabel, dataset, PlotOrientation.VERTICAL, legend,
tooltips, urls);
chart.setBackgroundPaint(new Color(218, 229, 240));
XYPlot plot = chart.getXYPlot();
plot.setRangeGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.red);
NumberAxis vn = (NumberAxis) plot.getRangeAxis();
vn.setAutoRangeIncludesZero(true);
DecimalFormat df = new DecimalFormat("#0");
vn.setNumberFormatOverride(df);
vn.setAutoTickUnitSelection(false);
vn.setLabelFont(font_12);
vn.setLabel("分数");
vn.setTickUnit(new NumberTickUnit(rangeTick));
vn.setLowerBound(startValue);
ValueAxis va = plot.getDomainAxis();
va.setAutoRange(false);
va.setAutoTickUnitSelection(false);
va.setLabelFont(font_12);
va.setLabel("年份");
plot.setDomainAxis(va);
plot.setBackgroundPaint(new Color(184, 220, 254));
plot.setNoDataMessageFont(font_16);
plot.setNoDataMessage("找不到数据。");
chart.setTitle("纵向比较图表");
XYLineAndShapeRenderer lasr = (XYLineAndShapeRenderer) plot.getRenderer();
lasr.setSeriesFillPaint(0, Color.RED);
lasr.setSeriesStroke(0, new BasicStroke(3f));
ChartUtilities.writeChartAsJPEG(os, chart, width, height);
}
/**
* Generate bar shape chart
*
* @param os
* @param width
* @param height
* @param title
* @param categoryAxisLabel
* @param valueAxisLabel
* @param dataset
* @param legend
* @param tooltips
* @param urls
* @throws Exception
*/
public static void generateBarShapeChart(OutputStream os, int width,
int height, String title, String categoryAxisLabel,
String valueAxisLabel, CategoryDataset dataset, boolean legend,
boolean tooltips, boolean urls, double rangeTick, double startValue) throws Exception {
JFreeChart chart = ChartFactory.createBarChart3D(title,
categoryAxisLabel, valueAxisLabel, dataset,
PlotOrientation.VERTICAL, legend, tooltips, urls);
chart.setBackgroundPaint(new Color(218, 229, 240));
CategoryPlot plot = chart.getCategoryPlot();
plot.setRangeGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.red);
NumberAxis vn = (NumberAxis) plot.getRangeAxis();
vn.setAutoRangeIncludesZero(true);
vn.setLabelFont(font_12);
vn.setLabel("指数");
vn.setTickUnit(new NumberTickUnit(rangeTick));
vn.setLowerBound(startValue);
CategoryAxis ca = plot.getDomainAxis();
ca.setLabelFont(font_12);
ca.setLabel("年份");
plot.setBackgroundPaint(new Color(184, 220, 254));
plot.setNoDataMessageFont(font_16);
plot.setNoDataMessage("找不到数据。");
chart.setTitle("横向比较图表");
chart.getLegend().setItemFont(font_12);
ChartUtilities.writeChartAsJPEG(os, chart, width, height);
}
/**
* Generate dial plot chart
*
* @param os
* @param width
* @param height
* @param title
* @param currentValue
* @param targetValue
* @throws Exception
*/
public static void generateDialPlotShapeChart(OutputStream os, int width,
int height, String title, double currentValue, double targetValue) throws Exception {
DefaultValueDataset ds1 = new DefaultValueDataset(currentValue);
DefaultValueDataset ds2 = new DefaultValueDataset(targetValue);
DialPlot dialplot = new DialPlot();
dialplot.setView(0.0, 0.0, 1.0, 0.55);
dialplot.setDataset(0, ds1);
dialplot.setDataset(1, ds2);
StandardDialFrame standarddialframe = new StandardDialFrame();
standarddialframe.setBackgroundPaint(Color.red);
standarddialframe.setForegroundPaint(Color.green);
dialplot.setDialFrame(standarddialframe);
DialBackground dialbackground = new DialBackground(new Color(184, 220, 254));
dialbackground.setGradientPaintTransformer(
new StandardGradientPaintTransformer(
GradientPaintTransformType.VERTICAL));
dialplot.setBackground(dialbackground);
StandardDialScale standarddialscale =
new StandardDialScale(0D, 100D, 0D, 180D, 10D, 0);
standarddialscale.setFirstTickLabelVisible(false);
standarddialscale.setMajorTickIncrement(10D);
standarddialscale.setTickRadius(0.88D);
standarddialscale.setTickLabelOffset(0.14999999999999999D);
standarddialscale.setTickLabelFont(new Font("Dialog", 0, 14));
dialplot.addScale(0, standarddialscale);
StandardDialScale standarddialscale1 =
new StandardDialScale(0D, 100D, 0D, 180D, 10D, 0);
standarddialscale1.setVisible(false);
standarddialscale1.setMajorTickIncrement(10D);
standarddialscale1.setTickRadius(0.68000000000000005D);
standarddialscale1.setTickLabelOffset(0.14999999999999999D);
standarddialscale1.setTickLabelFont(new Font("Dialog", 0, 14));
dialplot.addScale(1, standarddialscale1);
StandardDialRange standarddialrange = new StandardDialRange(0D, 60D, new Color(255, 0, 0, 255));
standarddialrange.setInnerRadius(0.52000000000000002D);
standarddialrange.setOuterRadius(0.55000000000000004D);
dialplot.addLayer(standarddialrange);
StandardDialRange standarddialrange1 = new StandardDialRange(60D, 70D, new Color(255, 127, 0, 255));
standarddialrange1.setInnerRadius(0.52000000000000002D);
standarddialrange1.setOuterRadius(0.55000000000000004D);
dialplot.addLayer(standarddialrange1);
StandardDialRange standarddialrange2 = new StandardDialRange(70D, 80D, Color.YELLOW);
standarddialrange2.setInnerRadius(0.52000000000000002D);
standarddialrange2.setOuterRadius(0.55000000000000004D);
dialplot.addLayer(standarddialrange2);
StandardDialRange standarddialrange3 = new StandardDialRange(80D, 90D, new Color(172, 250, 5));
standarddialrange3.setInnerRadius(0.52000000000000002D);
standarddialrange3.setOuterRadius(0.55000000000000004D);
dialplot.addLayer(standarddialrange3);
StandardDialRange standarddialrange4 = new StandardDialRange(90D, 100D, new Color(0, 255, 0, 255));
standarddialrange4.setInnerRadius(0.52000000000000002D);
standarddialrange4.setOuterRadius(0.55000000000000004D);
dialplot.addLayer(standarddialrange4);
Pointer pointer = new Pointer(0);
pointer.setRadius(0.5D);
dialplot.addLayer(pointer);
dialplot.mapDatasetToScale(1, 1);
Pointer pointer1 = new Pointer(1);
dialplot.addLayer(pointer1);
DialCap dialcap = new DialCap();
dialcap.setRadius(0.05D);
dialplot.setCap(dialcap);
dialplot.setNoDataMessageFont(font_16);
dialplot.setNoDataMessage("找不到数据。");
JFreeChart chart = new JFreeChart(title,
JFreeChart.DEFAULT_TITLE_FONT, dialplot, true);
chart.setBackgroundPaint(new Color(218, 229, 240));
chart.setTitle("与上级单位比较图表");
ChartUtilities.writeChartAsJPEG(os, chart, width, height);
}
public static void generateAlarmDialPlotShapeChart(OutputStream os, int width,
int height, String title, double currentValue) throws Exception {
MeterPlot meterplot = new MeterPlot(new DefaultValueDataset(currentValue));
meterplot.setRange(new Range(0.0D, 100D));
meterplot.addInterval(new MeterInterval("巨警",
new Range(0.0D, 60D), Color.RED, new BasicStroke(2.0F),
new Color(255, 0, 0, 255)));
meterplot.addInterval(new MeterInterval("重警",
new Range(60D, 70D), Color.ORANGE, new BasicStroke(2.0F),
new Color(255, 127, 0, 255)));
meterplot.addInterval(new MeterInterval("中警", new Range(70D,
80D), Color.YELLOW, new BasicStroke(2.0F), new Color(255, 255,
0, 255)));
meterplot.addInterval(new MeterInterval("轻警 ", new Range(80D,
90D), new Color(172, 250, 5), new BasicStroke(2.0F), new Color(172, 250, 5, 255)));
meterplot.addInterval(new MeterInterval("无警",
new Range(90D, 100D), Color.GREEN, new BasicStroke(2.0F),
new Color(0, 255, 0, 255)));
meterplot.setNeedlePaint(Color.darkGray);
meterplot.setDialBackgroundPaint(Color.white);
meterplot.setDialOutlinePaint(Color.gray);
meterplot.setDialShape(DialShape.CHORD);
meterplot.setMeterAngle(260);
meterplot.setTickLabelsVisible(true);
meterplot.setTickLabelFont(new Font(" Dialog ", 1, 10));
meterplot.setTickLabelPaint(Color.darkGray);
meterplot.setTickSize(10D);
meterplot.setTickPaint(Color.lightGray);
meterplot.setValuePaint(Color.black);
meterplot.setValueFont(new Font(" Dialog ", 1, 14));
meterplot.setNoDataMessageFont(font_16);
meterplot.setNoDataMessage("找不到数据。");
JFreeChart chart = new JFreeChart(title,
JFreeChart.DEFAULT_TITLE_FONT, meterplot, true);
chart.setBackgroundPaint(new Color(218, 229, 240));
chart.setTitle("预警图表");
ChartUtilities.writeChartAsJPEG(os, chart, width, height);
}
}