From b871a8de6e0c46887d843717d642f67f8d3cfa8b Mon Sep 17 00:00:00 2001 From: xfworld Date: Thu, 26 May 2016 14:24:29 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=96=B0=E6=A0=BC=E5=BC=8F=E5=8C=96?= =?UTF-8?q?=E7=89=88=E6=9C=AC=EF=BC=8C=E5=B0=86=E5=8E=9F=E6=9C=89=E5=9B=BE?= =?UTF-8?q?=E5=BD=A2=E5=8C=96=E7=9A=84=E6=9C=8D=E5=8A=A1=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E5=88=B0=E9=BB=98=E8=AE=A4=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../graph/builder/ExcelChartBuildService.java | 425 +++++++++--------- .../graph/constant/ExcelGraphElementType.java | 9 +- .../excel/graph/constant/ExcelGraphType.java | 11 +- .../poi/excel/graph/entity/ExcelGraph.java | 19 +- .../excel/graph/entity/ExcelGraphDefined.java | 102 ++--- .../excel/graph/entity/ExcelGraphElement.java | 101 +++-- .../excel/graph/entity/ExcelTitleCell.java | 63 +-- 7 files changed, 366 insertions(+), 364 deletions(-) diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/graph/builder/ExcelChartBuildService.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/graph/builder/ExcelChartBuildService.java index d10ea67..bcf40f9 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/graph/builder/ExcelChartBuildService.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/graph/builder/ExcelChartBuildService.java @@ -38,218 +38,225 @@ import com.google.common.collect.Lists; * @version 1.0 * */ -public class ExcelChartBuildService { - /** - * - * @param workbook - * @param graphList - * @param build 通过实时数据行来重新计算图形定义 - * @param append - */ - public static void createExcelChart(Workbook workbook, List graphList, - Boolean build, Boolean append) { - if (workbook != null && graphList != null) { - //设定默认第一个sheet为数据项 - Sheet dataSouce = workbook.getSheetAt(0); - if (dataSouce != null) { - buildTitle(dataSouce, graphList); - - if (build) { - PoiExcelGraphDataUtil.buildGraphData(dataSouce, graphList); - } - if (append) { - buildExcelChart(dataSouce, dataSouce, graphList); - } else { - Sheet sheet = workbook.createSheet("图形界面"); - buildExcelChart(dataSouce, sheet, graphList); - } - } - - } - } - - /** - * 构建基础图形 - * @param drawing - * @param anchor - * @param dataSourceSheet - * @param graph - */ - private static void buildExcelChart(Drawing drawing, ClientAnchor anchor, Sheet dataSourceSheet, - ExcelGraph graph) { - Chart chart = drawing.createChart(anchor); - ChartLegend legend = chart.getOrCreateLegend(); - legend.setPosition(LegendPosition.TOP_RIGHT); - - ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM); +public class ExcelChartBuildService +{ + /** + * + * @param workbook + * @param graph + * @param build 通过实时数据行来重新计算图形定义 + * @param append + */ + public static void createExcelChart(Workbook workbook,List graphList,Boolean build,Boolean append) + { + if(workbook!=null&&graphList!=null){ + //设定默认第一个sheet为数据项 + Sheet dataSouce=workbook.getSheetAt(0); + if(dataSouce!=null){ + buildTitle(dataSouce,graphList); + + if(build){ + PoiExcelGraphDataUtil.buildGraphData(dataSouce, graphList); + } + if(append){ + buildExcelChart(dataSouce, dataSouce, graphList); + }else{ + Sheet sheet=workbook.createSheet("图形界面"); + buildExcelChart(dataSouce, sheet, graphList); + } + } + + } + } + + /** + * 构建基础图形 + * @param drawing + * @param anchor + * @param dataSourceSheet + * @param graph + */ + private static void buildExcelChart(Drawing drawing,ClientAnchor anchor,Sheet dataSourceSheet,ExcelGraph graph){ + Chart chart = drawing.createChart(anchor); + ChartLegend legend = chart.getOrCreateLegend(); + legend.setPosition(LegendPosition.TOP_RIGHT); + + ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM); ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT); leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); - ExcelGraphElement categoryElement = graph.getCategory(); - + ExcelGraphElement categoryElement=graph.getCategory(); + ChartDataSource categoryChart; - if (categoryElement != null - && categoryElement.getElementType() == ExcelGraphElementType.STRING_TYPE) { - categoryChart = DataSources.fromStringCellRange(dataSourceSheet, - new CellRangeAddress(categoryElement.getStartRowNum(), - categoryElement.getEndRowNum(), categoryElement.getStartColNum(), - categoryElement.getEndColNum())); - } else { - categoryChart = DataSources.fromNumericCellRange(dataSourceSheet, - new CellRangeAddress(categoryElement.getStartRowNum(), - categoryElement.getEndRowNum(), categoryElement.getStartColNum(), - categoryElement.getEndColNum())); - } - - List valueList = graph.getValueList(); - List> chartValueList = Lists.newArrayList(); - if (valueList != null && valueList.size() > 0) { - for (ExcelGraphElement ele : valueList) { - ChartDataSource source = DataSources.fromNumericCellRange(dataSourceSheet, - new CellRangeAddress(ele.getStartRowNum(), ele.getEndRowNum(), - ele.getStartColNum(), ele.getEndColNum())); - chartValueList.add(source); - } - } - - if (graph.getGraphType() == ExcelGraphType.LINE_CHART) { - LineChartData data = chart.getChartDataFactory().createLineChartData(); - buildLineChartData(data, categoryChart, chartValueList, graph.getTitle()); - chart.plot(data, bottomAxis, leftAxis); - } else { - ScatterChartData data = chart.getChartDataFactory().createScatterChartData(); - buildScatterChartData(data, categoryChart, chartValueList, graph.getTitle()); - chart.plot(data, bottomAxis, leftAxis); - } - } - - /** - * 构建多个图形对象 - * @param dataSourceSheet - * @param tragetSheet - * @param graphList - */ - private static void buildExcelChart(Sheet dataSourceSheet, Sheet tragetSheet, - List graphList) { - int len = graphList.size(); - if (len == 1) { - buildExcelChart(dataSourceSheet, tragetSheet, graphList.get(0)); - } else { - int drawStart = 0; - int drawEnd = 20; - Drawing drawing = tragetSheet.createDrawingPatriarch(); - for (int i = 0; i < len; i++) { - ExcelGraph graph = graphList.get(i); - ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, drawStart, 15, drawEnd); - buildExcelChart(drawing, anchor, dataSourceSheet, graph); - drawStart = drawStart + drawEnd; - drawEnd = drawEnd + drawEnd; - } - } - } - - /** - * 构建图形对象 - * @param dataSourceSheet - * @param tragetSheet - * @param graph - */ - private static void buildExcelChart(Sheet dataSourceSheet, Sheet tragetSheet, - ExcelGraph graph) { - Drawing drawing = tragetSheet.createDrawingPatriarch(); - ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 0, 15, 20); - buildExcelChart(drawing, anchor, dataSourceSheet, graph); - } - - /** - * 构建Title - * @param sheet - * @param graph - */ - private static void buildTitle(Sheet sheet, ExcelGraph graph) { - int cellTitleLen = graph.getTitleCell().size(); - int titleLen = graph.getTitle().size(); - if (titleLen > 0) { - - } else { - for (int i = 0; i < cellTitleLen; i++) { - ExcelTitleCell titleCell = graph.getTitleCell().get(i); - if (titleCell != null) { - graph.getTitle().add( - PoiCellUtil.getCellValue(sheet, titleCell.getRow(), titleCell.getCol())); - } - } + if(categoryElement!=null&&categoryElement.getElementType()==ExcelGraphElementType.StringType){ + categoryChart=DataSources.fromStringCellRange(dataSourceSheet, new CellRangeAddress(categoryElement.getStartRowNum(),categoryElement.getEndRowNum(),categoryElement.getStartColNum(),categoryElement.getEndColNum())); + }else{ + categoryChart=DataSources.fromNumericCellRange(dataSourceSheet, new CellRangeAddress(categoryElement.getStartRowNum(),categoryElement.getEndRowNum(),categoryElement.getStartColNum(),categoryElement.getEndColNum())); } - } - - /** - * 构建Title - * @param sheet - * @param graphList - */ - private static void buildTitle(Sheet sheet, List graphList) { - if (graphList != null && graphList.size() > 0) { - for (ExcelGraph graph : graphList) { - if (graph != null) { - buildTitle(sheet, graph); - } - } + + List valueList=graph.getValueList(); + List> chartValueList=Lists.newArrayList(); + if(valueList!=null&&valueList.size()>0){ + for(ExcelGraphElement ele:valueList){ + ChartDataSource source=DataSources.fromNumericCellRange(dataSourceSheet, new CellRangeAddress(ele.getStartRowNum(),ele.getEndRowNum(),ele.getStartColNum(),ele.getEndColNum())); + chartValueList.add(source); + } } - } - - /** - * - * @param data - * @param categoryChart - * @param chartValueList - * @param title - */ - private static void buildLineChartData(LineChartData data, ChartDataSource categoryChart, - List> chartValueList, - List title) { - if (chartValueList.size() == title.size()) { - int len = title.size(); - for (int i = 0; i < len; i++) { - data.addSerie(categoryChart, chartValueList.get(i)).setTitle(title.get(i)); - } - } else { - int i = 0; - for (ChartDataSource source : chartValueList) { - String _title = title.get(i); - if (StringUtils.isNotBlank(_title)) { - data.addSerie(categoryChart, source).setTitle(_title); - } else { - data.addSerie(categoryChart, source); - } - } - } - } - - /** - * - * @param data - * @param categoryChart - * @param chartValueList - * @param title - */ - private static void buildScatterChartData(ScatterChartData data, ChartDataSource categoryChart, - List> chartValueList, - List title) { - if (chartValueList.size() == title.size()) { - int len = title.size(); - for (int i = 0; i < len; i++) { - data.addSerie(categoryChart, chartValueList.get(i)).setTitle(title.get(i)); - } - } else { - int i = 0; - for (ChartDataSource source : chartValueList) { - String _title = title.get(i); - if (StringUtils.isNotBlank(_title)) { - data.addSerie(categoryChart, source).setTitle(_title); - } else { - data.addSerie(categoryChart, source); - } - } - } - } - + + if(graph.getGraphType()==ExcelGraphType.LineChart){ + LineChartData data = chart.getChartDataFactory().createLineChartData(); + buildLineChartData(data, categoryChart, chartValueList, graph.getTitle()); + chart.plot(data, bottomAxis, leftAxis); + } + else + { + ScatterChartData data=chart.getChartDataFactory().createScatterChartData(); + buildScatterChartData(data, categoryChart, chartValueList,graph.getTitle()); + chart.plot(data, bottomAxis, leftAxis); + } + } + + + + + /** + * 构建多个图形对象 + * @param dataSourceSheet + * @param tragetSheet + * @param graphList + */ + private static void buildExcelChart(Sheet dataSourceSheet,Sheet tragetSheet,List graphList){ + int len=graphList.size(); + if(len==1) + { + buildExcelChart(dataSourceSheet, tragetSheet, graphList.get(0)); + } + else + { + int drawStart=0; + int drawEnd=20; + Drawing drawing = tragetSheet.createDrawingPatriarch(); + for(int i=0;i0){ + + }else{ + for(int i=0;i graphList){ + if(graphList!=null&&graphList.size()>0){ + for(ExcelGraph graph:graphList){ + if(graph!=null) + { + buildTitle(sheet, graph); + } + } + } + } + + /** + * + * @param data + * @param categoryChart + * @param chartValueList + * @param title + */ + private static void buildLineChartData(LineChartData data,ChartDataSource categoryChart,List> chartValueList,List title){ + if(chartValueList.size()==title.size()) + { + int len=title.size(); + for(int i=0;i source:chartValueList){ + String _title=title.get(i); + if(StringUtils.isNotBlank(_title)){ + data.addSerie(categoryChart, source).setTitle(_title); + }else{ + data.addSerie(categoryChart, source); + } + } + } + } + + /** + * + * @param data + * @param categoryChart + * @param chartValueList + * @param title + */ + private static void buildScatterChartData(ScatterChartData data,ChartDataSource categoryChart,List> chartValueList,List title){ + if(chartValueList.size()==title.size()) + { + int len=title.size(); + for(int i=0;i source:chartValueList){ + String _title=title.get(i); + if(StringUtils.isNotBlank(_title)){ + data.addSerie(categoryChart, source).setTitle(_title); + }else{ + data.addSerie(categoryChart, source); + } + } + } + } + + } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/graph/constant/ExcelGraphElementType.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/graph/constant/ExcelGraphElementType.java index 13d6802..db85314 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/graph/constant/ExcelGraphElementType.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/graph/constant/ExcelGraphElementType.java @@ -9,9 +9,8 @@ package org.jeecgframework.poi.excel.graph.constant; * @version 1.0 * 定义元素类型 */ -public interface ExcelGraphElementType { - - public static final Integer STRING_TYPE = 1; - - public static final Integer NUMERIC_TYPE = 2; +public interface ExcelGraphElementType +{ + public static final Integer StringType=1; + public static final Integer NumericType=2; } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/graph/constant/ExcelGraphType.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/graph/constant/ExcelGraphType.java index 33a68f1..627797e 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/graph/constant/ExcelGraphType.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/graph/constant/ExcelGraphType.java @@ -9,10 +9,9 @@ package org.jeecgframework.poi.excel.graph.constant; * @version 1.0 * 定义图形类型 */ -public interface ExcelGraphType { - - public static final Integer LINE_CHART = 1; - - public static final Integer SCATTER_CHART = 2; - +public interface ExcelGraphType +{ + public static final Integer LineChart=1; + public static final Integer ScatterChart=2; + } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/graph/entity/ExcelGraph.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/graph/entity/ExcelGraph.java index b70f94a..169f0e2 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/graph/entity/ExcelGraph.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/graph/entity/ExcelGraph.java @@ -9,17 +9,14 @@ import java.util.List; * @author xfworld * @since 2016-1-7 * @version 1.0 - * @see org.jeecgframework.poi.excel.graph.entity.ExcelGraph + * @see com.dawnpro.core.export.excel.model.ExcelGraph * */ -public interface ExcelGraph { - public ExcelGraphElement getCategory(); - - public List getValueList(); - - public Integer getGraphType(); - - public List getTitleCell(); - - public List getTitle(); +public interface ExcelGraph +{ + public ExcelGraphElement getCategory(); + public List getValueList(); + public Integer getGraphType(); + public List getTitleCell(); + public List getTitle(); } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/graph/entity/ExcelGraphDefined.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/graph/entity/ExcelGraphDefined.java index f005197..3729c16 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/graph/entity/ExcelGraphDefined.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/graph/entity/ExcelGraphDefined.java @@ -15,56 +15,56 @@ import com.google.common.collect.Lists; * @version 1.0 * */ -public class ExcelGraphDefined implements ExcelGraph { - - private ExcelGraphElement category; - - private List valueList = Lists.newArrayList(); - - private List titleCell = Lists.newArrayList(); - - private Integer graphType = ExcelGraphType.LINE_CHART; - - private List title = Lists.newArrayList(); - - public ExcelGraphElement getCategory() { - return category; - } - - public void setCategory(ExcelGraphElement category) { - this.category = category; - } - - public List getValueList() { - return valueList; - } - - public void setValueList(List valueList) { - this.valueList = valueList; - } - - public Integer getGraphType() { - return graphType; - } - - public void setGraphType(Integer graphType) { - this.graphType = graphType; - } - - public List getTitleCell() { - return titleCell; - } - - public void setTitleCell(List titleCell) { - this.titleCell = titleCell; - } - - public List getTitle() { - return title; - } - - public void setTitle(List title) { - this.title = title; - } +public class ExcelGraphDefined implements ExcelGraph +{ + private ExcelGraphElement category; + public List valueList=Lists.newArrayList(); + public List titleCell=Lists.newArrayList(); + private Integer graphType=ExcelGraphType.LineChart; + public List title=Lists.newArrayList(); + + public ExcelGraphElement getCategory() + { + return category; + } + public void setCategory(ExcelGraphElement category) + { + this.category = category; + } + public List getValueList() + { + return valueList; + } + public void setValueList(List valueList) + { + this.valueList = valueList; + } + public Integer getGraphType() + { + return graphType; + } + public void setGraphType(Integer graphType) + { + this.graphType = graphType; + } + public List getTitleCell() + { + return titleCell; + } + public void setTitleCell(List titleCell) + { + this.titleCell = titleCell; + } + public List getTitle() + { + return title; + } + public void setTitle(List title) + { + this.title = title; + } + + + } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/graph/entity/ExcelGraphElement.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/graph/entity/ExcelGraphElement.java index 6cbd286..2fe5f36 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/graph/entity/ExcelGraphElement.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/graph/entity/ExcelGraphElement.java @@ -5,61 +5,60 @@ package org.jeecgframework.poi.excel.graph.entity; import org.jeecgframework.poi.excel.graph.constant.ExcelGraphElementType; + /** * @author xfworld * @since 2015-12-30 * @version 1.0 * */ -public class ExcelGraphElement { - - private Integer startRowNum; - - private Integer endRowNum; - - private Integer startColNum; - - private Integer endColNum; - - private Integer elementType = ExcelGraphElementType.STRING_TYPE; - - public Integer getStartRowNum() { - return startRowNum; - } - - public void setStartRowNum(Integer startRowNum) { - this.startRowNum = startRowNum; - } - - public Integer getEndRowNum() { - return endRowNum; - } - - public void setEndRowNum(Integer endRowNum) { - this.endRowNum = endRowNum; - } - - public Integer getStartColNum() { - return startColNum; - } - - public void setStartColNum(Integer startColNum) { - this.startColNum = startColNum; - } - - public Integer getEndColNum() { - return endColNum; - } - - public void setEndColNum(Integer endColNum) { - this.endColNum = endColNum; - } - - public Integer getElementType() { - return elementType; - } - - public void setElementType(Integer elementType) { - this.elementType = elementType; - } +public class ExcelGraphElement +{ + private Integer startRowNum; + private Integer endRowNum; + private Integer startColNum; + private Integer endColNum; + private Integer elementType=ExcelGraphElementType.StringType; + + + public Integer getStartRowNum() + { + return startRowNum; + } + public void setStartRowNum(Integer startRowNum) + { + this.startRowNum = startRowNum; + } + public Integer getEndRowNum() + { + return endRowNum; + } + public void setEndRowNum(Integer endRowNum) + { + this.endRowNum = endRowNum; + } + public Integer getStartColNum() + { + return startColNum; + } + public void setStartColNum(Integer startColNum) + { + this.startColNum = startColNum; + } + public Integer getEndColNum() + { + return endColNum; + } + public void setEndColNum(Integer endColNum) + { + this.endColNum = endColNum; + } + public Integer getElementType() + { + return elementType; + } + public void setElementType(Integer elementType) + { + this.elementType = elementType; + } } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/graph/entity/ExcelTitleCell.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/graph/entity/ExcelTitleCell.java index ec78f3b..8a90431 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/graph/entity/ExcelTitleCell.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/graph/entity/ExcelTitleCell.java @@ -9,35 +9,36 @@ package org.jeecgframework.poi.excel.graph.entity; * @version 1.0 * */ -public class ExcelTitleCell { - - private Integer row; - - private Integer col; - - public ExcelTitleCell() { - - } - - public ExcelTitleCell(Integer row, Integer col) { - this.row = row; - this.col = col; - } - - public Integer getRow() { - return row; - } - - public void setRow(Integer row) { - this.row = row; - } - - public Integer getCol() { - return col; - } - - public void setCol(Integer col) { - this.col = col; - } - +public class ExcelTitleCell +{ + private Integer row; + private Integer col; + + public ExcelTitleCell(){ + + } + + public ExcelTitleCell(Integer row,Integer col){ + this.row=row; + this.col=col; + } + + public Integer getRow() + { + return row; + } + public void setRow(Integer row) + { + this.row = row; + } + public Integer getCol() + { + return col; + } + public void setCol(Integer col) + { + this.col = col; + } + + }