View | Details | Raw Unified | Return to bug 57989
Collapse All | Expand All

(-)src/ooxml/testcases/org/apache/poi/xssf/usermodel/charts/TestXSSFChartTitle.java (+118 lines)
Line 0 Link Here
1
package org.apache.poi.xssf.usermodel.charts;
2
3
import junit.framework.TestCase;
4
import org.apache.poi.ss.usermodel.*;
5
import org.apache.poi.ss.usermodel.charts.*;
6
import org.apache.poi.ss.util.CellRangeAddress;
7
import org.apache.poi.xssf.XSSFTestDataSamples;
8
import org.apache.poi.xssf.usermodel.*;
9
10
import java.util.List;
11
12
/**
13
 * Test get/set chart title.
14
 *
15
 * @author Jim King
16
 */
17
public class TestXSSFChartTitle extends TestCase {
18
19
    private Workbook createWorkbookWithChart() {
20
        Workbook wb = new XSSFWorkbook();
21
        Sheet sheet = wb.createSheet("linechart");
22
        final int NUM_OF_ROWS = 3;
23
        final int NUM_OF_COLUMNS = 10;
24
25
        // Create a row and put some cells in it. Rows are 0 based.
26
        Row row;
27
        Cell cell;
28
        for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) {
29
            row = sheet.createRow((short) rowIndex);
30
            for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
31
                cell = row.createCell((short) colIndex);
32
                cell.setCellValue(colIndex * (rowIndex + 1));
33
            }
34
        }
35
36
        Drawing drawing = sheet.createDrawingPatriarch();
37
        ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);
38
39
        Chart chart = drawing.createChart(anchor);
40
        ChartLegend legend = chart.getOrCreateLegend();
41
        legend.setPosition(LegendPosition.TOP_RIGHT);
42
43
        LineChartData data = chart.getChartDataFactory().createLineChartData();
44
45
        // Use a category axis for the bottom axis.
46
        ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
47
        ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
48
        leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
49
50
        ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
51
        ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
52
        ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));
53
54
        data.addSeries(xs, ys1);
55
        data.addSeries(xs, ys2);
56
57
        chart.plot(data, bottomAxis, leftAxis);
58
59
        return wb;
60
    }
61
62
    /**
63
     * Gets the first chart from the named sheet in the workbook.
64
     */
65
    private XSSFChart getChartFromWorkbook(Workbook wb, String sheetName) {
66
        Sheet sheet = wb.getSheet(sheetName);
67
        if (sheet instanceof XSSFSheet) {
68
            XSSFSheet xsheet = (XSSFSheet) sheet;
69
            XSSFDrawing drawing = xsheet.getDrawingPatriarch();
70
            if (drawing != null) {
71
                List<XSSFChart> charts = drawing.getCharts();
72
                if (charts != null && charts.size() > 0) {
73
                    return charts.get(0);
74
                }
75
            }
76
        }
77
        return null;
78
    }
79
80
    public void testNewChart() {
81
        Workbook wb = createWorkbookWithChart();
82
        XSSFChart chart = getChartFromWorkbook(wb, "linechart");
83
        assertNotNull(chart);
84
        assertNull(chart.getTitle());
85
        final String myTitle = "My chart title";
86
        chart.setTitle(myTitle);
87
        XSSFRichTextString queryTitle = chart.getTitle();
88
        assertNotNull(queryTitle);
89
        assertEquals(myTitle, queryTitle.toString());
90
    }
91
92
    public void testExistingChartWithTitle() {
93
        Workbook wb = XSSFTestDataSamples.openSampleWorkbook("chartTitle_withTitle.xlsx");
94
        XSSFChart chart = getChartFromWorkbook(wb, "Sheet1");
95
        assertNotNull(chart);
96
        XSSFRichTextString originalTitle = chart.getTitle();
97
        assertNotNull(originalTitle);
98
        final String myTitle = "My chart title";
99
        assertFalse(myTitle.equals(originalTitle.toString()));
100
        chart.setTitle(myTitle);
101
        XSSFRichTextString queryTitle = chart.getTitle();
102
        assertNotNull(queryTitle);
103
        assertEquals(myTitle, queryTitle.toString());
104
    }
