Make a bit of a start on being able to edit chart titles, based on the email to user@poi from Russ on the 2nd of April. Not quite there though

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@644343 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2008-04-03 15:04:52 +00:00
parent a4cb19f0e8
commit 78c69f1230
4 changed files with 139 additions and 0 deletions

View 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()
{

View File

@ -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());
}
}

Binary file not shown.

Binary file not shown.