package com.wonders.medic.common.tools;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
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.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.labels.StandardPieToolTipGenerator;
import org.jfree.chart.labels.StandardXYItemLabelGenerator;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.CombinedDomainCategoryPlot;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.urls.StandardCategoryURLGenerator;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.TextAnchor;
import org.jfree.util.Rotation;
import org.apache.commons.lang.StringUtils;
import com.wonders.medic.base.bo.PatientBaseInfo;
import com.wonders.medic.common.GlobalConstants;
public class CreateChartManager extends CategoryAxis{
/**
* 折线图和柱状图
* @param session
* @param pw
* @param chartMessage
* @return
*/
public String getBarLineChart(HttpSession session, PrintWriter pw, ChartMessage chartMessage)
{
String fileName = null;
List list=chartMessage.getDataSourceList();
if(list==null||list.size()==0)
{
return "";
}
JFreeChart chart = ChartFactory.createLineChart(chartMessage.getTitleName(),
chartMessage.getXTitle(), chartMessage.getYTitle(),
(DefaultCategoryDataset)list.get(0), PlotOrientation.VERTICAL, true, true,false);
chart.setBackgroundPaint(Color.lightGray);//设置图表背景颜色
CategoryPlot plot = chart.getCategoryPlot();
/*********设置第一条曲线展现形式*****/
LineAndShapeRenderer lineandshape = new LineAndShapeRenderer();
lineandshape.setBaseLinesVisible(true); // 点(即数据点)间有连线可见
lineandshape.setBaseShapesVisible(true);
lineandshape.setShape(new java.awt.geom.Ellipse2D.Double(-2, -2, 5,5));
lineandshape.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());// 设置鼠标移上热点显示数据
lineandshape.setStroke(new BasicStroke(1.7f));//设置曲线粗细
// 设置某坐标轴索引上数据集的显示样式
plot.setRenderer(0, lineandshape);
/*********设置第二个以柱状图展现形式*****/
DefaultCategoryDataset dataSource = (DefaultCategoryDataset) list.get(1);// 遍历数据源
BarRenderer renderer = new BarRenderer();
plot.setDataset(1, dataSource);
renderer.setBaseItemLabelsVisible(true);//设置柱子数据显示出来
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());//设置柱子数据显示出来
renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());//设置柱子鼠标移上显示数据
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.CENTER_LEFT));//设置柱子数据显示位置
renderer.setItemLabelAnchorOffset(9);// 设置数据点位置
renderer.setMaximumBarWidth(0.05);//设置柱子宽度
plot.setRenderer(1, renderer);
// 设置两个图的前后顺序 ,DatasetRenderingOrder.FORWARD表示后面的图在前者上面
// ,DatasetRenderingOrder.REVERSE表示 表示后面的图在前者后面
plot.setDatasetRenderingOrder(DatasetRenderingOrder.REVERSE);
TextTitle otherTitle = new TextTitle(chartMessage.getOtherTitle());// 设置副标题
chart.addSubtitle(otherTitle);// 设置副标题
plot.setBackgroundPaint(Color.white); // 设置墙体背景颜色
plot.setDomainGridlinesVisible(true);
plot.setDomainGridlinePaint(Color.black);// 虚线色彩 y轴
plot.setRangeGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.black);// 虚线色彩 x轴
// 对X轴做操作
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setTickLabelFont(new Font("黑体", Font.PLAIN, 13));// 设置X轴坐标上的文字
domainAxis.setLabelFont(new Font("黑体", Font.PLAIN, 15)); // 设置X轴的标题文字
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 3.0)); // 设置X轴旋转多少度
domainAxis.setMaximumCategoryLabelWidthRatio(0.2f);//
domainAxis.setLowerMargin(0.01);// 设置距离图片左端距离
domainAxis.setUpperMargin(0.01); // 设置距离图片右端距离
// 对Y轴做操作
ValueAxis rAxis = plot.getRangeAxis();
rAxis.setTickLabelFont(new Font("黑体", Font.PLAIN, 13)); // 设置Y轴坐标上的文字
rAxis.setLabelFont(new Font("黑体", Font.PLAIN, 15)); // 设置Y轴的标题文字
rAxis.setUpperMargin(0.25);// 设置最高的一个 Item 与图片顶端的距离
// rAxis.setLowerBound(-5000.00); // 设置y轴最小值
// 设置乱码字体
TextTitle textTitle = chart.getTitle(); // 设置标题字体
textTitle.setFont(new Font("黑体", Font.PLAIN, 25));
chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15)); // 图文字体
// 数据轴精度
NumberAxis na = (NumberAxis) plot.getRangeAxis();
na.setAutoRangeIncludesZero(true);
plot.setForegroundAlpha(1.0f); // //设置柱的透明度
try {
ChartRenderingInfo info = new ChartRenderingInfo(
new StandardEntityCollection());
fileName = ServletUtilities.saveChartAsPNG(chart, Integer.parseInt(chartMessage.getWidth()), Integer.parseInt(chartMessage.getHeight()), info,
session);
ChartUtilities.writeImageMap(pw, fileName, info, false);
} catch (IOException e) {
e.printStackTrace();
}
return fileName;
}
/***
* 创建平面柱状图
* @param session
* @param pw
* @param chartMessage
* @return
*/
public String getBarChart(HttpSession session, PrintWriter pw,
ChartMessage chartMessage) {
String fileName = null;
JFreeChart chart = ChartFactory.createBarChart(chartMessage
.getTitleName(), chartMessage.getXTitle(), chartMessage
.getYTitle(), chartMessage.getDefaultCategoryDataset(),
PlotOrientation.VERTICAL, true, true, false);
CategoryPlot plot = chart.getCategoryPlot();
// 设置副标题
TextTitle otherTitle = new TextTitle(chartMessage.getOtherTitle());
chart.addSubtitle(otherTitle);
chart.setBackgroundPaint(Color.lightGray);//设置图表背景颜色
plot.setBackgroundPaint(Color.white); // 设置墙体背景颜色
plot.setDomainGridlinesVisible(true);
plot.setDomainGridlinePaint(Color.black);// 虚线色彩 y轴
plot.setRangeGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.black);// 虚线色彩 x轴
plot.setForegroundAlpha(1);//设置柱状图的透明度
// 设置柱子属性
BarRenderer renderer =