Line 0
Link Here
|
|
|
1 |
/* ==================================================================== |
2 |
Licensed to the Apache Software Foundation (ASF) under one or more |
3 |
contributor license agreements. See the NOTICE file distributed with |
4 |
this work for additional information regarding copyright ownership. |
5 |
The ASF licenses this file to You under the Apache License, Version 2.0 |
6 |
(the "License"); you may not use this file except in compliance with |
7 |
the License. You may obtain a copy of the License at |
8 |
|
9 |
http://www.apache.org/licenses/LICENSE-2.0 |
10 |
|
11 |
Unless required by applicable law or agreed to in writing, software |
12 |
distributed under the License is distributed on an "AS IS" BASIS, |
13 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 |
See the License for the specific language governing permissions and |
15 |
limitations under the License. |
16 |
==================================================================== */ |
17 |
|
18 |
package org.apache.poi.xssf.usermodel.charts; |
19 |
|
20 |
import java.util.ArrayList; |
21 |
import java.util.List; |
22 |
|
23 |
import org.apache.poi.ss.usermodel.Chart; |
24 |
import org.apache.poi.ss.usermodel.charts.ChartAxis; |
25 |
import org.apache.poi.ss.usermodel.charts.ChartDataSource; |
26 |
import org.apache.poi.ss.usermodel.charts.LineChartData; |
27 |
import org.apache.poi.ss.usermodel.charts.LineChartSerie; |
28 |
import org.apache.poi.util.Beta; |
29 |
import org.apache.poi.xssf.usermodel.XSSFChart; |
30 |
import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource; |
31 |
import org.openxmlformats.schemas.drawingml.x2006.chart.CTLineChart; |
32 |
import org.openxmlformats.schemas.drawingml.x2006.chart.CTLineSer; |
33 |
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource; |
34 |
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea; |
35 |
import org.openxmlformats.schemas.drawingml.x2006.chart.STMarkerStyle; |
36 |
|
37 |
/** |
38 |
* |
39 |
* @author Martin Andersson |
40 |
*/ |
41 |
@Beta |
42 |
public class XSSFLineChartData implements LineChartData { |
43 |
|
44 |
/** |
45 |
* List of all data series. |
46 |
*/ |
47 |
private List<Serie> series; |
48 |
|
49 |
public XSSFLineChartData() { |
50 |
series = new ArrayList<Serie>(); |
51 |
} |
52 |
|
53 |
static class Serie implements LineChartSerie { |
54 |
private int id; |
55 |
private int order; |
56 |
private ChartDataSource<?> categories; |
57 |
private ChartDataSource<? extends Number> values; |
58 |
|
59 |
protected Serie(int id, int order, |
60 |
ChartDataSource<?> categories, |
61 |
ChartDataSource<? extends Number> values) { |
62 |
this.id = id; |
63 |
this.order = order; |
64 |
this.categories = categories; |
65 |
this.values = values; |
66 |
} |
67 |
|
68 |
public ChartDataSource<?> getCategoryAxisData() { |
69 |
return categories; |
70 |
} |
71 |
|
72 |
public ChartDataSource<? extends Number> getValues() { |
73 |
return values; |
74 |
} |
75 |
|
76 |
protected void addToChart(CTLineChart ctLineChart) { |
77 |
CTLineSer ctLineSer = ctLineChart.addNewSer(); |
78 |
ctLineSer.addNewIdx().setVal(id); |
79 |
ctLineSer.addNewOrder().setVal(order); |
80 |
|
81 |
// No marker symbol on the chart line. |
82 |
ctLineSer.addNewMarker().addNewSymbol().setVal(STMarkerStyle.NONE); |
83 |
|
84 |
CTAxDataSource catDS = ctLineSer.addNewCat(); |
85 |
XSSFChartUtil.buildAxDataSource(catDS, categories); |
86 |
CTNumDataSource valueDS = ctLineSer.addNewVal(); |
87 |
XSSFChartUtil.buildNumDataSource(valueDS, values); |
88 |
} |
89 |
} |
90 |
|
91 |
public LineChartSerie addSerie(ChartDataSource<?> categoryAxisData, ChartDataSource<? extends Number> values) { |
92 |
if (!values.isNumeric()) { |
93 |
throw new IllegalArgumentException("Value data source must be numeric."); |
94 |
} |
95 |
int numOfSeries = series.size(); |
96 |
Serie newSerie = new Serie(numOfSeries, numOfSeries, categoryAxisData, values); |
97 |
series.add(newSerie); |
98 |
return newSerie; |
99 |
} |
100 |
|
101 |
public List<? extends LineChartSerie> getSeries() { |
102 |
return series; |
103 |
} |
104 |
|
105 |
public void fillChart(Chart chart, ChartAxis... axis) { |
106 |
if (!(chart instanceof XSSFChart)) { |
107 |
throw new IllegalArgumentException("Chart must be instance of XSSFChart"); |
108 |
} |
109 |
|
110 |
XSSFChart xssfChart = (XSSFChart) chart; |
111 |
CTPlotArea plotArea = xssfChart.getCTChart().getPlotArea(); |
112 |
CTLineChart lineChart = plotArea.addNewLineChart(); |
113 |
lineChart.addNewVaryColors().setVal(false); |
114 |
|
115 |
for (Serie s : series) { |
116 |
s.addToChart(lineChart); |
117 |
} |
118 |
|
119 |
for (ChartAxis ax : axis) { |
120 |
lineChart.addNewAxId().setVal(ax.getId()); |
121 |
} |
122 |
} |
123 |
} |