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 |
} |