-
Notifications
You must be signed in to change notification settings - Fork 824
code to create chart without reading template file in PPT, Test Example file for DOC and PPT #135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a43a529
270cba7
68382e5
a3d28fe
e01f35e
bd7c3c5
f058e97
525581d
176255e
6c218a9
c020bdf
cd04b4f
53228de
6d488ea
8b5c28a
761f3ed
a45c87b
2929f97
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| /* | ||
| * ==================================================================== | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * ==================================================================== | ||
| */ | ||
|
|
||
| package org.apache.poi.xslf.usermodel; | ||
|
|
||
| import java.awt.geom.Rectangle2D; | ||
| import java.io.BufferedReader; | ||
| import java.io.FileOutputStream; | ||
| import java.io.FileReader; | ||
| import java.io.OutputStream; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import org.apache.poi.ss.util.CellRangeAddress; | ||
| import org.apache.poi.xddf.usermodel.chart.AxisCrosses; | ||
| import org.apache.poi.xddf.usermodel.chart.AxisPosition; | ||
| import org.apache.poi.xddf.usermodel.chart.BarDirection; | ||
| import org.apache.poi.xddf.usermodel.chart.ChartTypes; | ||
| import org.apache.poi.xddf.usermodel.chart.LegendPosition; | ||
| import org.apache.poi.xddf.usermodel.chart.XDDFBarChartData; | ||
| import org.apache.poi.xddf.usermodel.chart.XDDFChart; | ||
| import org.apache.poi.xddf.usermodel.chart.XDDFChartAxis; | ||
| import org.apache.poi.xddf.usermodel.chart.XDDFChartLegend; | ||
| import org.apache.poi.xddf.usermodel.chart.XDDFDataSource; | ||
| import org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory; | ||
| import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource; | ||
| import org.apache.poi.xddf.usermodel.chart.XDDFValueAxis; | ||
| import org.apache.poi.xslf.usermodel.XMLSlideShow; | ||
| import org.apache.poi.xslf.usermodel.XSLFChart; | ||
| import org.apache.poi.xslf.usermodel.XSLFSlide; | ||
|
|
||
| /** | ||
| * Build a chart without reading template file | ||
| */ | ||
| public class AutoChartPPTExample { | ||
| private static void usage(){ | ||
| System.out.println("Usage: BarChartExample <bar-chart-data.txt>"); | ||
| System.out.println(" bar-chart-data.txt the model to set. First line is chart title, " + | ||
| "then go pairs {axis-label value}"); | ||
| } | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| if(args.length < 1) { | ||
| usage(); | ||
| return; | ||
| } | ||
|
|
||
| try (BufferedReader modelReader = new BufferedReader(new FileReader(args[0]))) { | ||
|
|
||
| String chartTitle = modelReader.readLine(); // first line is chart title | ||
| String[] series = modelReader.readLine().split(","); | ||
|
|
||
| // Category Axis Data | ||
| List<String> listLanguages = new ArrayList<>(10); | ||
|
|
||
| // Values | ||
| List<Double> listCountries = new ArrayList<>(10); | ||
| List<Double> listSpeakers = new ArrayList<>(10); | ||
|
|
||
| // set model | ||
| String ln; | ||
| while((ln = modelReader.readLine()) != null) { | ||
| String[] vals = ln.split(","); | ||
| listCountries.add(Double.valueOf(vals[0])); | ||
| listSpeakers.add(Double.valueOf(vals[1])); | ||
| listLanguages.add(vals[2]); | ||
| } | ||
|
|
||
| String[] categories = listLanguages.toArray(new String[listLanguages.size()]); | ||
| Double[] values1 = listCountries.toArray(new Double[listCountries.size()]); | ||
| Double[] values2 = listSpeakers.toArray(new Double[listSpeakers.size()]); | ||
|
|
||
| try { | ||
|
|
||
| XMLSlideShow ppt = new XMLSlideShow(); | ||
| XSLFSlide slide = ppt.createSlide(); | ||
| XSLFChart chart = ppt.createChart(); | ||
| Rectangle2D rect2D = new java.awt.Rectangle(XDDFChart.DEFAULT_X, XDDFChart.DEFAULT_Y, | ||
| XDDFChart.DEFAULT_WIDTH, XDDFChart.DEFAULT_HEIGHT); | ||
| slide.addChart(chart, rect2D); | ||
| setBarData(chart, chartTitle, series, categories, values1, values2); | ||
| // save the result | ||
| try (OutputStream out = new FileOutputStream("bar-chart-demo-output.pptx")) { | ||
| ppt.write(out); | ||
| } | ||
| } | ||
| catch(Exception e) | ||
| { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| System.out.println("Done"); | ||
| } | ||
|
|
||
| private static void setBarData(XSLFChart chart, String chartTitle, String[] series, String[] categories, Double[] values1, Double[] values2) { | ||
| // Use a category axis for the bottom axis. | ||
| XDDFChartAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM); | ||
| bottomAxis.setTitle(series[2]); | ||
| XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT); | ||
| leftAxis.setTitle(series[0]+","+series[1]); | ||
| leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); | ||
|
|
||
| final int numOfPoints = categories.length; | ||
| final String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 0, 0)); | ||
| final String valuesDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 1, 1)); | ||
| final String valuesDataRange2 = chart.formatRange(new CellRangeAddress(1, numOfPoints, 2, 2)); | ||
| final XDDFDataSource<?> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, 0); | ||
| final XDDFNumericalDataSource<? extends Number> valuesData = XDDFDataSourcesFactory.fromArray(values1, valuesDataRange, 1); | ||
| values1[6] = 16.0; // if you ever want to change the underlying data | ||
| final XDDFNumericalDataSource<? extends Number> valuesData2 = XDDFDataSourcesFactory.fromArray(values2, valuesDataRange2, 2); | ||
|
|
||
|
|
||
| XDDFBarChartData bar = (XDDFBarChartData) chart.createData(ChartTypes.BAR, bottomAxis, leftAxis); | ||
| XDDFBarChartData.Series series1 = (XDDFBarChartData.Series) bar.addSeries(categoriesData, valuesData); | ||
| series1.setTitle(series[0], chart.setSheetTitle(series[0], 1)); | ||
|
|
||
| XDDFBarChartData.Series series2 = (XDDFBarChartData.Series) bar.addSeries(categoriesData, valuesData2); | ||
| series2.setTitle(series[1], chart.setSheetTitle(series[1], 2)); | ||
|
|
||
| bar.setVaryColors(true); | ||
| bar.setBarDirection(BarDirection.COL); | ||
| chart.plot(bar); | ||
|
|
||
| XDDFChartLegend legend = chart.getOrAddLegend(); | ||
| legend.setPosition(LegendPosition.LEFT); | ||
| legend.setOverlay(false); | ||
|
|
||
| chart.setTitleText(chartTitle); | ||
| chart.setTitleOverlay(false); | ||
| } | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| /* | ||
| * ==================================================================== | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * ==================================================================== | ||
| */ | ||
|
|
||
| package org.apache.poi.xwpf.usermodel.examples; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.FileOutputStream; | ||
| import java.io.FileReader; | ||
| import java.io.OutputStream; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import org.apache.poi.ss.util.CellRangeAddress; | ||
| import org.apache.poi.xddf.usermodel.chart.AxisCrosses; | ||
| import org.apache.poi.xddf.usermodel.chart.AxisPosition; | ||
| import org.apache.poi.xddf.usermodel.chart.BarDirection; | ||
| import org.apache.poi.xddf.usermodel.chart.ChartTypes; | ||
| import org.apache.poi.xddf.usermodel.chart.LegendPosition; | ||
| import org.apache.poi.xddf.usermodel.chart.XDDFBarChartData; | ||
| import org.apache.poi.xddf.usermodel.chart.XDDFChart; | ||
| import org.apache.poi.xddf.usermodel.chart.XDDFChartAxis; | ||
| import org.apache.poi.xddf.usermodel.chart.XDDFChartLegend; | ||
| import org.apache.poi.xddf.usermodel.chart.XDDFDataSource; | ||
| import org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory; | ||
| import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource; | ||
| import org.apache.poi.xddf.usermodel.chart.XDDFValueAxis; | ||
| import org.apache.poi.xwpf.usermodel.XWPFChart; | ||
| import org.apache.poi.xwpf.usermodel.XWPFDocument; | ||
|
|
||
| /** | ||
| * Build a chart without reading template file | ||
| */ | ||
| public class AutoChartDOCExample { | ||
| private static void usage(){ | ||
| System.out.println("Usage: BarChartExample <bar-chart-data.txt>"); | ||
| System.out.println(" bar-chart-data.txt the model to set. First line is chart title, " + | ||
| "then go pairs {axis-label value}"); | ||
| } | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| if(args.length < 1) { | ||
| usage(); | ||
| return; | ||
| } | ||
|
|
||
| try (BufferedReader modelReader = new BufferedReader(new FileReader(args[0]))) { | ||
|
|
||
| String chartTitle = modelReader.readLine(); // first line is chart title | ||
| String[] series = modelReader.readLine().split(","); | ||
|
|
||
| // Category Axis Data | ||
| List<String> listLanguages = new ArrayList<>(10); | ||
|
|
||
| // Values | ||
| List<Double> listCountries = new ArrayList<>(10); | ||
| List<Double> listSpeakers = new ArrayList<>(10); | ||
|
|
||
| // set model | ||
| String ln; | ||
| while((ln = modelReader.readLine()) != null) { | ||
| String[] vals = ln.split(","); | ||
| listCountries.add(Double.valueOf(vals[0])); | ||
| listSpeakers.add(Double.valueOf(vals[1])); | ||
| listLanguages.add(vals[2]); | ||
| } | ||
|
|
||
| String[] categories = listLanguages.toArray(new String[listLanguages.size()]); | ||
| Double[] values1 = listCountries.toArray(new Double[listCountries.size()]); | ||
| Double[] values2 = listSpeakers.toArray(new Double[listSpeakers.size()]); | ||
|
|
||
| try (XWPFDocument doc = new XWPFDocument()) { | ||
| XWPFChart chart = doc.createChart(XDDFChart.DEFAULT_WIDTH, XDDFChart.DEFAULT_HEIGHT); | ||
| setBarData(chart, chartTitle, series, categories, values1, values2); | ||
| // save the result | ||
| try (OutputStream out = new FileOutputStream("bar-chart-demo-output.docx")) { | ||
| doc.write(out); | ||
| } | ||
| } | ||
| catch(Exception e) | ||
| { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| System.out.println("Done"); | ||
| } | ||
|
|
||
| private static void setBarData(XWPFChart chart, String chartTitle, String[] series, String[] categories, Double[] values1, Double[] values2) { | ||
| // Use a category axis for the bottom axis. | ||
| XDDFChartAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM); | ||
| bottomAxis.setTitle(series[2]); | ||
| XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT); | ||
| leftAxis.setTitle(series[0]+","+series[1]); | ||
| leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); | ||
|
|
||
| final int numOfPoints = categories.length; | ||
| final String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 0, 0)); | ||
| final String valuesDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 1, 1)); | ||
| final String valuesDataRange2 = chart.formatRange(new CellRangeAddress(1, numOfPoints, 2, 2)); | ||
| final XDDFDataSource<?> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, 0); | ||
| final XDDFNumericalDataSource<? extends Number> valuesData = XDDFDataSourcesFactory.fromArray(values1, valuesDataRange, 1); | ||
| values1[6] = 16.0; // if you ever want to change the underlying data | ||
| final XDDFNumericalDataSource<? extends Number> valuesData2 = XDDFDataSourcesFactory.fromArray(values2, valuesDataRange2, 2); | ||
|
|
||
|
|
||
| XDDFBarChartData bar = (XDDFBarChartData) chart.createData(ChartTypes.BAR, bottomAxis, leftAxis); | ||
| XDDFBarChartData.Series series1 = (XDDFBarChartData.Series) bar.addSeries(categoriesData, valuesData); | ||
| series1.setTitle(series[0], chart.setSheetTitle(series[0], 1)); | ||
|
|
||
| XDDFBarChartData.Series series2 = (XDDFBarChartData.Series) bar.addSeries(categoriesData, valuesData2); | ||
| series2.setTitle(series[1], chart.setSheetTitle(series[1], 2)); | ||
|
|
||
| bar.setVaryColors(true); | ||
| bar.setBarDirection(BarDirection.COL); | ||
| chart.plot(bar); | ||
|
|
||
| XDDFChartLegend legend = chart.getOrAddLegend(); | ||
| legend.setPosition(LegendPosition.LEFT); | ||
| legend.setOverlay(false); | ||
|
|
||
| chart.setTitleText(chartTitle); | ||
| chart.setTitleOverlay(false); | ||
| } | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,6 +53,7 @@ | |
| import org.apache.poi.xssf.usermodel.XSSFCell; | ||
| import org.apache.poi.xssf.usermodel.XSSFRow; | ||
| import org.apache.poi.xssf.usermodel.XSSFSheet; | ||
| import org.apache.poi.xssf.usermodel.XSSFTable; | ||
| import org.apache.poi.xssf.usermodel.XSSFWorkbook; | ||
| import org.apache.xmlbeans.XmlException; | ||
| import org.apache.xmlbeans.XmlOptions; | ||
|
|
@@ -81,6 +82,27 @@ | |
|
|
||
| @Beta | ||
| public abstract class XDDFChart extends POIXMLDocumentPart implements TextContainer { | ||
|
|
||
| /** | ||
| * default width of chart in emu | ||
| */ | ||
| public static final int DEFAULT_WIDTH = 500000; | ||
|
|
||
| /** | ||
| * default height of chart in emu | ||
| */ | ||
| public static final int DEFAULT_HEIGHT = 500000; | ||
|
|
||
| /** | ||
| * default x-coordinate of chart in emu | ||
| */ | ||
| public static final int DEFAULT_X = 10; | ||
|
|
||
| /** | ||
| * default y-coordinate value of chart in emu | ||
| */ | ||
| public static final int DEFAULT_Y = 10; | ||
|
|
||
| /** | ||
| * Underlying workbook | ||
| */ | ||
|
|
@@ -712,10 +734,29 @@ public CellReference setSheetTitle(String title, int column) { | |
| XSSFRow row = this.getRow(sheet, 0); | ||
| XSSFCell cell = this.getCell(row, column); | ||
| cell.setCellValue(title); | ||
| this.updateSheetTable(sheet.getTables().get(0).getCTTable(), title, column); | ||
|
|
||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if sheet does't have table object then first create it and then pass newly created table to update sheet table data. so for that purpose created a method GetSheetTable to return table object. |
||
| CTTable ctTable = this.getSheetTable(sheet); | ||
|
|
||
| this.updateSheetTable(ctTable, title, column); | ||
| return new CellReference(sheet.getSheetName(), 0, column, true, true); | ||
| } | ||
|
|
||
| /** | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. method which check whether sheet have table object, in case table size is 0 then create table and return that table. |
||
| * this method will check whether sheet have table | ||
| * in case table size zero then create new table and add table columns element | ||
| * @param sheet | ||
| * @return table object | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed since tag |
||
| */ | ||
| private CTTable getSheetTable(XSSFSheet sheet) { | ||
| if(sheet.getTables().size() == 0) | ||
| { | ||
| XSSFTable newTable = sheet.createTable(null); | ||
| newTable.getCTTable().addNewTableColumns(); | ||
| sheet.getTables().add(newTable); | ||
| } | ||
| return sheet.getTables().get(0).getCTTable(); | ||
| } | ||
|
|
||
| /** | ||
| * this method update column header of sheet into table | ||
| * | ||
|
|
@@ -729,7 +770,8 @@ public CellReference setSheetTitle(String title, int column) { | |
| private void updateSheetTable(CTTable ctTable, String title, int index) { | ||
| CTTableColumns tableColumnList = ctTable.getTableColumns(); | ||
| CTTableColumn column = null; | ||
| for( int i = 0; tableColumnList.getCount() < index; i++) { | ||
| int columnCount = tableColumnList.getTableColumnList().size()-1; | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of starting loop form 0 each time start it with table column size. previously each time we are adding new column from 0 inseatd from required index. |
||
| for( int i = columnCount; i < index; i++) { | ||
| column = tableColumnList.addNewTableColumn(); | ||
| column.setId(i); | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you use this?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. my bad.. :P |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved constant variable code from XWPFChart TO XDDFChart
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with the moved constants, we can't delete them from XWPFChart as it would cause issues for other users but could you set the XWPFChart values to be be based on the XDDF ones?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it...
merged required code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
merged required code.