Allow for numerical category in charts

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1874705 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Alain Béarez 2020-03-02 23:57:05 +00:00
parent 2522a3bc89
commit 850387f5cb
4 changed files with 54 additions and 39 deletions

View File

@ -211,7 +211,7 @@ public abstract class XDDFChartData {
public void plot() { public void plot() {
if (categoryData.isNumeric()) { if (categoryData.isNumeric()) {
CTNumData cache = retrieveNumCache(getAxDS(), categoryData); CTNumData cache = retrieveNumCache(getAxDS(), categoryData);
((XDDFNumericalDataSource<?>) categoryData).fillNumericalCache(cache); categoryData.fillNumericalCache(cache);
} else { } else {
CTStrData cache = retrieveStrCache(getAxDS(), categoryData); CTStrData cache = retrieveStrCache(getAxDS(), categoryData);
categoryData.fillStringCache(cache); categoryData.fillStringCache(cache);

View File

@ -21,6 +21,8 @@ package org.apache.poi.xddf.usermodel.chart;
import org.apache.poi.util.Beta; import org.apache.poi.util.Beta;
import org.apache.poi.util.Internal; import org.apache.poi.util.Internal;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumData;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumVal;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrData; import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrData;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrVal; import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrVal;
@ -50,6 +52,44 @@ public interface XDDFDataSource<T> {
String getFormula(); String getFormula();
String getFormatCode();
/**
* @since POI 4.1.3
*/
@Internal
default void fillNumericalCache(CTNumData cache) {
String formatCode = getFormatCode();
if (formatCode == null) {
if (cache.isSetFormatCode()) {
cache.unsetFormatCode();
}
} else {
cache.setFormatCode(formatCode);
}
cache.setPtArray(null); // unset old values
final int numOfPoints = getPointCount();
int effectiveNumOfPoints = 0;
for (int i = 0; i < numOfPoints; ++i) {
Object value = getPointAt(i);
if (value != null) {
CTNumVal ctNumVal = cache.addNewPt();
ctNumVal.setIdx(i);
ctNumVal.setV(value.toString());
effectiveNumOfPoints++;
}
}
if (effectiveNumOfPoints == 0) {
cache.unsetPtCount();
} else {
if (cache.isSetPtCount()) {
cache.getPtCount().setVal(numOfPoints);
} else {
cache.addNewPtCount().setVal(numOfPoints);
}
}
}
/** /**
* @since POI 4.1.2 * @since POI 4.1.2
*/ */

View File

@ -45,6 +45,7 @@ public class XDDFDataSourcesFactory {
if (categoryDS.getStrRef() == null) { if (categoryDS.getStrRef() == null) {
return new XDDFCategoryDataSource() { return new XDDFCategoryDataSource() {
private CTNumData category = (CTNumData) categoryDS.getNumRef().getNumCache().copy(); private CTNumData category = (CTNumData) categoryDS.getNumRef().getNumCache().copy();
private String formatCode = category.isSetFormatCode() ? category.getFormatCode() : null;
@Override @Override
public boolean isCellRange() { public boolean isCellRange() {
@ -70,6 +71,9 @@ public class XDDFDataSourcesFactory {
public String getPointAt(int index) { public String getPointAt(int index) {
return category.getPtArray(index).getV(); return category.getPtArray(index).getV();
} }
@Override
public String getFormatCode() { return formatCode; }
}; };
} else { } else {
return new XDDFCategoryDataSource() { return new XDDFCategoryDataSource() {
@ -94,6 +98,9 @@ public class XDDFDataSourcesFactory {
public String getPointAt(int index) { public String getPointAt(int index) {
return category.getPtArray(index).getV(); return category.getPtArray(index).getV();
} }
@Override
public String getFormatCode() { return null; }
}; };
} }
} }
@ -287,6 +294,9 @@ public class XDDFDataSourcesFactory {
public String getFormula() { public String getFormula() {
return getDataRangeReference(); return getDataRangeReference();
} }
@Override
public String getFormatCode() { return null; }
} }
private static class LiteralNumericalArrayDataSource<T extends Number> extends NumericalArrayDataSource<T> { private static class LiteralNumericalArrayDataSource<T extends Number> extends NumericalArrayDataSource<T> {
@ -430,5 +440,8 @@ public class XDDFDataSourcesFactory {
public boolean isNumeric() { public boolean isNumeric() {
return false; return false;
} }
@Override
public String getFormatCode() { return null; }
} }
} }

View File

@ -26,48 +26,10 @@ import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumVal;
@Beta @Beta
public interface XDDFNumericalDataSource<T extends Number> extends XDDFDataSource<T> { public interface XDDFNumericalDataSource<T extends Number> extends XDDFDataSource<T> {
String getFormatCode();
void setFormatCode(String formatCode); void setFormatCode(String formatCode);
@Override @Override
default boolean isLiteral() { default boolean isLiteral() {
return false; return false;
} }
/**
* @since POI 4.1.2
*/
@Internal
default void fillNumericalCache(CTNumData cache) {
String formatCode = getFormatCode();
if (formatCode == null) {
if (cache.isSetFormatCode()) {
cache.unsetFormatCode();
}
} else {
cache.setFormatCode(formatCode);
}
cache.setPtArray(null); // unset old values
final int numOfPoints = getPointCount();
int effectiveNumOfPoints = 0;
for (int i = 0; i < numOfPoints; ++i) {
Object value = getPointAt(i);
if (value != null) {
CTNumVal ctNumVal = cache.addNewPt();
ctNumVal.setIdx(i);
ctNumVal.setV(value.toString());
effectiveNumOfPoints++;
}
}
if (effectiveNumOfPoints == 0) {
cache.unsetPtCount();
} else {
if (cache.isSetPtCount()) {
cache.getPtCount().setVal(numOfPoints);
} else {
cache.addNewPtCount().setVal(numOfPoints);
}
}
}
} }