Browse Source

Fix examples to build chart from scratch

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1870696 13f79535-47bb-0310-9956-ffa450edef68
tags/REL_4_1_2
Alain Béarez 4 years ago
parent
commit
dd3279df30

+ 1
- 1
src/examples/src/org/apache/poi/xslf/usermodel/BarChartDemo.java View File

@@ -105,7 +105,7 @@ public class BarChartDemo {
final String valuesDataRange2 = chart.formatRange(new CellRangeAddress(1, numOfPoints, columnSpeakers, columnSpeakers));
final XDDFDataSource<?> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, columnLanguages);
final XDDFNumericalDataSource<? extends Number> valuesData = XDDFDataSourcesFactory.fromArray(values1, valuesDataRange, columnCountries);
values1[6] = 16.0; // if you ever want to change the underlying data
values1[6] = 16.0; // if you ever want to change the underlying data, it has to be done before building the data source
final XDDFNumericalDataSource<? extends Number> valuesData2 = XDDFDataSourcesFactory.fromArray(values2, valuesDataRange2, columnSpeakers);

XDDFChartData.Series series1 = bar.getSeries(0);

+ 18
- 6
src/examples/src/org/apache/poi/xslf/usermodel/ChartFromScratch.java View File

@@ -28,13 +28,16 @@ import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.Units;
import org.apache.poi.xddf.usermodel.chart.AxisCrossBetween;
import org.apache.poi.xddf.usermodel.chart.AxisCrosses;
import org.apache.poi.xddf.usermodel.chart.AxisPosition;
import org.apache.poi.xddf.usermodel.chart.AxisTickMark;
import org.apache.poi.xddf.usermodel.chart.BarDirection;
import org.apache.poi.xddf.usermodel.chart.BarGrouping;
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;
@@ -47,7 +50,7 @@ import org.apache.poi.xddf.usermodel.chart.XDDFValueAxis;
*/
public class ChartFromScratch {
private static void usage(){
System.out.println("Usage: BarChartExample <bar-chart-data.txt>");
System.out.println("Usage: ChartFromScratch <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}");
}
@@ -86,8 +89,7 @@ public class ChartFromScratch {
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);
Rectangle2D rect2D = new java.awt.Rectangle(fromCM(1.5), fromCM(4), fromCM(22), fromCM(14));
slide.addChart(chart, rect2D);
setBarData(chart, chartTitle, series, categories, values1, values2);
// save the result
@@ -99,6 +101,10 @@ public class ChartFromScratch {
System.out.println("Done");
}

private static int fromCM(double cm) {
return (int) (Math.rint(cm * Units.EMU_PER_CENTIMETER));
}

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);
@@ -106,6 +112,8 @@ public class ChartFromScratch {
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setTitle(series[0]+","+series[1]);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
leftAxis.setMajorTickMark(AxisTickMark.OUT);
leftAxis.setCrossBetween(AxisCrossBetween.BETWEEN);

final int numOfPoints = categories.length;
final String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, columnLanguages, columnLanguages));
@@ -113,11 +121,15 @@ public class ChartFromScratch {
final String valuesDataRange2 = chart.formatRange(new CellRangeAddress(1, numOfPoints, columnSpeakers, columnSpeakers));
final XDDFDataSource<?> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, columnLanguages);
final XDDFNumericalDataSource<? extends Number> valuesData = XDDFDataSourcesFactory.fromArray(values1, valuesDataRange, columnCountries);
values1[6] = 16.0; // if you ever want to change the underlying data
valuesData.setFormatCode("General");
values1[6] = 16.0; // if you ever want to change the underlying data, it has to be done before building the data source
final XDDFNumericalDataSource<? extends Number> valuesData2 = XDDFDataSourcesFactory.fromArray(values2, valuesDataRange2, columnSpeakers);
valuesData2.setFormatCode("General");


