Lines 17-53
Link Here
|
17 |
|
17 |
|
18 |
package org.apache.poi.xssf.usermodel.charts; |
18 |
package org.apache.poi.xssf.usermodel.charts; |
19 |
|
19 |
|
20 |
import java.util.List; |
|
|
21 |
import java.util.ArrayList; |
22 |
|
23 |
import org.apache.poi.ss.usermodel.Chart; |
20 |
import org.apache.poi.ss.usermodel.Chart; |
24 |
import org.apache.poi.ss.usermodel.Sheet; |
21 |
import org.apache.poi.ss.usermodel.charts.ChartAxis; |
25 |
import org.apache.poi.ss.util.DataMarker; |
22 |
import org.apache.poi.ss.usermodel.charts.ChartDataSource; |
26 |
import org.apache.poi.ss.usermodel.charts.ScatterChartData; |
23 |
import org.apache.poi.ss.usermodel.charts.ScatterChartData; |
27 |
import org.apache.poi.ss.usermodel.charts.ScatterChartSerie; |
24 |
import org.apache.poi.ss.usermodel.charts.ScatterChartSerie; |
28 |
import org.apache.poi.ss.usermodel.charts.ChartDataFactory; |
25 |
import org.apache.poi.xssf.usermodel.XSSFChart; |
29 |
import org.apache.poi.ss.usermodel.charts.ChartAxis; |
26 |
import org.openxmlformats.schemas.drawingml.x2006.chart.*; |
30 |
|
27 |
|
31 |
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea; |
28 |
import java.util.ArrayList; |
32 |
import org.openxmlformats.schemas.drawingml.x2006.chart.CTScatterChart; |
29 |
import java.util.List; |
33 |
import org.openxmlformats.schemas.drawingml.x2006.chart.CTScatterStyle; |
|
|
34 |
import org.openxmlformats.schemas.drawingml.x2006.chart.CTScatterSer; |
35 |
import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource; |
36 |
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumRef; |
37 |
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource; |
38 |
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumFmt; |
39 |
import org.openxmlformats.schemas.drawingml.x2006.chart.CTValAx; |
40 |
import org.openxmlformats.schemas.drawingml.x2006.chart.STScatterStyle; |
41 |
import org.openxmlformats.schemas.drawingml.x2006.chart.STCrosses; |
42 |
import org.openxmlformats.schemas.drawingml.x2006.chart.STCrossBetween; |
43 |
import org.openxmlformats.schemas.drawingml.x2006.chart.STOrientation; |
44 |
import org.openxmlformats.schemas.drawingml.x2006.chart.STTickLblPos; |
45 |
import org.openxmlformats.schemas.drawingml.x2006.chart.STAxPos; |
46 |
|
30 |
|
47 |
import org.apache.poi.xssf.usermodel.XSSFChart; |
|
|
48 |
|
31 |
|
49 |
/** |
32 |
/** |
50 |
* Represents DrawingML scatter chart. |
33 |
* Represents DrawingML scatter charts. |
51 |
* |
34 |
* |
52 |
* @author Roman Kashitsyn |
35 |
* @author Roman Kashitsyn |
53 |
*/ |
36 |
*/ |
Lines 59-176
Link Here
|
59 |
private List<Serie> series; |
42 |
private List<Serie> series; |
60 |
|
43 |
|
61 |
public XSSFScatterChartData() { |
44 |
public XSSFScatterChartData() { |
62 |
series = new ArrayList<Serie>(); |
45 |
series = new ArrayList<Serie>(); |
63 |
} |
46 |
} |
64 |
|
47 |
|
65 |
/** |
48 |
/** |
66 |
* Package private ScatterChartSerie implementation. |
49 |
* Package private ScatterChartSerie implementation. |
67 |
*/ |
50 |
*/ |
68 |
static class Serie implements ScatterChartSerie { |
51 |
static class Serie implements ScatterChartSerie { |
69 |
private int id; |
52 |
private int id; |
70 |
private int order; |
53 |
private int order; |
71 |
private boolean useCache; |
54 |
private ChartDataSource<?> xs; |
72 |
private DataMarker xMarker; |
55 |
private ChartDataSource<? extends Number> ys; |
73 |
private DataMarker yMarker; |
|
|
74 |
private XSSFNumberCache lastCaclulatedXCache; |
75 |
private XSSFNumberCache lastCalculatedYCache; |
76 |
|
56 |
|
77 |
protected Serie(int id, int order) { |
57 |
protected Serie(int id, int order, |
|
|
58 |
ChartDataSource<?> xs, |
59 |
ChartDataSource<? extends Number> ys) { |
78 |
super(); |
60 |
super(); |
79 |
this.id = id; |
61 |
this.id = id; |
80 |
this.order = order; |
62 |
this.order = order; |
81 |
this.useCache = true; |
63 |
this.xs = xs; |
|
|
64 |
this.ys = ys; |
82 |
} |
65 |
} |
83 |
|
66 |
|
84 |
public void setXValues(DataMarker marker) { |
67 |
/** |
85 |
xMarker = marker; |
|
|
86 |
} |
87 |
|
88 |
public void setYValues(DataMarker marker) { |
89 |
yMarker = marker; |
90 |
} |
91 |
|
92 |
/** |
93 |
* @param useCache if true, cached results will be added on plot |
68 |
* Returns data source used for X axis values. |
|
|
69 |
* @return data source used for X axis values |
94 |
*/ |
70 |
*/ |
95 |
public void setUseCache(boolean useCache) { |
71 |
public ChartDataSource<?> getXValues() { |
96 |
this.useCache = useCache; |
72 |
return xs; |
97 |
} |
73 |
} |
98 |
|
74 |
|
99 |
/** |
75 |
/** |
100 |
* Returns last calculated number cache for X axis. |
76 |
* Returns data source used for Y axis values. |
101 |
* @return last calculated number cache for X axis. |
77 |
* @return data source used for Y axis values |
102 |
*/ |
78 |
*/ |
103 |
XSSFNumberCache getLastCaculatedXCache() { |
79 |
public ChartDataSource<? extends Number> getYValues() { |
104 |
return lastCaclulatedXCache; |
80 |
return ys; |
105 |
} |
81 |
} |
106 |
|
82 |
|
107 |
/** |
83 |
protected void addToChart(CTScatterChart ctScatterChart) { |
108 |
* Returns last calculated number cache for Y axis. |
84 |
CTScatterSer scatterSer = ctScatterChart.addNewSer(); |
109 |
* @return last calculated number cache for Y axis. |
85 |
scatterSer.addNewIdx().setVal(this.id); |
110 |
*/ |
86 |
scatterSer.addNewOrder().setVal(this.order); |
111 |
XSSFNumberCache getLastCalculatedYCache() { |
|
|
112 |
return lastCalculatedYCache; |
113 |
} |
114 |
|
115 |
protected void addToChart(CTScatterChart ctScatterChart) { |
116 |
CTScatterSer scatterSer = ctScatterChart.addNewSer(); |
117 |
scatterSer.addNewIdx().setVal(this.id); |
118 |
scatterSer.addNewOrder().setVal(this.order); |
119 |
|
87 |
|
120 |
/* TODO: add some logic to automatically recognize cell |
88 |
CTAxDataSource xVal = scatterSer.addNewXVal(); |
121 |
* types and choose appropriate data representation for |
|
|
122 |
* X axis. |
123 |
*/ |
124 |
CTAxDataSource xVal = scatterSer.addNewXVal(); |
125 |
CTNumRef xNumRef = xVal.addNewNumRef(); |
89 |
XSSFChartUtil.buildAxDataSource(xVal, xs); |
126 |
xNumRef.setF(xMarker.formatAsString()); |
|
|
127 |
|
90 |
|
128 |
CTNumDataSource yVal = scatterSer.addNewYVal(); |
91 |
CTNumDataSource yVal = scatterSer.addNewYVal(); |
129 |
CTNumRef yNumRef = yVal.addNewNumRef(); |
92 |
XSSFChartUtil.buildNumDataSource(yVal, ys); |
130 |
yNumRef.setF(yMarker.formatAsString()); |
|
|
131 |
|
132 |
if (useCache) { |
133 |
/* We can not store cache since markers are not immutable */ |
134 |
XSSFNumberCache.buildCache(xMarker, xNumRef); |
135 |
lastCalculatedYCache = XSSFNumberCache.buildCache(yMarker, yNumRef); |
136 |
} |
93 |
} |
137 |
} |
94 |
} |
138 |
} |
|
|
139 |
|
95 |
|
140 |
public ScatterChartSerie addSerie(DataMarker xMarker, DataMarker yMarker) { |
96 |
public ScatterChartSerie addSerie(ChartDataSource<?> xs, |
|
|
97 |
ChartDataSource<? extends Number> ys) { |
98 |
if (!ys.isNumeric()) { |
99 |
throw new IllegalArgumentException("Y axis data source must be numeric."); |
100 |
} |
141 |
int numOfSeries = series.size(); |
101 |
int numOfSeries = series.size(); |
142 |
Serie newSerie = new Serie(numOfSeries, numOfSeries); |
102 |
Serie newSerie = new Serie(numOfSeries, numOfSeries, xs, ys); |
143 |
newSerie.setXValues(xMarker); |
|
|
144 |
newSerie.setYValues(yMarker); |
145 |
series.add(newSerie); |
103 |
series.add(newSerie); |
146 |
return newSerie; |
104 |
return newSerie; |
147 |
} |
105 |
} |
148 |
|
106 |
|
149 |
public void fillChart(Chart chart, ChartAxis... axis) { |
107 |
public void fillChart(Chart chart, ChartAxis... axis) { |
150 |
if (!(chart instanceof XSSFChart)) { |
108 |
if (!(chart instanceof XSSFChart)) { |
151 |
throw new IllegalArgumentException("Chart must be instance of XSSFChart"); |
109 |
throw new IllegalArgumentException("Chart must be instance of XSSFChart"); |
152 |
} |
110 |
} |
153 |
|
111 |
|
154 |
XSSFChart xssfChart = (XSSFChart) chart; |
112 |
XSSFChart xssfChart = (XSSFChart) chart; |
155 |
CTPlotArea plotArea = xssfChart.getCTChart().getPlotArea(); |
113 |
CTPlotArea plotArea = xssfChart.getCTChart().getPlotArea(); |
156 |
CTScatterChart scatterChart = plotArea.addNewScatterChart(); |
114 |
CTScatterChart scatterChart = plotArea.addNewScatterChart(); |
157 |
addStyle(scatterChart); |
115 |
addStyle(scatterChart); |
158 |
|
116 |
|
159 |
for (Serie s : series) { |
117 |
for (Serie s : series) { |
160 |
s.addToChart(scatterChart); |
118 |
s.addToChart(scatterChart); |
161 |
} |
119 |
} |
162 |
|
120 |
|
163 |
for (ChartAxis ax : axis) { |
121 |
for (ChartAxis ax : axis) { |
164 |
scatterChart.addNewAxId().setVal(ax.getId()); |
122 |
scatterChart.addNewAxId().setVal(ax.getId()); |
165 |
} |
123 |
} |
166 |
} |
124 |
} |
167 |
|
125 |
|
168 |
public List<? extends Serie> getSeries() { |
126 |
public List<? extends Serie> getSeries() { |
169 |
return series; |
127 |
return series; |
170 |
} |
128 |
} |
171 |
|
129 |
|
172 |
private void addStyle(CTScatterChart ctScatterChart) { |
130 |
private void addStyle(CTScatterChart ctScatterChart) { |
173 |
CTScatterStyle scatterStyle = ctScatterChart.addNewScatterStyle(); |
131 |
CTScatterStyle scatterStyle = ctScatterChart.addNewScatterStyle(); |
174 |
scatterStyle.setVal(STScatterStyle.LINE_MARKER); |
132 |
scatterStyle.setVal(STScatterStyle.LINE_MARKER); |
175 |
} |
133 |
} |
176 |
} |
134 |
} |