105
106
    public void testExistingChartNoTitle() {
107
        Workbook wb = XSSFTestDataSamples.openSampleWorkbook("chartTitle_noTitle.xlsx");
108
        XSSFChart chart = getChartFromWorkbook(wb, "Sheet1");
109
        assertNotNull(chart);
110
        assertNull(chart.getTitle());
111
        final String myTitle = "My chart title";
112
        chart.setTitle(myTitle);
113
        XSSFRichTextString queryTitle = chart.getTitle();
114
        assertNotNull(queryTitle);
115
        assertEquals(myTitle, queryTitle.toString());
116
    }
117
118
}
(-)src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFChart.java (+54 lines)
Lines 51-58 Link Here
51
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
51
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
52
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPrintSettings;
52
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPrintSettings;
53
import org.openxmlformats.schemas.drawingml.x2006.chart.CTTitle;
53
import org.openxmlformats.schemas.drawingml.x2006.chart.CTTitle;
54
import org.openxmlformats.schemas.drawingml.x2006.chart.CTTx;
54
import org.openxmlformats.schemas.drawingml.x2006.chart.CTValAx;
55
import org.openxmlformats.schemas.drawingml.x2006.chart.CTValAx;
55
import org.openxmlformats.schemas.drawingml.x2006.chart.ChartSpaceDocument;
56
import org.openxmlformats.schemas.drawingml.x2006.chart.ChartSpaceDocument;
57
import org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun;
58
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
59
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextField;
60
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph;
56
import org.openxmlformats.schemas.officeDocument.x2006.relationships.STRelationshipId;
61
import org.openxmlformats.schemas.officeDocument.x2006.relationships.STRelationshipId;
57
import org.w3c.dom.NodeList;
62
import org.w3c.dom.NodeList;
58
import org.w3c.dom.Text;
63
import org.w3c.dom.Text;
Lines 277-282 Link Here
277
		}
282
		}
278
283
279
		return new XSSFRichTextString(text.toString());
284
		return new XSSFRichTextString(text.toString());
285
	}
286
287
	/**
288
	 * Sets the title text.
289
	 */
290
	public void setTitle(String newTitle) {
291
		CTTitle ctTitle;
292
		if (chart.isSetTitle()) {
293
			ctTitle = chart.getTitle();
294
		} else {
295
			ctTitle = chart.addNewTitle();
296
		}
297
298
		CTTx tx;
299
		if (ctTitle.isSetTx()) {
300
			tx = ctTitle.getTx();
301
		} else {
302
			tx = ctTitle.addNewTx();
303
		}
304
305
		if (tx.isSetStrRef()) {
306
			tx.unsetStrRef();
307
		}
308
309
		CTTextBody rich;
310
		if (tx.isSetRich()) {
311
			rich = tx.getRich();
312
		} else {
313
			rich = tx.addNewRich();
314
			rich.addNewBodyPr();  // body properties must exist (but can be empty)
315
		}
316
317
		CTTextParagraph para;
318
		if (rich.sizeOfPArray() > 0) {
319
			para = rich.getPArray(0);
320
		} else {
321
			para = rich.addNewP();
322
		}
323
324
		if (para.sizeOfRArray() > 0) {
325
			CTRegularTextRun run = para.getRArray(0);
326
			run.setT(newTitle);
327
		} else if (para.sizeOfFldArray() > 0) {
328
			CTTextField fld = para.getFldArray(0);
329
			fld.setT(newTitle);
330
		} else {
331
			CTRegularTextRun run = para.addNewR();
332
			run.setT(newTitle);
333
		}
280
	}
334
	}
281
335
282
	public XSSFChartLegend getOrCreateLegend() {
336
	public XSSFChartLegend getOrCreateLegend() {

Return to bug 57989