This is an automated email from the ASF dual-hosted git repository.

pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git


The following commit(s) were added to refs/heads/trunk by this push:
     new f5f1ddbb03 Keep cell number formats when plotting chart data from a 
sheet range (#1189)
f5f1ddbb03 is described below

commit f5f1ddbb03973cb3bce91af9ae20d6b73c086087
Author: Kamil Krzywanski <[email protected]>
AuthorDate: Tue Sep 8 22:32:44 2026 +0200

    Keep cell number formats when plotting chart data from a sheet range (#1189)
    
    * Keep cell number formats when plotting chart data from a sheet range
    
    fromNumericCellRange left formatCode null, so plot() cleared the chart
    numCache format and data labels linked to source showed unformatted
    values. Read formats from the cells and write them into the cache.
    
    Fixes #1007
    
    * Clarify getPointFormatCode null means use series format
    
    Signed-off-by: Kamil Krzywanski <[email protected]>
    
    * Update 
poi-ooxml/src/main/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSource.java
    
    * Fix formatting and clarity in XDDFDataSource JavaDoc
    
    ---------
    
    Signed-off-by: Kamil Krzywanski <[email protected]>
    Co-authored-by: PJ Fanning <[email protected]>
---
 .../poi/xddf/usermodel/chart/XDDFDataSource.java   | 22 ++++++++
 .../usermodel/chart/XDDFDataSourcesFactory.java    | 39 ++++++++++++--
 .../chart/TestXDDFDataSourcesFactory.java          | 60 ++++++++++++++++++++++
 3 files changed, 116 insertions(+), 5 deletions(-)

diff --git 
a/poi-ooxml/src/main/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSource.java
 
b/poi-ooxml/src/main/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSource.java
index 229e505bf6..7f7b2a0b57 100644
--- 
a/poi-ooxml/src/main/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSource.java
+++ 
b/poi-ooxml/src/main/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSource.java
@@ -56,6 +56,24 @@ public interface XDDFDataSource<T> {
 
     String getFormatCode();
 
+    /**
+     * Optional number format for a single data point in the numerical cache.
+     * <p>
+     * Return {@code null} when this point has no point-specific format and 
should
+     * inherit the series format from {@link #getFormatCode()}. 
Implementations that
+     * never use per-point formats can keep the default ({@code null}).
+     * NumericalCellRangeDataSource is one class that implements this method.
+     * </p>
+     *
+     * @param index zero-based point index
+     * @return the data point's format code, or {@code null} if the underlying 
class
+     * does not support this method or if there is no format code set for this 
data point.
+     * @since 6.0.0
+     */
+    default String getPointFormatCode(int index) {
+        return null;
+    }
+
     /**
      * @since 5.0.0
      */
@@ -78,6 +96,10 @@ public interface XDDFDataSource<T> {
                 CTNumVal ctNumVal = cache.addNewPt();
                 ctNumVal.setIdx(i);
                 ctNumVal.setV(value.toString());
+                String pointFormat = getPointFormatCode(i);
+                if (pointFormat != null && !pointFormat.equals(formatCode)) {
+                    ctNumVal.setFormatCode(pointFormat);
+                }
                 effectiveNumOfPoints++;
             }
         }
diff --git 
a/poi-ooxml/src/main/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSourcesFactory.java
 
b/poi-ooxml/src/main/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSourcesFactory.java
index 4864612a74..72c8527281 100644
--- 
a/poi-ooxml/src/main/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSourcesFactory.java
+++ 
b/poi-ooxml/src/main/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSourcesFactory.java
@@ -23,6 +23,7 @@ import org.apache.poi.ss.usermodel.CellType;
 import org.apache.poi.ss.usermodel.CellValue;
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.util.Beta;
+import org.apache.poi.xssf.usermodel.XSSFCell;
 import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
 import org.apache.poi.xssf.usermodel.XSSFRow;
 import org.apache.poi.xssf.usermodel.XSSFSheet;
@@ -520,7 +521,7 @@ public class XDDFDataSourcesFactory {
             return cellRangeAddress.formatAsString(sheet.getSheetName(), true);
         }
 
-        protected CellValue getCellValueAt(int index) {
+        protected XSSFCell getCellAt(int index) {
             if (index < 0 || index >= numOfCells) {
                 throw new IndexOutOfBoundsException(
                         "Index must be between 0 and " + (numOfCells - 1) + " 
(inclusive), given: " + index);
@@ -532,26 +533,54 @@ public class XDDFDataSourcesFactory {
             int rowIndex = firstRow + index / width;
             int cellIndex = firstCol + index % width;
             XSSFRow row = sheet.getRow(rowIndex);
-            return (row == null) ? null : 
evaluator.evaluate(row.getCell(cellIndex));
+            return (row == null) ? null : row.getCell(cellIndex);
+        }
+
+        protected CellValue getCellValueAt(int index) {
+            XSSFCell cell = getCellAt(index);
+            return (cell == null) ? null : evaluator.evaluate(cell);
+        }
+
+        protected String getCellFormatCodeAt(int index) {
+            XSSFCell cell = getCellAt(index);
+            return (cell == null) ? null : 
cell.getCellStyle().getDataFormatString();
         }
     }
 
     private static class NumericalCellRangeDataSource extends 
AbstractCellRangeDataSource<Double>
             implements XDDFNumericalDataSource<Double> {
+        private String formatCode;
+        private boolean formatCodeSet;
+
         protected NumericalCellRangeDataSource(XSSFSheet sheet, 
CellRangeAddress cellRangeAddress) {
             super(sheet, cellRangeAddress);
         }
 
-        private String formatCode;
-
         @Override
         public String getFormatCode() {
-            return formatCode;
+            if (formatCodeSet) {
+                return formatCode;
+            }
+            for (int i = 0; i < getPointCount(); i++) {
+                if (getPointAt(i) != null) {
+                    String cellFormat = getCellFormatCodeAt(i);
+                    if (cellFormat != null) {
+                        return cellFormat;
+                    }
+                }
+            }
+            return null;
         }
 
         @Override
         public void setFormatCode(String formatCode) {
             this.formatCode = formatCode;
+            this.formatCodeSet = true;
+        }
+
+        @Override
+        public String getPointFormatCode(int index) {
+            return getCellFormatCodeAt(index);
         }
 
         @Override
diff --git 
a/poi-ooxml/src/test/java/org/apache/poi/xddf/usermodel/chart/TestXDDFDataSourcesFactory.java
 
b/poi-ooxml/src/test/java/org/apache/poi/xddf/usermodel/chart/TestXDDFDataSourcesFactory.java
index e979486c7d..c8a47e653b 100644
--- 
a/poi-ooxml/src/test/java/org/apache/poi/xddf/usermodel/chart/TestXDDFDataSourcesFactory.java
+++ 
b/poi-ooxml/src/test/java/org/apache/poi/xddf/usermodel/chart/TestXDDFDataSourcesFactory.java
@@ -23,8 +23,14 @@ import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
+import java.io.IOException;
+
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.ss.util.SheetBuilder;
+import org.apache.poi.xssf.usermodel.XSSFCell;
+import org.apache.poi.xssf.usermodel.XSSFCellStyle;
+import org.apache.poi.xssf.usermodel.XSSFDataFormat;
+import org.apache.poi.xssf.usermodel.XSSFRow;
 import org.apache.poi.xssf.usermodel.XSSFSheet;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.junit.jupiter.api.Test;
@@ -186,6 +192,60 @@ class TestXDDFDataSourcesFactory {
         assertThrows(ArithmeticException.class, numLitSource::getPointCount);
     }
 
+    @Test
+    void testNumericCellRangeFormatCodes() throws IOException {
+        try (XSSFWorkbook wb = new XSSFWorkbook()) {
+            XSSFSheet sheet = wb.createSheet();
+            XSSFDataFormat dataFormat = wb.createDataFormat();
+
+            XSSFCellStyle decimalStyle = wb.createCellStyle();
+            decimalStyle.setDataFormat(dataFormat.getFormat("#,##0.###"));
+            XSSFCellStyle intStyle = wb.createCellStyle();
+            intStyle.setDataFormat(dataFormat.getFormat("#,##0"));
+
+            XSSFRow row = sheet.createRow(0);
+            XSSFCell c0 = row.createCell(0);
+            c0.setCellValue(1000.5);
+            c0.setCellStyle(decimalStyle);
+            XSSFCell c1 = row.createCell(1);
+            c1.setCellValue(1200);
+            c1.setCellStyle(intStyle);
+            XSSFCell c2 = row.createCell(2);
+            c2.setCellValue(2000.034);
+            c2.setCellStyle(decimalStyle);
+
+            CellRangeAddress range = new CellRangeAddress(0, 0, 0, 2);
+            XDDFNumericalDataSource<Double> ds = 
XDDFDataSourcesFactory.fromNumericCellRange(sheet, range);
+
+            assertEquals("#,##0.###", ds.getFormatCode());
+            assertEquals("#,##0.###", ds.getPointFormatCode(0));
+            assertEquals("#,##0", ds.getPointFormatCode(1));
+            assertEquals("#,##0.###", ds.getPointFormatCode(2));
+
+            ds.setFormatCode("0.00");
+            assertEquals("0.00", ds.getFormatCode());
+            assertEquals("#,##0", ds.getPointFormatCode(1));
+
+            ds = XDDFDataSourcesFactory.fromNumericCellRange(sheet, range);
+            CTNumData cache = CTNumData.Factory.newInstance();
+            cache.setFormatCode("General");
+            ds.fillNumericalCache(cache);
+
+            assertEquals("#,##0.###", cache.getFormatCode());
+            assertEquals(3, cache.sizeOfPtArray());
+            assertFalse(cache.getPtArray(0).isSetFormatCode());
+            assertEquals("#,##0", cache.getPtArray(1).getFormatCode());
+            assertFalse(cache.getPtArray(2).isSetFormatCode());
+
+            ds.setFormatCode(null);
+            cache = CTNumData.Factory.newInstance();
+            cache.setFormatCode("General");
+            ds.fillNumericalCache(cache);
+            assertFalse(cache.isSetFormatCode());
+            assertEquals("#,##0.###", cache.getPtArray(0).getFormatCode());
+        }
+    }
+
     private <T> void assertDataSourceIsEqualToArray(XDDFDataSource<T> ds, T[] 
array) {
         assertEquals(ds.getPointCount(), array.length);
         for (int i = 0; i < array.length; ++i) {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to