Lines 6-19
Link Here
|
6 |
(the "License"); you may not use this file except in compliance with |
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 |
7 |
the License. You may obtain a copy of the License at |
8 |
|
8 |
|
9 |
http://www.apache.org/licenses/LICENSE-2.0 |
9 |
http://www.apache.org/licenses/LICENSE-2.0 |
10 |
|
10 |
|
11 |
Unless required by applicable law or agreed to in writing, software |
11 |
Unless required by applicable law or agreed to in writing, software |
12 |
distributed under the License is distributed on an "AS IS" BASIS, |
12 |
distributed under the License is distributed on an "AS IS" BASIS, |
13 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 |
See the License for the specific language governing permissions and |
14 |
See the License for the specific language governing permissions and |
15 |
limitations under the License. |
15 |
limitations under the License. |
16 |
==================================================================== */ |
16 |
==================================================================== */ |
17 |
|
17 |
|
18 |
package org.apache.poi.xssf.usermodel.charts; |
18 |
package org.apache.poi.xssf.usermodel.charts; |
19 |
|
19 |
|
Lines 53-145
Link Here
|
53 |
*/ |
53 |
*/ |
54 |
public class XSSFScatterChartData implements ScatterChartData { |
54 |
public class XSSFScatterChartData implements ScatterChartData { |
55 |
|
55 |
|
56 |
/** |
56 |
/** |
57 |
* List of all data series. |
57 |
* List of all data series. |
58 |
*/ |
58 |
*/ |
59 |
private List<Serie> series; |
59 |
private List<Serie> series; |
60 |
|
60 |
|
61 |
public XSSFScatterChartData() { |
61 |
public XSSFScatterChartData() { |
62 |
series = new ArrayList<Serie>(); |
62 |
series = new ArrayList<Serie>(); |
|
|
63 |
} |
64 |
|
65 |
/** |
66 |
* Package private ScatterChartSerie implementation. |
67 |
*/ |
68 |
static class Serie implements ScatterChartSerie { |
69 |
private int id; |
70 |
private int order; |
71 |
private boolean useCache; |
72 |
private DataMarker xMarker; |
73 |
private DataMarker yMarker; |
74 |
private XSSFNumberCache lastCaclulatedXCache; |
75 |
private XSSFNumberCache lastCalculatedYCache; |
76 |
|
77 |
protected Serie(int id, int order) { |
78 |
super(); |
79 |
this.id = id; |
80 |
this.order = order; |
81 |
this.useCache = true; |
63 |
} |
82 |
} |
64 |
|
83 |
|
65 |
public static class Serie implements ScatterChartSerie { |
84 |
public void setXValues(DataMarker marker) { |
66 |
private int id; |
85 |
xMarker = marker; |
67 |
private int order; |
86 |
} |
68 |
private boolean useCache; |
|
|
69 |
private DataMarker xMarker; |
70 |
private DataMarker yMarker; |
71 |
|
87 |
|
72 |
public Serie(int id, int order) { |
88 |
public void setYValues(DataMarker marker) { |
73 |
super(); |
89 |
yMarker = marker; |
74 |
this.id = id; |
90 |
} |
75 |
this.order = order; |
|
|
76 |
this.useCache = false; |
77 |
} |
78 |
|
91 |
|
79 |
public void setXValues(DataMarker marker) { |
92 |
/** |
80 |
xMarker = marker; |
93 |
* @param useCache if true, cached results will be added on plot |
81 |
} |
94 |
*/ |
|
|
95 |
public void setUseCache(boolean useCache) { |
96 |
this.useCache = useCache; |
97 |
} |
82 |
|
98 |
|
83 |
public void setYValues(DataMarker marker) { |
99 |
/** |
84 |
yMarker = marker; |
100 |
* Returns last calculated number cache for X axis. |
85 |
} |
101 |
* @return last calculated number cache for X axis. |
|
|
102 |
*/ |
103 |
XSSFNumberCache getLastCaculatedXCache() { |
104 |
return lastCaclulatedXCache; |
105 |
} |
86 |
|
106 |
|
87 |
/** |
107 |
/** |
88 |
* @param useCache if true, cached results will be added on plot |
108 |
* Returns last calculated number cache for Y axis. |
89 |
*/ |
109 |
* @return last calculated number cache for Y axis. |
90 |
public void setUseCache(boolean useCache) { |
110 |
*/ |
91 |
this.useCache = useCache; |
111 |
XSSFNumberCache getLastCalculatedYCache() { |
92 |
} |
112 |
return lastCalculatedYCache; |
|
|
113 |
} |
93 |
|
114 |
|
94 |
protected void addToChart(CTScatterChart ctScatterChart) { |
115 |
protected void addToChart(CTScatterChart ctScatterChart) { |
95 |
CTScatterSer scatterSer = ctScatterChart.addNewSer(); |
116 |
CTScatterSer scatterSer = ctScatterChart.addNewSer(); |
96 |
scatterSer.addNewIdx().setVal(this.id); |
117 |
scatterSer.addNewIdx().setVal(this.id); |
97 |
scatterSer.addNewOrder().setVal(this.order); |
118 |
scatterSer.addNewOrder().setVal(this.order); |
98 |
|
119 |
|
99 |
CTAxDataSource xVal = scatterSer.addNewXVal(); |
120 |
/* TODO: add some logic to automatically recognize cell |
100 |
CTNumRef numRef = xVal.addNewNumRef(); |
121 |
* types and choose appropriate data representation for |
101 |
numRef.setF(xMarker.formatAsString()); |
122 |
* X axis. |
|
|
123 |
*/ |
124 |
CTAxDataSource xVal = scatterSer.addNewXVal(); |
125 |
CTNumRef xNumRef = xVal.addNewNumRef(); |
126 |
xNumRef.setF(xMarker.formatAsString()); |
102 |
|
127 |
|
103 |
CTNumDataSource yVal = scatterSer.addNewYVal(); |
128 |
CTNumDataSource yVal = scatterSer.addNewYVal(); |
104 |
numRef = yVal.addNewNumRef(); |
129 |
CTNumRef yNumRef = yVal.addNewNumRef(); |
105 |
numRef.setF(yMarker.formatAsString()); |
130 |
yNumRef.setF(yMarker.formatAsString()); |
106 |
} |
|
|
107 |
} |
108 |
|
131 |
|
109 |
public XSSFScatterChartData.Serie addSerie(DataMarker xMarker, DataMarker yMarker) { |
132 |
if (useCache) { |
110 |
int numOfSeries = series.size(); |
133 |
/* We can not store cache since markers are not immutable */ |
111 |
Serie newSerie = new Serie(numOfSeries, numOfSeries); |
134 |
XSSFNumberCache.buildCache(xMarker, xNumRef); |
112 |
newSerie.setXValues(xMarker); |
135 |
lastCalculatedYCache = XSSFNumberCache.buildCache(yMarker, yNumRef); |
113 |
newSerie.setYValues(yMarker); |
136 |
} |
114 |
series.add(newSerie); |
|
|
115 |
return newSerie; |
116 |
} |
137 |
} |
|
|
138 |
} |
117 |
|
139 |
|
118 |
public void fillChart(Chart chart, ChartAxis... axis) { |
140 |
public ScatterChartSerie addSerie(DataMarker xMarker, DataMarker yMarker) { |
119 |
if (!(chart instanceof XSSFChart)) { |
141 |
int numOfSeries = series.size(); |
120 |
throw new IllegalArgumentException("Chart must be instance of XSSFChart"); |
142 |
Serie newSerie = new Serie(numOfSeries, numOfSeries); |
121 |
} |
143 |
newSerie.setXValues(xMarker); |
|
|
144 |
newSerie.setYValues(yMarker); |
145 |
series.add(newSerie); |
146 |
return newSerie; |
147 |
} |
122 |
|
148 |
|
123 |
XSSFChart xssfChart = (XSSFChart) chart; |
149 |
public void fillChart(Chart chart, ChartAxis... axis) { |
124 |
CTPlotArea plotArea = xssfChart.getCTChart().getPlotArea(); |
150 |
if (!(chart instanceof XSSFChart)) { |
125 |
CTScatterChart scatterChart = plotArea.addNewScatterChart(); |
151 |
throw new IllegalArgumentException("Chart must be instance of XSSFChart"); |
126 |
addStyle(scatterChart); |
152 |
} |
127 |
|
153 |
|
128 |
for (Serie s : series) { |
154 |
XSSFChart xssfChart = (XSSFChart) chart; |
129 |
s.addToChart(scatterChart); |
155 |
CTPlotArea plotArea = xssfChart.getCTChart().getPlotArea(); |
130 |
} |
156 |
CTScatterChart scatterChart = plotArea.addNewScatterChart(); |
|
|
157 |
addStyle(scatterChart); |
131 |
|
158 |
|
132 |
for (ChartAxis ax : axis) { |
159 |
for (Serie s : series) { |
133 |
scatterChart.addNewAxId().setVal(ax.getId()); |
160 |
s.addToChart(scatterChart); |
134 |
} |
|
|
135 |
} |
161 |
} |
136 |
|
162 |
|
137 |
public List<? extends Serie> getSeries() { |
163 |
for (ChartAxis ax : axis) { |
138 |
return series; |
164 |
scatterChart.addNewAxId().setVal(ax.getId()); |
139 |
} |
165 |
} |
|
|
166 |
} |
140 |
|
167 |
|
141 |
private void addStyle(CTScatterChart ctScatterChart) { |
168 |
public List<? extends Serie> getSeries() { |
142 |
CTScatterStyle scatterStyle = ctScatterChart.addNewScatterStyle(); |
169 |
return series; |
143 |
scatterStyle.setVal(STScatterStyle.LINE_MARKER); |
170 |
} |
144 |
} |
171 |
|
|
|
172 |
private void addStyle(CTScatterChart ctScatterChart) { |
173 |
CTScatterStyle scatterStyle = ctScatterChart.addNewScatterStyle(); |
174 |
scatterStyle.setVal(STScatterStyle.LINE_MARKER); |
175 |
} |
145 |
} |
176 |
} |