]> source.dussan.org Git - poi.git/commitdiff
Make a bit of a start on being able to edit chart titles, based on the email to user...
authorNick Burch <nick@apache.org>
Thu, 3 Apr 2008 15:04:52 +0000 (15:04 +0000)
committerNick Burch <nick@apache.org>
Thu, 3 Apr 2008 15:04:52 +0000 (15:04 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@644343 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/src/org/apache/poi/hssf/usermodel/HSSFChart.java
src/scratchpad/testcases/org/apache/poi/hssf/usermodel/TestHSSFChart.java [new file with mode: 0644]
src/testcases/org/apache/poi/hssf/data/WithThreeCharts.xls [new file with mode: 0755]
src/testcases/org/apache/poi/hssf/data/WithThreeCharts.xlsx [new file with mode: 0755]

index bf9b0cc2369e4de84f2cb0d880ccf004dc201c95..d708a5c1daf31f8ba73675101c1b77f17075c336 100644 (file)
@@ -23,6 +23,7 @@ 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;
 
@@ -33,6 +34,15 @@ import java.util.Stack;
  */
 public class HSSFChart
 {
+       private ChartRecord chartRecord;
+       private SeriesRecord seriesRecord;
+       
+       private ChartTitleFormatRecord chartTitleFormat;
+       private SeriesTextRecord chartTitleText;
+       
+       private HSSFChart(ChartRecord chartRecord) {
+               this.chartRecord = chartRecord;
+       }
 
     /**
      * Creates a bar chart.  API needs some work. :)
@@ -107,6 +117,74 @@ public class HSSFChart
         sheet.insertChartRecords( records );
         workbook.insertChartRecord();
     }
+    
+    /**
+     * Returns all the charts for the given sheet.
+     * 
+     * NOTE:  Does not yet work...  checking it in just so others
+     * can take a look.
+     */
+    public static HSSFChart[] getSheetCharts(HSSFSheet sheet) {
+       List charts = new ArrayList();
+       HSSFChart lastChart = null;
+       
+       // Find records of interest
+       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 SeriesRecord) {
+                       lastChart.seriesRecord = (SeriesRecord)r;
+               }
+               if(r instanceof ChartTitleFormatRecord) {
+                       lastChart.chartTitleFormat =
+                               (ChartTitleFormatRecord)r;
+               }
+               if(r instanceof SeriesTextRecord) {
+                       lastChart.chartTitleText =
+                               (SeriesTextRecord)r;
+               }
+       }
+       
+       return (HSSFChart[])
+               charts.toArray( new HSSFChart[charts.size()] );
+    }
+    
+    
+    /**
+     * Returns the chart's title, if there is one,
+     *  or null if not
+     */
+    public String getChartTitle() {
+       if(chartTitleText != null) {
+               return chartTitleText.getText();
+       }
+       return null;
+    }
+    
+    /**
+     * Changes the chart's title, but only if there 
+     *  was one already.
+     * TODO - add in the records if not
+     */
+    public void setChartTitle(String title) {
+       if(chartTitleText != null) {
+               chartTitleText.setText(title);
+       } else {
+               throw new IllegalStateException("No chart title found to change");
+       }
+    }
+    
+    
 
     private EOFRecord createEOFRecord()
     {
diff --git a/src/scratchpad/testcases/org/apache/poi/hssf/usermodel/TestHSSFChart.java b/src/scratchpad/testcases/org/apache/poi/hssf/usermodel/TestHSSFChart.java
new file mode 100644 (file)
index 0000000..2a069c3
--- /dev/null
@@ -0,0 +1,61 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+package org.apache.poi.hssf.usermodel;
+
+import java.io.File;
+import java.io.FileInputStream;
+
+import junit.framework.TestCase;
+
+public class TestHSSFChart extends TestCase {
+       private String dirName;
+
+       protected void setUp() throws Exception {
+               dirName = System.getProperty("HSSF.testdata.path");
+       }
+
+       public void testSingleChart() throws Exception {
+               
+       }
+
+       public void testTwoCharts() throws Exception {
+               
+       }
+
+       public void BROKENtestThreeCharts() throws Exception {
+               HSSFWorkbook wb = new HSSFWorkbook(
+                               new FileInputStream(new File(dirName, "WithThreeCharts.xls"))
+               );
+               
+               HSSFSheet s1 = wb.getSheetAt(0);
+               HSSFSheet s2 = wb.getSheetAt(1);
+               HSSFSheet s3 = wb.getSheetAt(2);
+               
+               assertEquals(0, HSSFChart.getSheetCharts(s1).length);
+               assertEquals(2, HSSFChart.getSheetCharts(s2).length);
+               assertEquals(1, HSSFChart.getSheetCharts(s3).length);
+               
+               HSSFChart[] charts;
+               
+               charts = HSSFChart.getSheetCharts(s2);
+               assertNull(charts[0].getChartTitle());
+               assertEquals("Pie Chart Title Thingy", charts[1].getChartTitle());
+               
+               charts = HSSFChart.getSheetCharts(s3);
+               assertEquals("Sheet 3 Chart with Title", charts[1].getChartTitle());
+       }
+}
diff --git a/src/testcases/org/apache/poi/hssf/data/WithThreeCharts.xls b/src/testcases/org/apache/poi/hssf/data/WithThreeCharts.xls
new file mode 100755 (executable)
index 0000000..157bf33
Binary files /dev/null and b/src/testcases/org/apache/poi/hssf/data/WithThreeCharts.xls differ
diff --git a/src/testcases/org/apache/poi/hssf/data/WithThreeCharts.xlsx b/src/testcases/org/apache/poi/hssf/data/WithThreeCharts.xlsx
new file mode 100755 (executable)
index 0000000..e7398e2
Binary files /dev/null and b/src/testcases/org/apache/poi/hssf/data/WithThreeCharts.xlsx differ