]> source.dussan.org Git - poi.git/commitdiff
Initial support for getting and changing chart titles and series titles
authorNick Burch <nick@apache.org>
Thu, 10 Apr 2008 15:54:02 +0000 (15:54 +0000)
committerNick Burch <nick@apache.org>
Thu, 10 Apr 2008 15:54:02 +0000 (15:54 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@646854 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/changes.xml
src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/hssf/record/DrawingRecordForBiffViewer.java
src/java/org/apache/poi/hssf/record/RecordFactory.java
src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
src/scratchpad/src/org/apache/poi/hssf/usermodel/HSSFChart.java
src/scratchpad/testcases/org/apache/poi/hssf/usermodel/TestHSSFChart.java

index 35e5a8f01576ff912dfec808adadadc8e3f5bf34..c3b1f72e846fb934b6eb1de9e21c0787cb6a4a22 100644 (file)
@@ -37,6 +37,8 @@
 
                <!-- Don't forget to update status.xml too! -->
         <release version="3.0.3-beta1" date="2008-04-??">
+           <action dev="POI-DEVELOPERS" type="add">Initial support for getting and changing chart and series titles</action>
+           <action dev="POI-DEVELOPERS" type="add">Implement a proxy HSSFListener which tracks the format records, and lets you lookup the format string for a given cell. Convert the xls to csv example to use it</action>
            <action dev="POI-DEVELOPERS" type="fix">44792 - fixed encode/decode problems in ExternalNameRecord and CRNRecord.</action>
            <action dev="POI-DEVELOPERS" type="fix">43670, 44501 - Fix how HDGF deals with trailing data in the list of chunk headers</action>
            <action dev="POI-DEVELOPERS" type="add">30311 - More work on Conditional Formatting</action>
index c102f37e86406eb36121d0aa15236ee2748107a7..2ceb4ea9e568a0cdb993f3226b6121b9a714a985 100644 (file)
@@ -34,6 +34,8 @@
        <!-- Don't forget to update changes.xml too! -->
     <changes>
         <release version="3.0.3-beta1" date="2008-04-??">
+           <action dev="POI-DEVELOPERS" type="add">Initial support for getting and changing chart and series titles</action>
+           <action dev="POI-DEVELOPERS" type="add">Implement a proxy HSSFListener which tracks the format records, and lets you lookup the format string for a given cell. Convert the xls to csv example to use it</action>
            <action dev="POI-DEVELOPERS" type="fix">44792 - fixed encode/decode problems in ExternalNameRecord and CRNRecord.</action>
            <action dev="POI-DEVELOPERS" type="fix">43670, 44501 - Fix how HDGF deals with trailing data in the list of chunk headers</action>
            <action dev="POI-DEVELOPERS" type="add">30311 - More work on Conditional Formatting</action>
index b0a1bbf2e65fabde78beff1c377f0d3a6a28301c..4e5536f3096a774757b0bd6b4ad71f4265d941e8 100644 (file)
@@ -17,6 +17,8 @@
 
 package org.apache.poi.hssf.record;
 
+import java.io.ByteArrayInputStream;
+
 /**
  * This is purely for the biff viewer.  During normal operations we don't want
  * to be seeing this.
@@ -35,6 +37,21 @@ public class DrawingRecordForBiffViewer
         super(in);
     }
 
+    public DrawingRecordForBiffViewer(DrawingRecord r)
+    {
+       super(convertToInputStream(r));
+       convertRawBytesToEscherRecords();
+    }
+    private static RecordInputStream convertToInputStream(DrawingRecord r)
+    {
+       byte[] data = r.serialize();
+       RecordInputStream rinp = new RecordInputStream(
+                       new ByteArrayInputStream(data)
+       );
+       rinp.nextRecord();
+       return rinp;
+    }
+
     protected String getRecordName()
     {
         return "MSODRAWING";
index 1dada4450f5fd7aa23381cb0297d10e8c5fa1d10..5a627988f22f5586531dbf904f0d2eef927689e1 100644 (file)
@@ -77,6 +77,8 @@ public class RecordFactory
                 NoteRecord.class, ObjectProtectRecord.class, ScenarioProtectRecord.class, 
                 FileSharingRecord.class, ChartTitleFormatRecord.class,
                 DVRecord.class, DVALRecord.class, UncalcedRecord.class,
+                ChartRecord.class, LegendRecord.class, ChartTitleFormatRecord.class, 
+                SeriesRecord.class, SeriesTextRecord.class,
                 HyperlinkRecord.class,
                 ExternalNameRecord.class, // TODO - same changes in non-@deprecated version of this class
                 SupBookRecord.class,
index 856ada17066f8d39833c9daddb7683eb1a03cacd..197139ccc3a29f0d205215bbeed8e9af0839a3db 100644 (file)
@@ -1554,18 +1554,14 @@ public final class HSSFSheet {
     }
 
     /**
-     * Returns the top-level drawing patriach, if there is
-     *  one.
-     * This will hold any graphics or charts for the sheet.
+     * Returns the agregate escher records for this sheet,
+     *  it there is one.
      * WARNING - calling this will trigger a parsing of the
      *  associated escher records. Any that aren't supported
      *  (such as charts and complex drawing types) will almost
-     *  certainly be lost or corrupted when written out. Only
-     *  use this with simple drawings, otherwise call
-     *  {@link HSSFSheet#createDrawingPatriarch()} and
-     *  start from scratch!
+     *  certainly be lost or corrupted when written out.
      */
-    public HSSFPatriarch getDrawingPatriarch() {
+    public EscherAggregate getDrawingEscherAggregate() {
         book.findDrawingGroup();
 
         // If there's now no drawing manager, then there's
@@ -1584,6 +1580,25 @@ public final class HSSFSheet {
 
         // Grab our aggregate record, and wire it up
         EscherAggregate agg = (EscherAggregate) sheet.findFirstRecordBySid(EscherAggregate.sid);
+        return agg;
+    }
+    
+    /**
+     * Returns the top-level drawing patriach, if there is
+     *  one.
+     * This will hold any graphics or charts for the sheet.
+     * WARNING - calling this will trigger a parsing of the
+     *  associated escher records. Any that aren't supported
+     *  (such as charts and complex drawing types) will almost
+     *  certainly be lost or corrupted when written out. Only
+     *  use this with simple drawings, otherwise call
+     *  {@link HSSFSheet#createDrawingPatriarch()} and
+     *  start from scratch!
+     */
+    public HSSFPatriarch getDrawingPatriarch() {
+       EscherAggregate agg = getDrawingEscherAggregate();
+       if(agg == null) return null;
+       
         HSSFPatriarch patriarch = new HSSFPatriarch(this, agg);
         agg.setPatriarch(patriarch);
 
index d708a5c1daf31f8ba73675101c1b77f17075c336..5b48501938bf0863e51cf6094cbb92d107f050c8 100644 (file)
 
 package org.apache.poi.hssf.usermodel;
 
-import org.apache.poi.hssf.record.*;
-import org.apache.poi.hssf.record.formula.Area3DPtg;
-
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Stack;
 
+import org.apache.poi.hssf.record.AreaFormatRecord;
+import org.apache.poi.hssf.record.AxisLineFormatRecord;
+import org.apache.poi.hssf.record.AxisOptionsRecord;
+import org.apache.poi.hssf.record.AxisParentRecord;
+import org.apache.poi.hssf.record.AxisRecord;
+import org.apache.poi.hssf.record.AxisUsedRecord;
+import org.apache.poi.hssf.record.BOFRecord;
+import org.apache.poi.hssf.record.BarRecord;
+import org.apache.poi.hssf.record.BeginRecord;
+import org.apache.poi.hssf.record.CategorySeriesAxisRecord;
+import org.apache.poi.hssf.record.ChartFormatRecord;
+import org.apache.poi.hssf.record.ChartRecord;
+import org.apache.poi.hssf.record.ChartTitleFormatRecord;
+import org.apache.poi.hssf.record.DataFormatRecord;
+import org.apache.poi.hssf.record.DefaultDataLabelTextPropertiesRecord;
+import org.apache.poi.hssf.record.DimensionsRecord;
+import org.apache.poi.hssf.record.EOFRecord;
+import org.apache.poi.hssf.record.EndRecord;
+import org.apache.poi.hssf.record.FontBasisRecord;
+import org.apache.poi.hssf.record.FontIndexRecord;
+import org.apache.poi.hssf.record.FooterRecord;
+import org.apache.poi.hssf.record.FrameRecord;
+import org.apache.poi.hssf.record.HCenterRecord;
+import org.apache.poi.hssf.record.HeaderRecord;
+import org.apache.poi.hssf.record.LegendRecord;
+import org.apache.poi.hssf.record.LineFormatRecord;
+import org.apache.poi.hssf.record.LinkedDataFormulaField;
+import org.apache.poi.hssf.record.LinkedDataRecord;
+import org.apache.poi.hssf.record.PlotAreaRecord;
+import org.apache.poi.hssf.record.PlotGrowthRecord;
+import org.apache.poi.hssf.record.PrintSetupRecord;
+import org.apache.poi.hssf.record.ProtectRecord;
+import org.apache.poi.hssf.record.Record;
+import org.apache.poi.hssf.record.SCLRecord;
+import org.apache.poi.hssf.record.SeriesIndexRecord;
+import org.apache.poi.hssf.record.SeriesRecord;
+import org.apache.poi.hssf.record.SeriesTextRecord;
+import org.apache.poi.hssf.record.SeriesToChartGroupRecord;
+import org.apache.poi.hssf.record.SheetPropertiesRecord;
+import org.apache.poi.hssf.record.TextRecord;
+import org.apache.poi.hssf.record.TickRecord;
+import org.apache.poi.hssf.record.UnitsRecord;
+import org.apache.poi.hssf.record.UnknownRecord;
+import org.apache.poi.hssf.record.VCenterRecord;
+import org.apache.poi.hssf.record.ValueRangeRecord;
+import org.apache.poi.hssf.record.formula.Area3DPtg;
+
 /**
  * Has methods for construction of a chart object.
  *
@@ -35,11 +79,13 @@ import java.util.Stack;
 public class HSSFChart
 {
        private ChartRecord chartRecord;
-       private SeriesRecord seriesRecord;
        
+       private LegendRecord legendRecord;
        private ChartTitleFormatRecord chartTitleFormat;
        private SeriesTextRecord chartTitleText;
        
+       private List series = new ArrayList();
+       
        private HSSFChart(ChartRecord chartRecord) {
                this.chartRecord = chartRecord;
        }
@@ -121,8 +167,8 @@ public class HSSFChart
     /**
      * Returns all the charts for the given sheet.
      * 
-     * NOTE:  Does not yet work...  checking it in just so others
-     * can take a look.
+     * NOTE: You won't be able to do very much with
+     *  these charts yet, as this is very limited support
      */
     public static HSSFChart[] getSheetCharts(HSSFSheet sheet) {
        List charts = new ArrayList();
@@ -132,33 +178,49 @@ public class HSSFChart
        List records = sheet.getSheet().getRecords();
        for(Iterator it = records.iterator(); it.hasNext();) {
                Record r = (Record)it.next();
-               System.err.println(r);
-               
-               if(r instanceof DrawingRecord) {
-                       DrawingRecord dr = (DrawingRecord)r;
-               }
                
                if(r instanceof ChartRecord) {
                        lastChart = new HSSFChart((ChartRecord)r);
                        charts.add(lastChart);
                }
+               if(r instanceof LegendRecord) {
+                       lastChart.legendRecord = (LegendRecord)r;
+               }
                if(r instanceof SeriesRecord) {
-                       lastChart.seriesRecord = (SeriesRecord)r;
+                       HSSFSeries series = lastChart.new HSSFSeries( (SeriesRecord)r );
+                       lastChart.series.add(series);
                }
                if(r instanceof ChartTitleFormatRecord) {
                        lastChart.chartTitleFormat =
                                (ChartTitleFormatRecord)r;
                }
                if(r instanceof SeriesTextRecord) {
-                       lastChart.chartTitleText =
-                               (SeriesTextRecord)r;
+                       // Applies to a series, unless we've seen
+                       //  a legend already
+                       SeriesTextRecord str = (SeriesTextRecord)r;
+                       if(lastChart.legendRecord == null &&
+                                       lastChart.series.size() > 0) {
+                               HSSFSeries series = (HSSFSeries)
+                                       lastChart.series.get(lastChart.series.size()-1);
+                               series.seriesTitleText = str;
+                       } else {
+                               lastChart.chartTitleText = str;
+                       }
                }
        }
        
        return (HSSFChart[])
                charts.toArray( new HSSFChart[charts.size()] );
     }
+
     
+    /**
+     * Returns the series of the chart
+     */
+    public HSSFSeries[] getSeries() {
+       return (HSSFSeries[])
+               series.toArray(new HSSFSeries[series.size()]);
+    }
     
     /**
      * Returns the chart's title, if there is one,
@@ -184,7 +246,6 @@ public class HSSFChart
        }
     }
     
-    
 
     private EOFRecord createEOFRecord()
     {
@@ -858,4 +919,51 @@ public class HSSFChart
         r.setUnits( (short) 0 );
         return r;
     }
+
+    
+    /**
+     * A series in a chart
+     */
+    public class HSSFSeries {
+       private SeriesRecord series;
+       private SeriesTextRecord seriesTitleText;
+       
+       private HSSFSeries(SeriesRecord series) {
+               this.series = series;
+       }
+       
+       public short getNumValues() {
+               return series.getNumValues();
+       }
+       /**
+        * See {@link SeriesRecord}
+        */
+       public short getValueType() {
+               return series.getValuesDataType();
+       }
+       
+        /**
+         * Returns the series' title, if there is one,
+         *  or null if not
+         */
+        public String getSeriesTitle() {
+               if(seriesTitleText != null) {
+                       return seriesTitleText.getText();
+               }
+               return null;
+        }
+        
+        /**
+         * Changes the series' title, but only if there 
+         *  was one already.
+         * TODO - add in the records if not
+         */
+        public void setSeriesTitle(String title) {
+               if(seriesTitleText != null) {
+                       seriesTitleText.setText(title);
+               } else {
+                       throw new IllegalStateException("No series title found to change");
+               }
+        }
+    }
 }
index 2a069c3ff676c99064ef296470536a068a8c8457..184d46d2fe2008e9e650c766745ffadd0e6fb335 100644 (file)
@@ -19,6 +19,8 @@ package org.apache.poi.hssf.usermodel;
 import java.io.File;
 import java.io.FileInputStream;
 
+import org.apache.poi.hssf.record.SeriesRecord;
+
 import junit.framework.TestCase;
 
 public class TestHSSFChart extends TestCase {
@@ -29,14 +31,65 @@ public class TestHSSFChart extends TestCase {
        }
 
        public void testSingleChart() throws Exception {
+               HSSFWorkbook wb = new HSSFWorkbook(
+                               new FileInputStream(new File(dirName, "WithChart.xls"))
+               );
+               
+               HSSFSheet s1 = wb.getSheetAt(0);
+               HSSFSheet s2 = wb.getSheetAt(1);
+               HSSFSheet s3 = wb.getSheetAt(2);
+               
+               assertEquals(0, HSSFChart.getSheetCharts(s1).length);
+               assertEquals(1, HSSFChart.getSheetCharts(s2).length);
+               assertEquals(0, HSSFChart.getSheetCharts(s3).length);
                
+               HSSFChart[] charts;
+               
+               // Check the chart on the 2nd sheet
+               charts = HSSFChart.getSheetCharts(s2);
+               assertEquals(1, charts.length);
+               
+               assertEquals(2, charts[0].getSeries().length);
+               assertEquals("1st Column", charts[0].getSeries()[0].getSeriesTitle());
+               assertEquals("2nd Column", charts[0].getSeries()[1].getSeriesTitle());
+               assertEquals(null, charts[0].getChartTitle());
        }
 
        public void testTwoCharts() throws Exception {
+               HSSFWorkbook wb = new HSSFWorkbook(
+                               new FileInputStream(new File(dirName, "WithTwoCharts.xls"))
+               );
                
+               HSSFSheet s1 = wb.getSheetAt(0);
+               HSSFSheet s2 = wb.getSheetAt(1);
+               HSSFSheet s3 = wb.getSheetAt(2);
+               
+               assertEquals(0, HSSFChart.getSheetCharts(s1).length);
+               assertEquals(1, HSSFChart.getSheetCharts(s2).length);
+               assertEquals(1, HSSFChart.getSheetCharts(s3).length);
+               
+               HSSFChart[] charts;
+               
+               // Check the chart on the 2nd sheet
+               charts = HSSFChart.getSheetCharts(s2);
+               assertEquals(1, charts.length);
+               
+               assertEquals(2, charts[0].getSeries().length);
+               assertEquals("1st Column", charts[0].getSeries()[0].getSeriesTitle());
+               assertEquals("2nd Column", charts[0].getSeries()[1].getSeriesTitle());
+               assertEquals(null, charts[0].getChartTitle());
+               
+               // And the third sheet
+               charts = HSSFChart.getSheetCharts(s3);
+               assertEquals(1, charts.length);
+               
+               assertEquals(2, charts[0].getSeries().length);
+               assertEquals("Squares", charts[0].getSeries()[0].getSeriesTitle());
+               assertEquals("Base Numbers", charts[0].getSeries()[1].getSeriesTitle());
+               assertEquals(null, charts[0].getChartTitle());
        }
-
-       public void BROKENtestThreeCharts() throws Exception {
+       
+       public void testThreeCharts() throws Exception {
                HSSFWorkbook wb = new HSSFWorkbook(
                                new FileInputStream(new File(dirName, "WithThreeCharts.xls"))
                );
@@ -51,11 +104,30 @@ public class TestHSSFChart extends TestCase {
                
                HSSFChart[] charts;
                
+               // Check the charts on the 2nd sheet
                charts = HSSFChart.getSheetCharts(s2);
-               assertNull(charts[0].getChartTitle());
+               assertEquals(2, charts.length);
+               
+               assertEquals(2, charts[0].getSeries().length);
+               assertEquals("1st Column", charts[0].getSeries()[0].getSeriesTitle());
+               assertEquals("2nd Column", charts[0].getSeries()[1].getSeriesTitle());
+               assertEquals(6, charts[0].getSeries()[0].getNumValues());
+               assertEquals(6, charts[0].getSeries()[1].getNumValues());
+               assertEquals(SeriesRecord.CATEGORY_DATA_TYPE_NUMERIC, charts[0].getSeries()[0].getValueType());
+               assertEquals(SeriesRecord.CATEGORY_DATA_TYPE_NUMERIC, charts[0].getSeries()[1].getValueType());
+               assertEquals(null, charts[0].getChartTitle());
+               
+               assertEquals(1, charts[1].getSeries().length);
+               assertEquals(null, charts[1].getSeries()[0].getSeriesTitle());
                assertEquals("Pie Chart Title Thingy", charts[1].getChartTitle());
                
+               // And the third sheet
                charts = HSSFChart.getSheetCharts(s3);
-               assertEquals("Sheet 3 Chart with Title", charts[1].getChartTitle());
+               assertEquals(1, charts.length);
+               
+               assertEquals(2, charts[0].getSeries().length);
+               assertEquals("Squares", charts[0].getSeries()[0].getSeriesTitle());
+               assertEquals("Base Numbers", charts[0].getSeries()[1].getSeriesTitle());
+               assertEquals("Sheet 3 Chart with Title", charts[0].getChartTitle());
        }
 }