1 | /*******************************************************************************
|
---|
2 | * You may amend and distribute as you like, but don't remove this header!
|
---|
3 | *
|
---|
4 | * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
|
---|
5 | * See http://www.codeplex.com/EPPlus for details.
|
---|
6 | *
|
---|
7 | * Copyright (C) 2011 Jan Källman
|
---|
8 | *
|
---|
9 | * This library is free software; you can redistribute it and/or
|
---|
10 | * modify it under the terms of the GNU Lesser General Public
|
---|
11 | * License as published by the Free Software Foundation; either
|
---|
12 | * version 2.1 of the License, or (at your option) any later version.
|
---|
13 |
|
---|
14 | * This library is distributed in the hope that it will be useful,
|
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
---|
17 | * See the GNU Lesser General Public License for more details.
|
---|
18 | *
|
---|
19 | * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
|
---|
20 | * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
|
---|
21 | *
|
---|
22 | * All code and executables are provided "as is" with no warranty either express or implied.
|
---|
23 | * The author accepts no liability for any damage or loss of business that this product may cause.
|
---|
24 | *
|
---|
25 | * Code change notes:
|
---|
26 | *
|
---|
27 | * Author Change Date
|
---|
28 | *******************************************************************************
|
---|
29 | * Jan Källman Added 2009-10-01
|
---|
30 | * Jan Källman License changed GPL-->LGPL 2011-12-16
|
---|
31 | *******************************************************************************/
|
---|
32 | using System;
|
---|
33 | using System.Collections.Generic;
|
---|
34 | using System.Text;
|
---|
35 | using System.Xml;
|
---|
36 | using System.IO.Packaging;
|
---|
37 | using System.Collections;
|
---|
38 | using OfficeOpenXml.Table.PivotTable;
|
---|
39 | namespace OfficeOpenXml.Drawing.Chart
|
---|
40 | {
|
---|
41 | /// <summary>
|
---|
42 | /// Collection class for chart series
|
---|
43 | /// </summary>
|
---|
44 | public sealed class ExcelChartSeries : XmlHelper, IEnumerable
|
---|
45 | {
|
---|
46 | List<ExcelChartSerie> _list=new List<ExcelChartSerie>();
|
---|
47 | internal ExcelChart _chart;
|
---|
48 | XmlNode _node;
|
---|
49 | XmlNamespaceManager _ns;
|
---|
50 | internal ExcelChartSeries(ExcelChart chart, XmlNamespaceManager ns, XmlNode node, bool isPivot)
|
---|
51 | : base(ns,node)
|
---|
52 | {
|
---|
53 | _ns = ns;
|
---|
54 | _chart=chart;
|
---|
55 | _node=node;
|
---|
56 | _isPivot = isPivot;
|
---|
57 | SchemaNodeOrder = new string[] { "view3D", "plotArea", "barDir", "grouping", "scatterStyle", "varyColors", "ser", "explosion", "dLbls", "firstSliceAng", "holeSize", "shape", "legend", "axId" };
|
---|
58 | foreach(XmlNode n in node.SelectNodes("c:ser",ns))
|
---|
59 | {
|
---|
60 | ExcelChartSerie s;
|
---|
61 | if (chart.ChartNode.LocalName == "scatterChart")
|
---|
62 | {
|
---|
63 | s = new ExcelScatterChartSerie(this, ns, n, isPivot);
|
---|
64 | }
|
---|
65 | else if (chart.ChartNode.LocalName == "lineChart")
|
---|
66 | {
|
---|
67 | s = new ExcelLineChartSerie(this, ns, n, isPivot);
|
---|
68 | }
|
---|
69 | else if (chart.ChartNode.LocalName == "pieChart" ||
|
---|
70 | chart.ChartNode.LocalName == "ofPieChart" ||
|
---|
71 | chart.ChartNode.LocalName == "pie3DChart" ||
|
---|
72 | chart.ChartNode.LocalName == "doughnutChart")
|
---|
73 | {
|
---|
74 | s = new ExcelPieChartSerie(this, ns, n, isPivot);
|
---|
75 | }
|
---|
76 | else
|
---|
77 | {
|
---|
78 | s = new ExcelChartSerie(this, ns, n, isPivot);
|
---|
79 | }
|
---|
80 | _list.Add(s);
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | #region IEnumerable Members
|
---|
85 |
|
---|
86 | public IEnumerator GetEnumerator()
|
---|
87 | {
|
---|
88 | return (_list.GetEnumerator());
|
---|
89 | }
|
---|
90 | /// <summary>
|
---|
91 | /// Returns the serie at the specified position.
|
---|
92 | /// </summary>
|
---|
93 | /// <param name="PositionID">The position of the series.</param>
|
---|
94 | /// <returns></returns>
|
---|
95 | public ExcelChartSerie this[int PositionID]
|
---|
96 | {
|
---|
97 | get
|
---|
98 | {
|
---|
99 | return (_list[PositionID]);
|
---|
100 | }
|
---|
101 | }
|
---|
102 | public int Count
|
---|
103 | {
|
---|
104 | get
|
---|
105 | {
|
---|
106 | return _list.Count;
|
---|
107 | }
|
---|
108 | }
|
---|
109 | /// <summary>
|
---|
110 | /// Delete the chart at the specific position
|
---|
111 | /// </summary>
|
---|
112 | /// <param name="PositionID">Zero based</param>
|
---|
113 | public void Delete(int PositionID)
|
---|
114 | {
|
---|
115 | ExcelChartSerie ser = _list[PositionID];
|
---|
116 | ser.TopNode.ParentNode.RemoveChild(ser.TopNode);
|
---|
117 | _list.RemoveAt(PositionID);
|
---|
118 | }
|
---|
119 | #endregion
|
---|
120 | /// <summary>
|
---|
121 | /// A reference to the chart object
|
---|
122 | /// </summary>
|
---|
123 | public ExcelChart Chart
|
---|
124 | {
|
---|
125 | get
|
---|
126 | {
|
---|
127 | return _chart;
|
---|
128 | }
|
---|
129 | }
|
---|
130 | #region "Add Series"
|
---|
131 |
|
---|
132 | /// <summary>
|
---|
133 | /// Add a new serie to the chart. Do not apply to pivotcharts.
|
---|
134 | /// </summary>
|
---|
135 | /// <param name="Serie">The Y-Axis range</param>
|
---|
136 | /// <param name="XSerie">The X-Axis range</param>
|
---|
137 | /// <returns></returns>
|
---|
138 | public ExcelChartSerie Add(ExcelRangeBase Serie, ExcelRangeBase XSerie)
|
---|
139 | {
|
---|
140 | if (_chart.PivotTableSource != null)
|
---|
141 | {
|
---|
142 | throw (new InvalidOperationException("Can't add a serie to a pivotchart"));
|
---|
143 | }
|
---|
144 | return AddSeries(Serie.FullAddressAbsolute, XSerie.FullAddressAbsolute);
|
---|
145 | }
|
---|
146 | /// <summary>
|
---|
147 | /// Add a new serie to the chart.Do not apply to pivotcharts.
|
---|
148 | /// </summary>
|
---|
149 | /// <param name="SerieAddress">The Y-Axis range</param>
|
---|
150 | /// <param name="XSerieAddress">The X-Axis range</param>
|
---|
151 | /// <returns></returns>
|
---|
152 | public ExcelChartSerie Add(string SerieAddress, string XSerieAddress)
|
---|
153 | {
|
---|
154 | if (_chart.PivotTableSource != null)
|
---|
155 | {
|
---|
156 | throw (new InvalidOperationException("Can't add a serie to a pivotchart"));
|
---|
157 | }
|
---|
158 | return AddSeries(SerieAddress, XSerieAddress);
|
---|
159 | }
|
---|
160 | private ExcelChartSerie AddSeries(string SeriesAddress, string XSeriesAddress)
|
---|
161 | {
|
---|
162 | XmlElement ser = _node.OwnerDocument.CreateElement("ser", ExcelPackage.schemaChart);
|
---|
163 | XmlNodeList node = _node.SelectNodes("c:ser", _ns);
|
---|
164 | if (node.Count > 0)
|
---|
165 | {
|
---|
166 | _node.InsertAfter(ser, node[node.Count-1]);
|
---|
167 | }
|
---|
168 | else
|
---|
169 | {
|
---|
170 | InserAfter(_node, "c:varyColors,c:grouping,c:barDir,c:scatterStyle", ser);
|
---|
171 | }
|
---|
172 | int idx = FindIndex();
|
---|
173 | ser.InnerXml = string.Format("<c:idx val=\"{1}\" /><c:order val=\"{1}\" /><c:tx><c:strRef><c:f></c:f><c:strCache><c:ptCount val=\"1\" /></c:strCache></c:strRef></c:tx>{5}{0}{2}{3}{4}", AddExplosion(Chart.ChartType), idx, AddScatterPoint(Chart.ChartType), AddAxisNodes(Chart.ChartType), AddSmooth(Chart.ChartType), AddMarker(Chart.ChartType));
|
---|
174 | ExcelChartSerie serie;
|
---|
175 | switch (Chart.ChartType)
|
---|
176 | {
|
---|
177 | case eChartType.XYScatter:
|
---|
178 | case eChartType.XYScatterLines:
|
---|
179 | case eChartType.XYScatterLinesNoMarkers:
|
---|
180 | case eChartType.XYScatterSmooth:
|
---|
181 | case eChartType.XYScatterSmoothNoMarkers:
|
---|
182 | serie = new ExcelScatterChartSerie(this, NameSpaceManager, ser, _isPivot);
|
---|
183 | break;
|
---|
184 | case eChartType.Pie:
|
---|
185 | case eChartType.Pie3D:
|
---|
186 | case eChartType.PieExploded:
|
---|
187 | case eChartType.PieExploded3D:
|
---|
188 | case eChartType.PieOfPie:
|
---|
189 | case eChartType.Doughnut:
|
---|
190 | case eChartType.DoughnutExploded:
|
---|
191 | case eChartType.BarOfPie:
|
---|
192 | serie = new ExcelPieChartSerie(this, NameSpaceManager, ser, _isPivot);
|
---|
193 | break;
|
---|
194 | case eChartType.Line:
|
---|
195 | case eChartType.LineMarkers:
|
---|
196 | case eChartType.LineMarkersStacked:
|
---|
197 | case eChartType.LineMarkersStacked100:
|
---|
198 | case eChartType.LineStacked:
|
---|
199 | case eChartType.LineStacked100:
|
---|
200 | serie = new ExcelLineChartSerie(this, NameSpaceManager, ser, _isPivot);
|
---|
201 | if (Chart.ChartType == eChartType.LineMarkers ||
|
---|
202 | Chart.ChartType == eChartType.LineMarkersStacked ||
|
---|
203 | Chart.ChartType == eChartType.LineMarkersStacked100)
|
---|
204 | {
|
---|
205 | ((ExcelLineChartSerie)serie).Marker = eMarkerStyle.Square;
|
---|
206 | }
|
---|
207 | ((ExcelLineChartSerie)serie).Smooth = ((ExcelLineChart)Chart).Smooth;
|
---|
208 | break;
|
---|
209 |
|
---|
210 | default:
|
---|
211 | serie = new ExcelChartSerie(this, NameSpaceManager, ser, _isPivot);
|
---|
212 | break;
|
---|
213 | }
|
---|
214 | serie.Series = SeriesAddress;
|
---|
215 | serie.XSeries = XSeriesAddress;
|
---|
216 | _list.Add(serie);
|
---|
217 | return serie;
|
---|
218 | }
|
---|
219 | bool _isPivot;
|
---|
220 | internal void AddPivotSerie(ExcelPivotTable pivotTableSource)
|
---|
221 | {
|
---|
222 | var r=pivotTableSource.WorkSheet.Cells[pivotTableSource.Address.Address];
|
---|
223 | _isPivot = true;
|
---|
224 | AddSeries(r.Offset(0, 1, r._toRow - r._fromRow + 1, 1).FullAddressAbsolute, r.Offset(0, 0, r._toRow - r._fromRow + 1, 1).FullAddressAbsolute);
|
---|
225 | }
|
---|
226 | private int FindIndex()
|
---|
227 | {
|
---|
228 | int ret = 0, newID=0;
|
---|
229 | if (_chart.PlotArea.ChartTypes.Count > 1)
|
---|
230 | {
|
---|
231 | foreach (var chart in _chart.PlotArea.ChartTypes)
|
---|
232 | {
|
---|
233 | if (newID>0)
|
---|
234 | {
|
---|
235 | foreach (ExcelChartSerie serie in chart.Series)
|
---|
236 | {
|
---|
237 | serie.SetID((++newID).ToString());
|
---|
238 | }
|
---|
239 | }
|
---|
240 | else
|
---|
241 | {
|
---|
242 | if (chart == _chart)
|
---|
243 | {
|
---|
244 | ret += _list.Count + 1;
|
---|
245 | newID=ret;
|
---|
246 | }
|
---|
247 | else
|
---|
248 | {
|
---|
249 | ret += chart.Series.Count;
|
---|
250 | }
|
---|
251 | }
|
---|
252 | }
|
---|
253 | return ret-1;
|
---|
254 | }
|
---|
255 | else
|
---|
256 | {
|
---|
257 | return _list.Count;
|
---|
258 | }
|
---|
259 | }
|
---|
260 | #endregion
|
---|
261 | #region "Xml init Functions"
|
---|
262 | private string AddMarker(eChartType chartType)
|
---|
263 | {
|
---|
264 | if (chartType == eChartType.Line ||
|
---|
265 | chartType == eChartType.LineStacked ||
|
---|
266 | chartType == eChartType.LineStacked100 ||
|
---|
267 | chartType == eChartType.XYScatterLines ||
|
---|
268 | chartType == eChartType.XYScatterSmooth ||
|
---|
269 | chartType == eChartType.XYScatterLinesNoMarkers ||
|
---|
270 | chartType == eChartType.XYScatterSmoothNoMarkers)
|
---|
271 | {
|
---|
272 | return "<c:marker><c:symbol val=\"none\" /></c:marker>";
|
---|
273 | }
|
---|
274 | else
|
---|
275 | {
|
---|
276 | return "";
|
---|
277 | }
|
---|
278 | }
|
---|
279 | private string AddScatterPoint(eChartType chartType)
|
---|
280 | {
|
---|
281 | if (chartType == eChartType.XYScatter)
|
---|
282 | {
|
---|
283 | return "<c:spPr><a:ln w=\"28575\"><a:noFill /></a:ln></c:spPr>";
|
---|
284 | }
|
---|
285 | else
|
---|
286 | {
|
---|
287 | return "";
|
---|
288 | }
|
---|
289 | }
|
---|
290 | private string AddAxisNodes(eChartType chartType)
|
---|
291 | {
|
---|
292 | if ( chartType == eChartType.XYScatter ||
|
---|
293 | chartType == eChartType.XYScatterLines ||
|
---|
294 | chartType == eChartType.XYScatterLinesNoMarkers ||
|
---|
295 | chartType == eChartType.XYScatterSmooth ||
|
---|
296 | chartType == eChartType.XYScatterSmoothNoMarkers)
|
---|
297 | {
|
---|
298 | return "<c:xVal /><c:yVal />";
|
---|
299 | }
|
---|
300 | else
|
---|
301 | {
|
---|
302 | return "<c:val />";
|
---|
303 | }
|
---|
304 | }
|
---|
305 |
|
---|
306 | private string AddExplosion(eChartType chartType)
|
---|
307 | {
|
---|
308 | if (chartType == eChartType.PieExploded3D ||
|
---|
309 | chartType == eChartType.PieExploded ||
|
---|
310 | chartType == eChartType.DoughnutExploded)
|
---|
311 | {
|
---|
312 | return "<c:explosion val=\"25\" />"; //Default 25;
|
---|
313 | }
|
---|
314 | else
|
---|
315 | {
|
---|
316 | return "";
|
---|
317 | }
|
---|
318 | }
|
---|
319 | private string AddSmooth(eChartType chartType)
|
---|
320 | {
|
---|
321 | if (chartType == eChartType.XYScatterSmooth ||
|
---|
322 | chartType == eChartType.XYScatterSmoothNoMarkers)
|
---|
323 | {
|
---|
324 | return "<c:smooth val=\"1\" />"; //Default 25;
|
---|
325 | }
|
---|
326 | else
|
---|
327 | {
|
---|
328 | return "";
|
---|
329 | }
|
---|
330 | }
|
---|
331 | #endregion
|
---|
332 | }
|
---|
333 | }
|
---|