XDDFBarChartData bar = (XDDFBarChartData) chart.createData(ChartTypes.BAR, bottomAxis, leftAxis);
bar.setBarGrouping(BarGrouping.CLUSTERED);

XDDFBarChartData.Series series1 = (XDDFBarChartData.Series) bar.addSeries(categoriesData, valuesData);
series1.setTitle(series[0], chart.setSheetTitle(series[0], columnCountries));

@@ -134,10 +146,10 @@ public class ChartFromScratch {

chart.setTitleText(chartTitle);
chart.setTitleOverlay(false);
chart.setAutoTitleDeleted(false);
}

private static final int columnLanguages = 0;
private static final int columnCountries = 1;
private static final int columnSpeakers = 2;
}


+ 26
- 12
src/examples/src/org/apache/poi/xwpf/usermodel/examples/ChartFromScratch.java View File

@@ -27,9 +27,12 @@ import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xddf.usermodel.chart.AxisCrossBetween;
import org.apache.poi.xddf.usermodel.chart.AxisCrosses;
import org.apache.poi.xddf.usermodel.chart.AxisPosition;
import org.apache.poi.xddf.usermodel.chart.AxisTickMark;
import org.apache.poi.xddf.usermodel.chart.BarDirection;
import org.apache.poi.xddf.usermodel.chart.BarGrouping;
import org.apache.poi.xddf.usermodel.chart.ChartTypes;
import org.apache.poi.xddf.usermodel.chart.LegendPosition;
import org.apache.poi.xddf.usermodel.chart.XDDFBarChartData;
@@ -48,7 +51,7 @@ import org.apache.poi.xwpf.usermodel.XWPFDocument;
*/
public class ChartFromScratch {
private static void usage(){
System.out.println("Usage: BarChartExample <bar-chart-data.txt>");
System.out.println("Usage: ChartFromScratch <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}");
}
@@ -85,10 +88,10 @@ public class ChartFromScratch {
Double[] values2 = listSpeakers.toArray(new Double[0]);

try (XWPFDocument doc = new XWPFDocument()) {
XWPFChart chart = doc.createChart(XDDFChart.DEFAULT_WIDTH, XDDFChart.DEFAULT_HEIGHT);
XWPFChart chart = doc.createChart(XDDFChart.DEFAULT_WIDTH * 10, XDDFChart.DEFAULT_HEIGHT * 15);
setBarData(chart, chartTitle, series, categories, values1, values2);
// save the result
try (OutputStream out = new FileOutputStream("bar-chart-demo-output.docx")) {
try (OutputStream out = new FileOutputStream("chart-from-scratch.docx")) {
doc.write(out);
}
}
@@ -107,23 +110,29 @@ public class ChartFromScratch {
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setTitle(series[0]+","+series[1]);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
leftAxis.setMajorTickMark(AxisTickMark.OUT);
leftAxis.setCrossBetween(AxisCrossBetween.BETWEEN);

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);
final String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, columnLanguages, columnLanguages));
final String valuesDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, columnCountries, columnCountries));
final String valuesDataRange2 = chart.formatRange(new CellRangeAddress(1, numOfPoints, columnSpeakers, columnSpeakers));
final XDDFDataSource<?> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, columnLanguages);
final XDDFNumericalDataSource<? extends Number> valuesData = XDDFDataSourcesFactory.fromArray(values1, valuesDataRange, columnCountries);
valuesData.setFormatCode("General");
values1[6] = 16.0; // if you ever want to change the underlying data, it has to be done before building the data source
final XDDFNumericalDataSource<? extends Number> valuesData2 = XDDFDataSourcesFactory.fromArray(values2, valuesDataRange2, columnSpeakers);
valuesData2.setFormatCode("General");


XDDFBarChartData bar = (XDDFBarChartData) chart.createData(ChartTypes.BAR, bottomAxis, leftAxis);
bar.setBarGrouping(BarGrouping.CLUSTERED);

