summaryrefslogtreecommitdiffstats
path: root/src/examples
diff options
context:
space:
mode:
authorAlain Béarez <abearez@apache.org>2019-12-02 00:15:34 +0000
committerAlain Béarez <abearez@apache.org>2019-12-02 00:15:34 +0000
commitdd3279df30eb9656c3a72f4a6a2445f777c7dd5a (patch)
treec54018db0d77fd5b8db59d651b940c153bcb0dee /src/examples
parent2eee47406368fdcb016922aa0e6971b14926648f (diff)
downloadpoi-dd3279df30eb9656c3a72f4a6a2445f777c7dd5a.tar.gz
poi-dd3279df30eb9656c3a72f4a6a2445f777c7dd5a.zip
Fix examples to build chart from scratch
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1870696 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/examples')
-rw-r--r--src/examples/src/org/apache/poi/xslf/usermodel/BarChartDemo.java2
-rw-r--r--src/examples/src/org/apache/poi/xslf/usermodel/ChartFromScratch.java24
-rw-r--r--src/examples/src/org/apache/poi/xwpf/usermodel/examples/ChartFromScratch.java38
3 files changed, 45 insertions, 19 deletions
diff --git a/src/examples/src/org/apache/poi/xslf/usermodel/BarChartDemo.java b/src/examples/src/org/apache/poi/xslf/usermodel/BarChartDemo.java
index bc8b6fb900..f9e8bac0c3 100644
--- a/src/examples/src/org/apache/poi/xslf/usermodel/BarChartDemo.java
+++ b/src/examples/src/org/apache/poi/xslf/usermodel/BarChartDemo.java
@@ -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);
diff --git a/src/examples/src/org/apache/poi/xslf/usermodel/ChartFromScratch.java b/src/examples/src/org/apache/poi/xslf/usermodel/ChartFromScratch.java
index 3d1f7c7b9c..1e923b9aab 100644
--- a/src/examples/src/org/apache/poi/xslf/usermodel/ChartFromScratch.java
+++ b/src/examples/src/org/apache/poi/xslf/usermodel/ChartFromScratch.java
@@ -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;
}
-
diff --git a/src/examples/src/org/apache/poi/xwpf/usermodel/examples/ChartFromScratch.java b/src/examples/src/org/apache/poi/xwpf/usermodel/examples/ChartFromScratch.java
index ee5624fd57..b1ebbdc01e 100644
--- a/src/examples/src/org/apache/poi/xwpf/usermodel/examples/ChartFromScratch.java
+++ b/src/examples/src/org/apache/poi/xwpf/usermodel/examples/ChartFromScratch.java
@@ -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;
}