mirror of https://github.com/apache/poi.git
adapting some contributions by Axel Richter on SO
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1841988 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
5df2742d73
commit
9973fecad0
|
@ -115,6 +115,9 @@ public class BarChartDemo {
|
||||||
// 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);
|
||||||
|
|
||||||
|
// looking for "Stacked Bar Chart"? uncomment the following line
|
||||||
|
// bar.setBarGrouping(BarGrouping.STACKED);
|
||||||
|
|
||||||
// additionally, you can adjust the axes
|
// additionally, you can adjust the axes
|
||||||
bar.getCategoryAxis().setOrientation(AxisOrientation.MAX_MIN);
|
bar.getCategoryAxis().setOrientation(AxisOrientation.MAX_MIN);
|
||||||
bar.getValueAxes().get(0).setPosition(AxisPosition.TOP);
|
bar.getValueAxes().get(0).setPosition(AxisPosition.TOP);
|
||||||
|
|
|
@ -28,8 +28,10 @@ import org.apache.poi.xddf.usermodel.XDDFShapeProperties;
|
||||||
import org.apache.poi.xddf.usermodel.XDDFSolidFillProperties;
|
import org.apache.poi.xddf.usermodel.XDDFSolidFillProperties;
|
||||||
import org.apache.poi.xddf.usermodel.chart.AxisCrosses;
|
import org.apache.poi.xddf.usermodel.chart.AxisCrosses;
|
||||||
import org.apache.poi.xddf.usermodel.chart.AxisPosition;
|
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.ChartTypes;
|
||||||
import org.apache.poi.xddf.usermodel.chart.LegendPosition;
|
import org.apache.poi.xddf.usermodel.chart.LegendPosition;
|
||||||
|
import org.apache.poi.xddf.usermodel.chart.XDDFBarChartData;
|
||||||
import org.apache.poi.xddf.usermodel.chart.XDDFCategoryAxis;
|
import org.apache.poi.xddf.usermodel.chart.XDDFCategoryAxis;
|
||||||
import org.apache.poi.xddf.usermodel.chart.XDDFChartData;
|
import org.apache.poi.xddf.usermodel.chart.XDDFChartData;
|
||||||
import org.apache.poi.xddf.usermodel.chart.XDDFChartLegend;
|
import org.apache.poi.xddf.usermodel.chart.XDDFChartLegend;
|
||||||
|
@ -86,14 +88,14 @@ public class BarChart {
|
||||||
data.addSeries(xs, ys2);
|
data.addSeries(xs, ys2);
|
||||||
chart.plot(data);
|
chart.plot(data);
|
||||||
|
|
||||||
XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(PresetColor.CHARTREUSE));
|
// in order to transform a bar chart into a column chart, you just need to change the bar direction
|
||||||
XDDFChartData.Series firstSeries = data.getSeries().get(0);
|
XDDFBarChartData bar = (XDDFBarChartData) data;
|
||||||
XDDFShapeProperties properties = firstSeries.getShapeProperties();
|
bar.setBarDirection(BarDirection.COL);
|
||||||
if (properties == null) {
|
// looking for "Stacked Bar Chart"? uncomment the following line
|
||||||
properties = new XDDFShapeProperties();
|
// bar.setBarGrouping(BarGrouping.STACKED);
|
||||||
}
|
|
||||||
properties.setFillProperties(fill);
|
solidFillSeries(data, 0, PresetColor.CHARTREUSE);
|
||||||
firstSeries.setShapeProperties(properties);
|
solidFillSeries(data, 1, PresetColor.TURQUOISE);
|
||||||
|
|
||||||
// Write the output to a file
|
// Write the output to a file
|
||||||
try (FileOutputStream fileOut = new FileOutputStream("ooxml-bar-chart.xlsx")) {
|
try (FileOutputStream fileOut = new FileOutputStream("ooxml-bar-chart.xlsx")) {
|
||||||
|
@ -101,4 +103,15 @@ public class BarChart {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void solidFillSeries(XDDFChartData data, int index, PresetColor color) {
|
||||||
|
XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(color));
|
||||||
|
XDDFChartData.Series firstSeries = data.getSeries().get(index);
|
||||||
|
XDDFShapeProperties properties = firstSeries.getShapeProperties();
|
||||||
|
if (properties == null) {
|
||||||
|
properties = new XDDFShapeProperties();
|
||||||
|
}
|
||||||
|
properties.setFillProperties(fill);
|
||||||
|
firstSeries.setShapeProperties(properties);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,9 +43,9 @@ import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
||||||
/**
|
/**
|
||||||
* Build a bar chart from a template docx
|
* Build a bar chart from a template docx
|
||||||
*/
|
*/
|
||||||
public class BarChartExampleDOCX {
|
public class BarChartExample {
|
||||||
private static void usage(){
|
private static void usage(){
|
||||||
System.out.println("Usage: BarChartDemo <bar-chart-template.docx> <bar-chart-data.txt>");
|
System.out.println("Usage: BarChartExample <bar-chart-template.docx> <bar-chart-data.txt>");
|
||||||
System.out.println(" bar-chart-template.docx template with a bar chart");
|
System.out.println(" bar-chart-template.docx template with a bar chart");
|
||||||
System.out.println(" bar-chart-data.txt the model to set. First line is chart title, " +
|
System.out.println(" bar-chart-data.txt the model to set. First line is chart title, " +
|
||||||
"then go pairs {axis-label value}");
|
"then go pairs {axis-label value}");
|
||||||
|
@ -119,6 +119,9 @@ public class BarChartExampleDOCX {
|
||||||
|
|
||||||
// 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);
|
||||||
|
|
||||||
|
// looking for "Stacked Bar Chart"? uncomment the following line
|
||||||
|
// bar.setBarGrouping(BarGrouping.STACKED);
|
||||||
|
|
||||||
// additionally, you can adjust the axes
|
// additionally, you can adjust the axes
|
||||||
bar.getCategoryAxis().setOrientation(AxisOrientation.MAX_MIN);
|
bar.getCategoryAxis().setOrientation(AxisOrientation.MAX_MIN);
|
|
@ -34,9 +34,24 @@ public class XDDFBarChartData extends XDDFChartData {
|
||||||
public XDDFBarChartData(CTBarChart chart, Map<Long, XDDFChartAxis> categories,
|
public XDDFBarChartData(CTBarChart chart, Map<Long, XDDFChartAxis> categories,
|
||||||
Map<Long, XDDFValueAxis> values) {
|
Map<Long, XDDFValueAxis> values) {
|
||||||
this.chart = chart;
|
this.chart = chart;
|
||||||
|
if (chart.getBarDir() == null) {
|
||||||
|
chart.addNewBarDir().setVal(BarDirection.BAR.underlying);
|
||||||
|
}
|
||||||
for (CTBarSer series : chart.getSerList()) {
|
for (CTBarSer series : chart.getSerList()) {
|
||||||
this.series.add(new Series(series, series.getCat(), series.getVal()));
|
this.series.add(new Series(series, series.getCat(), series.getVal()));
|
||||||
}
|
}
|
||||||
|
defineAxes(categories, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void defineAxes(Map<Long, XDDFChartAxis> categories, Map<Long, XDDFValueAxis> values) {
|
||||||
|
if (chart.sizeOfAxIdArray() == 0) {
|
||||||
|
for (Long id : categories.keySet()) {
|
||||||
|
chart.addNewAxId().setVal(id);
|
||||||
|
}
|
||||||
|
for (Long id : values.keySet()) {
|
||||||
|
chart.addNewAxId().setVal(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
defineAxes(chart.getAxIdArray(), categories, values);
|
defineAxes(chart.getAxIdArray(), categories, values);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -214,6 +214,34 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai
|
||||||
chart.getAutoTitleDeleted().setVal(deleted);
|
chart.getAutoTitleDeleted().setVal(deleted);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since 4.0.1
|
||||||
|
*/
|
||||||
|
public Boolean getTitleOverlay() {
|
||||||
|
if (chart.isSetTitle()) {
|
||||||
|
CTTitle title = chart.getTitle();
|
||||||
|
if (title.isSetOverlay()) {
|
||||||
|
return title.getOverlay().getVal();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since 4.0.1
|
||||||
|
*/
|
||||||
|
public void setTitleOverlay(boolean overlay) {
|
||||||
|
if (!chart.isSetTitle()) {
|
||||||
|
chart.addNewTitle();
|
||||||
|
}
|
||||||
|
CTTitle title = chart.getTitle();
|
||||||
|
if (title.isSetOverlay()) {
|
||||||
|
title.getOverlay().setVal(overlay);
|
||||||
|
} else {
|
||||||
|
title.addNewOverlay().setVal(overlay);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the chart title body if there is one, i.e. title is set and is not a
|
* Get the chart title body if there is one, i.e. title is set and is not a
|
||||||
* formula.
|
* formula.
|
||||||
|
@ -327,7 +355,7 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai
|
||||||
private Map<Long, XDDFChartAxis> getCategoryAxes() {
|
private Map<Long, XDDFChartAxis> getCategoryAxes() {
|
||||||
CTPlotArea plotArea = getCTPlotArea();
|
CTPlotArea plotArea = getCTPlotArea();
|
||||||
int sizeOfArray = plotArea.sizeOfCatAxArray();
|
int sizeOfArray = plotArea.sizeOfCatAxArray();
|
||||||
Map<Long, XDDFChartAxis> axes = new HashMap<Long, XDDFChartAxis>(sizeOfArray);
|
Map<Long, XDDFChartAxis> axes = new HashMap<>(sizeOfArray);
|
||||||
for (int i = 0; i < sizeOfArray; i++) {
|
for (int i = 0; i < sizeOfArray; i++) {
|
||||||
CTCatAx category = plotArea.getCatAxArray(i);
|
CTCatAx category = plotArea.getCatAxArray(i);
|
||||||
axes.put(category.getAxId().getVal(), new XDDFCategoryAxis(category));
|
axes.put(category.getAxId().getVal(), new XDDFCategoryAxis(category));
|
||||||
|
|
|
@ -38,6 +38,18 @@ public class XDDFLineChartData extends XDDFChartData {
|
||||||
for (CTLineSer series : chart.getSerList()) {
|
for (CTLineSer series : chart.getSerList()) {
|
||||||
this.series.add(new Series(series, series.getCat(), series.getVal()));
|
this.series.add(new Series(series, series.getCat(), series.getVal()));
|
||||||
}
|
}
|
||||||
|
defineAxes(categories, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void defineAxes(Map<Long, XDDFChartAxis> categories, Map<Long, XDDFValueAxis> values) {
|
||||||
|
if (chart.sizeOfAxIdArray() == 0) {
|
||||||
|
for (Long id : categories.keySet()) {
|
||||||
|
chart.addNewAxId().setVal(id);
|
||||||
|
}
|
||||||
|
for (Long id : values.keySet()) {
|
||||||
|
chart.addNewAxId().setVal(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
defineAxes(chart.getAxIdArray(), categories, values);
|
defineAxes(chart.getAxIdArray(), categories, values);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -38,6 +38,18 @@ public class XDDFRadarChartData extends XDDFChartData {
|
||||||
for (CTRadarSer series : chart.getSerList()) {
|
for (CTRadarSer series : chart.getSerList()) {
|
||||||
this.series.add(new Series(series, series.getCat(), series.getVal()));
|
this.series.add(new Series(series, series.getCat(), series.getVal()));
|
||||||
}
|
}
|
||||||
|
defineAxes(categories, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void defineAxes(Map<Long, XDDFChartAxis> categories, Map<Long, XDDFValueAxis> values) {
|
||||||
|
if (chart.sizeOfAxIdArray() == 0) {
|
||||||
|
for (Long id : categories.keySet()) {
|
||||||
|
chart.addNewAxId().setVal(id);
|
||||||
|
}
|
||||||
|
for (Long id : values.keySet()) {
|
||||||
|
chart.addNewAxId().setVal(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
defineAxes(chart.getAxIdArray(), categories, values);
|
defineAxes(chart.getAxIdArray(), categories, values);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -38,6 +38,18 @@ public class XDDFScatterChartData extends XDDFChartData {
|
||||||
for (CTScatterSer series : chart.getSerList()) {
|
for (CTScatterSer series : chart.getSerList()) {
|
||||||
this.series.add(new Series(series, series.getXVal(), series.getYVal()));
|
this.series.add(new Series(series, series.getXVal(), series.getYVal()));
|
||||||
}
|
}
|
||||||
|
defineAxes(categories, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void defineAxes(Map<Long, XDDFChartAxis> categories, Map<Long, XDDFValueAxis> values) {
|
||||||
|
if (chart.sizeOfAxIdArray() == 0) {
|
||||||
|
for (Long id : categories.keySet()) {
|
||||||
|
chart.addNewAxId().setVal(id);
|
||||||
|
}
|
||||||
|
for (Long id : values.keySet()) {
|
||||||
|
chart.addNewAxId().setVal(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
defineAxes(chart.getAxIdArray(), categories, values);
|
defineAxes(chart.getAxIdArray(), categories, values);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,6 +111,28 @@ public class XDDFScatterChartData extends XDDFChartData {
|
||||||
return series.getTx();
|
return series.getTx();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since 4.0.1
|
||||||
|
*/
|
||||||
|
public Boolean getSmooth() {
|
||||||
|
if (series.isSetSmooth()) {
|
||||||
|
return series.getSmooth().getVal();
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since 4.0.1
|
||||||
|
*/
|
||||||
|
public void setSmooth(boolean smooth) {
|
||||||
|
if (series.isSetSmooth()) {
|
||||||
|
series.getSmooth().setVal(smooth);
|
||||||
|
} else {
|
||||||
|
series.addNewSmooth().setVal(smooth);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setShowLeaderLines(boolean showLeaderLines) {
|
public void setShowLeaderLines(boolean showLeaderLines) {
|
||||||
if (!series.isSetDLbls()) {
|
if (!series.isSetDLbls()) {
|
||||||
|
|
Loading…
Reference in New Issue