XDDFBarChartData.Series series1 = (XDDFBarChartData.Series) bar.addSeries(categoriesData, valuesData);
series1.setTitle(series[0], chart.setSheetTitle(series[0], 1));
series1.setTitle(series[0], chart.setSheetTitle(series[0], columnCountries));

XDDFBarChartData.Series series2 = (XDDFBarChartData.Series) bar.addSeries(categoriesData, valuesData2);
series2.setTitle(series[1], chart.setSheetTitle(series[1], 2));
series2.setTitle(series[1], chart.setSheetTitle(series[1], columnSpeakers));

bar.setVaryColors(true);
bar.setBarDirection(BarDirection.COL);
@@ -135,6 +144,11 @@ public class ChartFromScratch {

chart.setTitleText(chartTitle);
chart.setTitleOverlay(false);
chart.setAutoTitleDeleted(false);
}

private static final int columnLanguages = 0;
private static final int columnCountries = 1;
private static final int columnSpeakers = 2;
}


+ 9
- 3
src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChart.java View File

@@ -392,7 +392,7 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai
series.plot();
XDDFDataSource<?> categoryDS = series.getCategoryData();
XDDFNumericalDataSource<? extends Number> valuesDS = series.getValuesData();
if (categoryDS.isReference() || valuesDS.isReference()
if (categoryDS.isCellRange() || valuesDS.isCellRange()
|| categoryDS.isLiteral() || valuesDS.isLiteral()) {
// let's assume the data is already in the sheet
} else {
@@ -773,8 +773,14 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai
int numOfPoints = categoryData.getPointCount();
for (int i = 0; i < numOfPoints; i++) {
XSSFRow row = this.getRow(sheet, i + 1); // first row is for title
this.getCell(row, categoryData.getColIndex()).setCellValue(categoryData.getPointAt(i).toString());
this.getCell(row, valuesData.getColIndex()).setCellValue(valuesData.getPointAt(i).doubleValue());
Object category = categoryData.getPointAt(i);
if (category != null) {
this.getCell(row, categoryData.getColIndex()).setCellValue(category.toString());
}
Number value = valuesData.getPointAt(i);
if (value != null) {
this.getCell(row, valuesData.getColIndex()).setCellValue(value.doubleValue());
}
}
}


+ 5
- 0
src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSource.java View File

@@ -32,6 +32,11 @@ public interface XDDFDataSource<T> {
*/
boolean isLiteral();

/**
* @since POI 4.1.2
*/
boolean isCellRange();

boolean isReference();

boolean isNumeric();

+ 25
- 0
src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSourcesFactory.java View File

@@ -46,6 +46,11 @@ public class XDDFDataSourcesFactory {
return new XDDFCategoryDataSource() {
private CTNumData category = (CTNumData) categoryDS.getNumRef().getNumCache().copy();

@Override
public boolean isCellRange() {
return true;
}

@Override
public boolean isNumeric() {
return true;
@@ -70,6 +75,11 @@ public class XDDFDataSourcesFactory {
return new XDDFCategoryDataSource() {
private CTStrData category = (CTStrData) categoryDS.getStrRef().getStrCache().copy();

@Override
public boolean isCellRange() {
return true;
}

@Override
public String getFormula() {
return categoryDS.getStrRef().getF();
@@ -108,6 +118,11 @@ public class XDDFDataSourcesFactory {
this.formatCode = formatCode;
}

@Override
public boolean isCellRange() {
return true;
}

@Override
public boolean isNumeric() {
return true;
@@ -199,6 +214,11 @@ public class XDDFDataSourcesFactory {
return elements[index];
}

@Override
public boolean isCellRange() {
return false;
}

@Override
public boolean isReference() {
return dataRange != null;
@@ -310,6 +330,11 @@ public class XDDFDataSourcesFactory {
return numOfCells;
}

@Override
public boolean isCellRange() {
return true;
}

@Override
public boolean isReference() {
return true;

Loading…
Cancel
Save