importorg.apache.poi.ss.usermodel.Drawing;importorg.apache.poi.ss.usermodel.Sheet;importorg.apache.poi.xssf.streaming.SXSSFWorkbook;importorg.apache.poi.xssf.usermodel.XSSFClientAnchor;importorg.jfree.chart.ChartColor;importorg.jfree.chart.ChartFactory;importorg.jfree.chart.ChartUtilities;importorg.jfree.chart.JFreeChart;import org.jfree.chart.axis.*;importorg.jfree.chart.labels.StandardCategoryItemLabelGenerator;importorg.jfree.chart.labels.StandardPieSectionLabelGenerator;importorg.jfree.chart.plot.CategoryPlot;importorg.jfree.chart.plot.PiePlot3D;importorg.jfree.chart.plot.PlotOrientation;importorg.jfree.chart.renderer.category.BarRenderer;importorg.jfree.chart.renderer.category.LineAndShapeRenderer;importorg.jfree.chart.title.LegendTitle;importorg.jfree.chart.title.TextTitle;importorg.jfree.data.category.CategoryDataset;importorg.jfree.data.category.DefaultCategoryDataset;importorg.jfree.data.general.DefaultPieDataset;importorg.jfree.data.general.PieDataset;importorg.jfree.ui.RectangleEdge;importorg.springframework.util.CollectionUtils;importorg.springframework.util.StringUtils;importjavax.imageio.ImageIO;import java.awt.*;importjava.awt.image.BufferedImage;importjava.io.ByteArrayOutputStream;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.text.DecimalFormat;importjava.text.NumberFormat;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importjava.util.Map;public classJfreeChartUtil {//图片起始位置/左
private static short LEFT_INDEX = 1;//图片起始位置/上
private static short TOP_INDEX = 1;//图片起始位置/右
private static short RIGHT_INDEX = 12;//图片起始位置/下
private static short BOTTOM_INDEX = 15;publicJfreeChartUtil() {
}public JfreeChartUtil(short LEFT_INDEX, short TOP_INDEX, short RIGHT_INDEX, shortBOTTOM_INDEX) {this.LEFT_INDEX =LEFT_INDEX;this.TOP_INDEX =TOP_INDEX;this.RIGHT_INDEX =RIGHT_INDEX;this.BOTTOM_INDEX =BOTTOM_INDEX;
}/*** 生成饼状图*/
public void createPinChart(MappinChartDataMap, SXSSFWorkbook workbook, Drawing drawing) {
String title= StringUtils.isEmpty(pinChartDataMap.get("title").toString()) ? "" : pinChartDataMap.get("title").toString();
executePinChart(title, workbook, pieData(pinChartDataMap), drawing, LEFT_INDEX, TOP_INDEX, RIGHT_INDEX, BOTTOM_INDEX);
}/*** 生成柱状图*/
public void createBarChart(MapchartDataMap, SXSSFWorkbook workbook, Drawing drawing) {
String title= StringUtils.isEmpty(chartDataMap.get("title").toString()) ? "" : chartDataMap.get("title").toString();
executeBarChart(title, chartData(chartDataMap), workbook, drawing, LEFT_INDEX, TOP_INDEX, RIGHT_INDEX, BOTTOM_INDEX);
}/*** 生成折线图*/
public void createLineChart(List>lineChartDataList, SXSSFWorkbook workbook, Drawing drawing) {for (MapresultMap : lineChartDataList) {
String title= StringUtils.isEmpty(resultMap.get("title").toString()) ? "" : resultMap.get("title").toString();
executeLineChart(title, chartData(resultMap), workbook, drawing, LEFT_INDEX, TOP_INDEX, RIGHT_INDEX, BOTTOM_INDEX);//图片整体向右位移12单元格//左边距位移12
LEFT_INDEX += 12;//右边距位移12
RIGHT_INDEX += 12;
}
}//饼状图数据组合
public PieDataset pieData(MappinChartDataMap) {
List dataList= (List) pinChartDataMap.get("dataList");
List keyList= (List) pinChartDataMap.get("keyList");if (!CollectionUtils.isEmpty(dataList)) {if (dataList.size() ==keyList.size()) {
DefaultPieDataset dataSet= newDefaultPieDataset();for (int i = 0; i < dataList.size(); i++) {
dataSet.setValue(keyList.get(i).toString(), Integer.parseInt(dataList.get(i).toString()));
}returndataSet;
}
}return null;
}//柱状图/折线图数据组合
public DefaultCategoryDataset chartData(MapresultMap) {
List keyList = (List) resultMap.get("keyList