pjfanning commented on code in PR #974:
URL: https://github.com/apache/poi/pull/974#discussion_r2630358005


##########
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFChart.java:
##########
@@ -258,12 +270,76 @@ public long getChartRightMargin(long margin) {
      * @param right  margin from right
      * @param bottom margin from bottom
      * @param left   margin from left
-     * @since 4.0.0
+     * @since POI 4.0.0
      */
     public void setChartMargin(long top, long right, long bottom, long left) {
         this.setChartBottomMargin(bottom);
         this.setChartRightMargin(right);
         this.setChartLeftMargin(left);
         this.setChartRightMargin(right);
     }
+    /**
+        * Word keeps cached values that need be refreshed after the embedded 
workbook
+        * has been updated. This is just the first step refreshing categories 
and
+        * series labels (tested for bar charts). TODO ultimately a function 
should be
+        * provided to refresh the entire cache
+        */
+       public void refreshCachedLabels() {
+               Workbook wb;
+               try {
+                       wb = getWorkbook();
+               } catch (InvalidFormatException | IOException e) {
+                       return;
+               }
+               for (XDDFChartData chartData : getChartSeries()) {
+                       for (int chartIndex = 0; chartIndex < 
chartData.getSeriesCount(); chartIndex++) {
+                               XDDFChartData.Series series = 
chartData.getSeries(chartIndex);
+                               String freshSeriesTitle=getWorkbookCellValue( 
series.getTitleReference(),wb);
+                               series.setTitleCached(freshSeriesTitle);
+                               XDDFDataSource<?> catData = 
series.getCategoryData();
+                               if (catData == null)
+                                       continue;
+                               String referenceFormula = catData.getFormula();
+                               if (referenceFormula != null) {
+                                       List<Cell> cells = 
getWorkbookCells(referenceFormula, wb);
+                                       String[] freshCategories = new 
String[cells.size()];
+                                       int categIndex = 0;
+                                       for (Cell cell : cells) {
+                                               freshCategories[categIndex++] = 
cell.toString();
+                                       }
+                                       // create categories from a range 
formula and the values below, it will create then as "cached" values like word 
expects
+                                       XDDFDataSource<String> 
freshCategoryDataSrouce = 
XDDFDataSourcesFactory.fromArray(freshCategories,referenceFormula);
+                                       
series.replaceData(freshCategoryDataSrouce, (XDDFNumericalDataSource<?>) 
series.getValuesData());
+                               }
+                       }
+                       //this mystery "plot" will somehow update the 
underlying document XML
+                       plot(chartData);
+               }
+       }
+       /**
+        * 
+        * @param cell must include the sheet reference
+        * @param wb
+        * @return toString() value of the cell
+        */
+       private static String getWorkbookCellValue(CellReference cell, Workbook 
wb) {
+               
+               return wb.getSheet( 
cell.getSheetName()).getRow(cell.getRow()).getCell(cell.getCol()).toString();
+               
+       }
+       /**

Review Comment:
   whitespace between methods - no need for all the whitespace inside the 
method - please try to make the code follow the existing style in classes



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to