change chart data to experiment diverse scripts

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1842687 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Alain Béarez 2018-10-03 08:20:50 +00:00
parent f3a1ed5a45
commit e88fc19ca4
9 changed files with 88 additions and 55 deletions

View File

@ -59,26 +59,30 @@ public class BarChartDemo {
BufferedReader modelReader = new BufferedReader(new FileReader(args[1]))) { BufferedReader modelReader = new BufferedReader(new FileReader(args[1]))) {
String chartTitle = modelReader.readLine(); // first line is chart title String chartTitle = modelReader.readLine(); // first line is chart title
String[] series = modelReader.readLine().split(",");
// Category Axis Data // Category Axis Data
List<String> listCategories = new ArrayList<String>(3); List<String> listLanguages = new ArrayList<>(10);
// Values // Values
List<Double> listValues = new ArrayList<Double>(3); List<Double> listCountries = new ArrayList<>(10);
List<Double> listSpeakers = new ArrayList<>(10);
// set model // set model
String ln; String ln;
while((ln = modelReader.readLine()) != null) { while((ln = modelReader.readLine()) != null) {
String[] vals = ln.split("\\s+"); String[] vals = ln.split(",");
listCategories.add(vals[0]); listCountries.add(Double.valueOf(vals[0]));
listValues.add(Double.valueOf(vals[1])); listSpeakers.add(Double.valueOf(vals[1]));
listLanguages.add(vals[2]);
} }
String[] categories = listCategories.toArray(new String[listCategories.size()]); String[] categories = listLanguages.toArray(new String[listLanguages.size()]);
Double[] values = listValues.toArray(new Double[listValues.size()]); Double[] values1 = listCountries.toArray(new Double[listCountries.size()]);
Double[] values2 = listSpeakers.toArray(new Double[listSpeakers.size()]);
try (XMLSlideShow pptx = new XMLSlideShow(argIS)) { try (XMLSlideShow pptx = new XMLSlideShow(argIS)) {
XSLFSlide slide = pptx.getSlides().get(0); XSLFSlide slide = pptx.getSlides().get(0);
setBarData(findChart(slide), chartTitle, categories, values); setBarData(findChart(slide), chartTitle, series, categories, values1, values2);
XSLFChart chart = findChart(pptx.createSlide().importContent(slide)); XSLFChart chart = findChart(pptx.createSlide().importContent(slide));
setColumnData(chart, "Column variant"); setColumnData(chart, "Column variant");
@ -91,18 +95,22 @@ public class BarChartDemo {
} }
} }
private static void setBarData(XSLFChart chart, String chartTitle, String[] categories, Double[] values) { private static void setBarData(XSLFChart chart, String chartTitle, String[] series, String[] categories, Double[] values1, Double[] values2) {
final List<XDDFChartData> series = chart.getChartSeries(); final List<XDDFChartData> data = chart.getChartSeries();
final XDDFBarChartData bar = (XDDFBarChartData) series.get(0); final XDDFBarChartData bar = (XDDFBarChartData) data.get(0);
final int numOfPoints = categories.length; final int numOfPoints = categories.length;
final String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 0, 0)); final String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 0, 0));
final String valuesDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 1, 1)); final String valuesDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 1, 1));
final XDDFDataSource<?> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange); final String valuesDataRange2 = chart.formatRange(new CellRangeAddress(1, numOfPoints, 2, 2));
final XDDFNumericalDataSource<? extends Number> valuesData = XDDFDataSourcesFactory.fromArray(values, valuesDataRange); 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);
bar.getSeries().get(0).replaceData(categoriesData, valuesData); bar.getSeries().get(0).replaceData(categoriesData, valuesData);
bar.getSeries().get(0).setTitle(chartTitle, chart.setSheetTitle(chartTitle)); bar.addSeries(categoriesData, valuesData2);
bar.getSeries().get(0).setTitle(series[0], chart.setSheetTitle(series[0], 0));
bar.getSeries().get(1).setTitle(series[1], chart.setSheetTitle(series[1], 1));
chart.plot(bar); chart.plot(bar);
} }
@ -110,7 +118,6 @@ public class BarChartDemo {
// Series Text // Series Text
List<XDDFChartData> series = chart.getChartSeries(); List<XDDFChartData> series = chart.getChartSeries();
XDDFBarChartData bar = (XDDFBarChartData) series.get(0); XDDFBarChartData bar = (XDDFBarChartData) series.get(0);
bar.getSeries().get(0).setTitle(chartTitle, chart.setSheetTitle(chartTitle));
// in order to transform a bar chart into a column chart, you just need to change the bar direction // in order to transform a bar chart into a column chart, you just need to change the bar direction
bar.setBarDirection(BarDirection.COL); bar.setBarDirection(BarDirection.COL);

View File

@ -77,10 +77,10 @@ public class PieChartDemo {
XDDFPieChartData pie = (XDDFPieChartData) series.get(0); XDDFPieChartData pie = (XDDFPieChartData) series.get(0);
// Category Axis Data // Category Axis Data
List<String> listCategories = new ArrayList<String>(3); List<String> listCategories = new ArrayList<>(3);
// Values // Values
List<Double> listValues = new ArrayList<Double>(3); List<Double> listValues = new ArrayList<>(3);
// set model // set model
String ln; String ln;
@ -100,7 +100,7 @@ public class PieChartDemo {
XDDFPieChartData.Series firstSeries = (XDDFPieChartData.Series) pie.getSeries().get(0); XDDFPieChartData.Series firstSeries = (XDDFPieChartData.Series) pie.getSeries().get(0);
firstSeries.replaceData(categoriesData, valuesData); firstSeries.replaceData(categoriesData, valuesData);
firstSeries.setTitle(chartTitle, chart.setSheetTitle(chartTitle)); firstSeries.setTitle(chartTitle, chart.setSheetTitle(chartTitle, 0));
firstSeries.setExplosion(25); firstSeries.setExplosion(25);
chart.plot(pie); chart.plot(pie);

View File

@ -1,4 +1,12 @@
My Bar or Column Chart 10 languages with most speakers as first language
First 1.0 countries,speakers,language
Second 3.0 58,315,العربية
Third 4.0 4,243,বাংলা
38,1299,中文
118,378,English
4,260,हिन्दी
2,128,日本語
15,223,português
6,119,ਪੰਜਾਬੀ
18,154,Русский язык
31,442,español

View File

@ -61,26 +61,30 @@ public class BarChartExample {
BufferedReader modelReader = new BufferedReader(new FileReader(args[1]))) { BufferedReader modelReader = new BufferedReader(new FileReader(args[1]))) {
String chartTitle = modelReader.readLine(); // first line is chart title String chartTitle = modelReader.readLine(); // first line is chart title
String[] series = modelReader.readLine().split(",");
// Category Axis Data // Category Axis Data
List<String> listCategories = new ArrayList<String>(3); List<String> listLanguages = new ArrayList<>(10);
// Values // Values
List<Double> listValues = new ArrayList<Double>(3); List<Double> listCountries = new ArrayList<>(10);
List<Double> listSpeakers = new ArrayList<>(10);
// set model // set model
String ln; String ln;
while((ln = modelReader.readLine()) != null) { while((ln = modelReader.readLine()) != null) {
String[] vals = ln.split("\\s+"); String[] vals = ln.split(",");
listCategories.add(vals[0]); listCountries.add(Double.valueOf(vals[0]));
listValues.add(Double.valueOf(vals[1])); listSpeakers.add(Double.valueOf(vals[1]));
listLanguages.add(vals[2]);
} }
String[] categories = listCategories.toArray(new String[listCategories.size()]); String[] categories = listLanguages.toArray(new String[listLanguages.size()]);
Double[] values = listValues.toArray(new Double[listValues.size()]); Double[] values1 = listCountries.toArray(new Double[listCountries.size()]);
Double[] values2 = listSpeakers.toArray(new Double[listSpeakers.size()]);
try (XWPFDocument doc = new XWPFDocument(argIS)) { try (XWPFDocument doc = new XWPFDocument(argIS)) {
XWPFChart chart = doc.getCharts().get(0); XWPFChart chart = doc.getCharts().get(0);
setBarData(chart, chartTitle, categories, values); setBarData(chart, chartTitle, series, categories, values1, values2);
chart = doc.getCharts().get(1); chart = doc.getCharts().get(1);
setColumnData(chart, "Column variant"); setColumnData(chart, "Column variant");
@ -93,21 +97,22 @@ public class BarChartExample {
System.out.println("Done"); System.out.println("Done");
} }
private static void setBarData(XWPFChart chart, String chartTitle, String[] categories, Double[] values) { private static void setBarData(XWPFChart chart, String chartTitle, String[] series, String[] categories, Double[] values1, Double[] values2) {
final List<XDDFChartData> series = chart.getChartSeries(); final List<XDDFChartData> data = chart.getChartSeries();
final XDDFBarChartData bar = (XDDFBarChartData) series.get(0); final XDDFBarChartData bar = (XDDFBarChartData) data.get(0);
final int numOfPoints = categories.length; final int numOfPoints = categories.length;
final String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 0, 0)); final String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 0, 0));
final String valuesDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 1, 1)); final String valuesDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 1, 1));
final String valuesDataRange2 = chart.formatRange(new CellRangeAddress(1, numOfPoints, 2, 2)); final String valuesDataRange2 = chart.formatRange(new CellRangeAddress(1, numOfPoints, 2, 2));
final XDDFDataSource<?> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, 0); final XDDFDataSource<?> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, 0);
final XDDFNumericalDataSource<? extends Number> valuesData = XDDFDataSourcesFactory.fromArray(values, valuesDataRange, 1); final XDDFNumericalDataSource<? extends Number> valuesData = XDDFDataSourcesFactory.fromArray(values1, valuesDataRange, 1);
values[2] = 10.0; values1[6] = 16.0; // if you ever want to change the underlying data
final XDDFNumericalDataSource<? extends Number> valuesData2 = XDDFDataSourcesFactory.fromArray(values, valuesDataRange2, 2); final XDDFNumericalDataSource<? extends Number> valuesData2 = XDDFDataSourcesFactory.fromArray(values2, valuesDataRange2, 2);
bar.getSeries().get(0).replaceData(categoriesData, valuesData); bar.getSeries().get(0).replaceData(categoriesData, valuesData);
bar.addSeries(categoriesData, valuesData2); bar.addSeries(categoriesData, valuesData2);
bar.getSeries().get(0).setTitle(chartTitle, chart.setSheetTitle(chartTitle)); bar.getSeries().get(0).setTitle(series[0], chart.setSheetTitle(series[0], 0));
bar.getSeries().get(1).setTitle(series[1], chart.setSheetTitle(series[1], 1));
chart.plot(bar); chart.plot(bar);
} }
@ -115,7 +120,6 @@ public class BarChartExample {
// Series Text // Series Text
List<XDDFChartData> series = chart.getChartSeries(); List<XDDFChartData> series = chart.getChartSeries();
XDDFBarChartData bar = (XDDFBarChartData) series.get(0); XDDFBarChartData bar = (XDDFBarChartData) series.get(0);
bar.getSeries().get(0).setTitle(chartTitle, chart.setSheetTitle(chartTitle));
// in order to transform a bar chart into a column chart, you just need to change the bar direction // in order to transform a bar chart into a column chart, you just need to change the bar direction
bar.setBarDirection(BarDirection.COL); bar.setBarDirection(BarDirection.COL);

View File

@ -1,4 +1,12 @@
My Bar or Column Chart 10 languages with most speakers as first language
First 1.0 countries,speakers,language
Second 3.0 58,315,العربية
Third 4.0 4,243,বাংলা
38,1299,中文
118,378,English
4,260,हिन्दी
2,128,日本語
15,223,português
6,119,ਪੰਜਾਬੀ
18,154,Русский язык
31,442,español

View File

@ -109,6 +109,7 @@ public class XDDFBarChartData extends XDDFChartData {
XDDFNumericalDataSource<? extends Number> values) { XDDFNumericalDataSource<? extends Number> values) {
final int index = this.series.size(); final int index = this.series.size();
final CTBarSer ctSer = this.chart.addNewSer(); final CTBarSer ctSer = this.chart.addNewSer();
ctSer.addNewTx();
ctSer.addNewCat(); ctSer.addNewCat();
ctSer.addNewVal(); ctSer.addNewVal();
ctSer.addNewIdx().setVal(index); ctSer.addNewIdx().setVal(index);

View File

@ -669,20 +669,22 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai
} }
/** /**
* set sheet time in excel file * set sheet title in excel file
* *
* @param title * @param title
* title of sheet * title of sheet
* @param column
* column index
* @return return cell reference * @return return cell reference
* @since POI 4.0.0 * @since POI 4.0.0
*/ */
public CellReference setSheetTitle(String title) { public CellReference setSheetTitle(String title, int column) {
XSSFSheet sheet = getSheet(); XSSFSheet sheet = getSheet();
XSSFRow row = this.getRow(sheet, 0); XSSFRow row = this.getRow(sheet, 0);
XSSFCell cell = this.getCell(row, 1); XSSFCell cell = this.getCell(row, column);
cell.setCellValue(title); cell.setCellValue(title);
this.updateSheetTable(sheet.getTables().get(0).getCTTable(), title, 1); this.updateSheetTable(sheet.getTables().get(0).getCTTable(), title, column);
return new CellReference(sheet.getSheetName(), 0, 1, true, true); return new CellReference(sheet.getSheetName(), 0, column, true, true);
} }
/** /**
@ -698,12 +700,11 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai
private void updateSheetTable(CTTable ctTable, String title, int index) { private void updateSheetTable(CTTable ctTable, String title, int index) {
CTTableColumns tableColumnList = ctTable.getTableColumns(); CTTableColumns tableColumnList = ctTable.getTableColumns();
CTTableColumn column = null; CTTableColumn column = null;
if (tableColumnList.getCount() >= index) { for( int i = 0; tableColumnList.getCount() < index; i++) {
column = tableColumnList.getTableColumnArray(index);
} else {
column = tableColumnList.addNewTableColumn(); column = tableColumnList.addNewTableColumn();
column.setId(index); column.setId(i);
} }
column = tableColumnList.getTableColumnArray(index);
column.setName(title); column.setName(title);
} }

View File

@ -130,6 +130,10 @@ public abstract class XDDFChartData {
} else { } else {
cache = ref.addNewStrCache(); cache = ref.addNewStrCache();
} }
if (cache.sizeOfPtArray() < 1) {
cache.addNewPtCount().setVal(1);
cache.addNewPt().setIdx(0);;
}
cache.getPtArray(0).setV(title); cache.getPtArray(0).setV(title);
ref.setF(titleRef.formatAsString()); ref.setF(titleRef.formatAsString());
} }

View File

@ -158,7 +158,7 @@ public class TestXSLFChart {
final XDDFNumericalDataSource<Integer> valuesData = XDDFDataSourcesFactory.fromArray(values, valuesDataRange); final XDDFNumericalDataSource<Integer> valuesData = XDDFDataSourcesFactory.fromArray(values, valuesDataRange);
series.replaceData(categoryData, valuesData); series.replaceData(categoryData, valuesData);
final String title = "Apache POI"; final String title = "Apache POI";
series.setTitle(title, chart.setSheetTitle(title)); series.setTitle(title, chart.setSheetTitle(title, 0));
chart.plot(data); chart.plot(data);
} }