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 Initial Release 2011-05-25
|
---|
30 | * Jan Källman License changed GPL-->LGPL 2011-12-16
|
---|
31 | *******************************************************************************/
|
---|
32 | using System;
|
---|
33 | using System.Collections.Generic;
|
---|
34 | using System.Linq;
|
---|
35 | using System.Text;
|
---|
36 | using System.Xml;
|
---|
37 | using System.Globalization;
|
---|
38 |
|
---|
39 | namespace OfficeOpenXml.Drawing.Chart
|
---|
40 | {
|
---|
41 | /// <summary>
|
---|
42 | /// A collection of trendlines.
|
---|
43 | /// </summary>
|
---|
44 | public class ExcelChartTrendlineCollection : IEnumerable<ExcelChartTrendline>
|
---|
45 | {
|
---|
46 | List<ExcelChartTrendline> _list = new List<ExcelChartTrendline>();
|
---|
47 | ExcelChartSerie _serie;
|
---|
48 | internal ExcelChartTrendlineCollection(ExcelChartSerie serie)
|
---|
49 | {
|
---|
50 | _serie = serie;
|
---|
51 | foreach (XmlNode node in _serie.TopNode.SelectNodes("c:trendline", _serie.NameSpaceManager))
|
---|
52 | {
|
---|
53 | _list.Add(new ExcelChartTrendline(_serie.NameSpaceManager, node));
|
---|
54 | }
|
---|
55 | }
|
---|
56 | /// <summary>
|
---|
57 | /// Add a new trendline
|
---|
58 | /// </summary>
|
---|
59 | /// <param name="Type"></param>
|
---|
60 | /// <returns>The trendline</returns>
|
---|
61 | public ExcelChartTrendline Add(eTrendLine Type)
|
---|
62 | {
|
---|
63 | if (_serie._chartSeries._chart.IsType3D() ||
|
---|
64 | _serie._chartSeries._chart.IsTypePercentStacked() ||
|
---|
65 | _serie._chartSeries._chart.IsTypeStacked() ||
|
---|
66 | _serie._chartSeries._chart.IsTypePieDoughnut())
|
---|
67 | {
|
---|
68 | throw(new ArgumentException("Trendlines don't apply to 3d-charts, stacked charts, pie charts or doughnut charts"));
|
---|
69 | }
|
---|
70 | ExcelChartTrendline tl;
|
---|
71 | XmlNode insertAfter;
|
---|
72 | if (_list.Count > 0)
|
---|
73 | {
|
---|
74 | insertAfter = _list[_list.Count - 1].TopNode;
|
---|
75 | }
|
---|
76 | else
|
---|
77 | {
|
---|
78 | insertAfter = _serie.TopNode.SelectSingleNode("c:marker", _serie.NameSpaceManager);
|
---|
79 | if (insertAfter == null)
|
---|
80 | {
|
---|
81 | insertAfter = _serie.TopNode.SelectSingleNode("c:tx", _serie.NameSpaceManager);
|
---|
82 | if (insertAfter == null)
|
---|
83 | {
|
---|
84 | insertAfter = _serie.TopNode.SelectSingleNode("c:order", _serie.NameSpaceManager);
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 | var node=_serie.TopNode.OwnerDocument.CreateElement("c","trendline", ExcelPackage.schemaChart);
|
---|
89 | _serie.TopNode.InsertAfter(node, insertAfter);
|
---|
90 |
|
---|
91 | tl = new ExcelChartTrendline(_serie.NameSpaceManager, node);
|
---|
92 | tl.Type = Type;
|
---|
93 | return tl;
|
---|
94 | }
|
---|
95 | IEnumerator<ExcelChartTrendline> IEnumerable<ExcelChartTrendline>.GetEnumerator()
|
---|
96 | {
|
---|
97 | return _list.GetEnumerator();
|
---|
98 | }
|
---|
99 |
|
---|
100 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
|
---|
101 | {
|
---|
102 | return _list.GetEnumerator();
|
---|
103 | }
|
---|
104 | }
|
---|
105 | /// <summary>
|
---|
106 | /// A trendline object
|
---|
107 | /// </summary>
|
---|
108 | public class ExcelChartTrendline : XmlHelper
|
---|
109 | {
|
---|
110 | internal ExcelChartTrendline(XmlNamespaceManager namespaceManager, XmlNode topNode) :
|
---|
111 | base(namespaceManager,topNode)
|
---|
112 |
|
---|
113 | {
|
---|
114 | SchemaNodeOrder = new string[] { "name", "trendlineType","order","period", "forward","backward","intercept", "dispRSqr", "dispEq", "trendlineLbl" };
|
---|
115 | }
|
---|
116 | const string TRENDLINEPATH = "c:trendlineType/@val";
|
---|
117 | /// <summary>
|
---|
118 | /// Type of Trendline
|
---|
119 | /// </summary>
|
---|
120 | public eTrendLine Type
|
---|
121 | {
|
---|
122 | get
|
---|
123 | {
|
---|
124 | switch (GetXmlNodeString(TRENDLINEPATH).ToLower())
|
---|
125 | {
|
---|
126 | case "exp":
|
---|
127 | return eTrendLine.Exponential;
|
---|
128 | case "log":
|
---|
129 | return eTrendLine.Logarithmic;
|
---|
130 | case "poly":
|
---|
131 | return eTrendLine.Polynomial;
|
---|
132 | case "movingavg":
|
---|
133 | return eTrendLine.MovingAvgerage;
|
---|
134 | case "power":
|
---|
135 | return eTrendLine.Power;
|
---|
136 | default:
|
---|
137 | return eTrendLine.Linear;
|
---|
138 | }
|
---|
139 | }
|
---|
140 | set
|
---|
141 | {
|
---|
142 | switch (value)
|
---|
143 | {
|
---|
144 | case eTrendLine.Exponential:
|
---|
145 | SetXmlNodeString(TRENDLINEPATH, "exp");
|
---|
146 | break;
|
---|
147 | case eTrendLine.Logarithmic:
|
---|
148 | SetXmlNodeString(TRENDLINEPATH, "log");
|
---|
149 | break;
|
---|
150 | case eTrendLine.Polynomial:
|
---|
151 | SetXmlNodeString(TRENDLINEPATH, "poly");
|
---|
152 | Order = 2;
|
---|
153 | break;
|
---|
154 | case eTrendLine.MovingAvgerage:
|
---|
155 | SetXmlNodeString(TRENDLINEPATH, "movingAvg");
|
---|
156 | Period = 2;
|
---|
157 | break;
|
---|
158 | case eTrendLine.Power:
|
---|
159 | SetXmlNodeString(TRENDLINEPATH, "power");
|
---|
160 | break;
|
---|
161 | default:
|
---|
162 | SetXmlNodeString(TRENDLINEPATH, "linear");
|
---|
163 | break;
|
---|
164 | }
|
---|
165 | }
|
---|
166 | }
|
---|
167 | const string NAMEPATH = "c:name";
|
---|
168 | /// <summary>
|
---|
169 | /// Name in the legend
|
---|
170 | /// </summary>
|
---|
171 | public string Name
|
---|
172 | {
|
---|
173 | get
|
---|
174 | {
|
---|
175 | return GetXmlNodeString(NAMEPATH);
|
---|
176 | }
|
---|
177 | set
|
---|
178 | {
|
---|
179 | SetXmlNodeString(NAMEPATH, value, true);
|
---|
180 | }
|
---|
181 | }
|
---|
182 | const string ORDERPATH = "c:order/@val";
|
---|
183 | /// <summary>
|
---|
184 | /// Order for polynominal trendlines
|
---|
185 | /// </summary>
|
---|
186 | public decimal Order
|
---|
187 | {
|
---|
188 | get
|
---|
189 | {
|
---|
190 | return GetXmlNodeDecimal(ORDERPATH);
|
---|
191 | }
|
---|
192 | set
|
---|
193 | {
|
---|
194 | if (Type == eTrendLine.MovingAvgerage)
|
---|
195 | {
|
---|
196 | throw (new ArgumentException("Can't set period for trendline type MovingAvgerage"));
|
---|
197 | }
|
---|
198 | DeleteAllNode(PERIODPATH);
|
---|
199 | SetXmlNodeString(ORDERPATH, value.ToString(CultureInfo.InvariantCulture));
|
---|
200 | }
|
---|
201 | }
|
---|
202 | const string PERIODPATH = "c:period/@val";
|
---|
203 | /// <summary>
|
---|
204 | /// Period for monthly average trendlines
|
---|
205 | /// </summary>
|
---|
206 | public decimal Period
|
---|
207 | {
|
---|
208 | get
|
---|
209 | {
|
---|
210 | return GetXmlNodeDecimal(PERIODPATH);
|
---|
211 | }
|
---|
212 | set
|
---|
213 | {
|
---|
214 | if (Type == eTrendLine.Polynomial)
|
---|
215 | {
|
---|
216 | throw (new ArgumentException("Can't set period for trendline type Polynomial"));
|
---|
217 | }
|
---|
218 | DeleteAllNode(ORDERPATH);
|
---|
219 | SetXmlNodeString(PERIODPATH, value.ToString(CultureInfo.InvariantCulture));
|
---|
220 | }
|
---|
221 | }
|
---|
222 | const string FORWARDPATH = "c:forward/@val";
|
---|
223 | /// <summary>
|
---|
224 | /// Forcast forward periods
|
---|
225 | /// </summary>
|
---|
226 | public decimal Forward
|
---|
227 | {
|
---|
228 | get
|
---|
229 | {
|
---|
230 | return GetXmlNodeDecimal(FORWARDPATH);
|
---|
231 | }
|
---|
232 | set
|
---|
233 | {
|
---|
234 | SetXmlNodeString(FORWARDPATH, value.ToString(CultureInfo.InvariantCulture));
|
---|
235 | }
|
---|
236 | }
|
---|
237 | const string BACKWARDPATH = "c:backward/@val";
|
---|
238 | /// <summary>
|
---|
239 | /// Forcast backwards periods
|
---|
240 | /// </summary>
|
---|
241 | public decimal Backward
|
---|
242 | {
|
---|
243 | get
|
---|
244 | {
|
---|
245 | return GetXmlNodeDecimal(BACKWARDPATH);
|
---|
246 | }
|
---|
247 | set
|
---|
248 | {
|
---|
249 | SetXmlNodeString(BACKWARDPATH, value.ToString(CultureInfo.InvariantCulture));
|
---|
250 | }
|
---|
251 | }
|
---|
252 | const string INTERCEPTPATH = "c:intercept/@val";
|
---|
253 | /// <summary>
|
---|
254 | /// Specify the point where the trendline crosses the vertical axis
|
---|
255 | /// </summary>
|
---|
256 | public decimal Intercept
|
---|
257 | {
|
---|
258 | get
|
---|
259 | {
|
---|
260 | return GetXmlNodeDecimal(INTERCEPTPATH);
|
---|
261 | }
|
---|
262 | set
|
---|
263 | {
|
---|
264 | SetXmlNodeString(INTERCEPTPATH, value.ToString(CultureInfo.InvariantCulture));
|
---|
265 | }
|
---|
266 | }
|
---|
267 | const string DISPLAYRSQUAREDVALUEPATH = "c:dispRSqr/@val";
|
---|
268 | /// <summary>
|
---|
269 | /// Display the R-squared value for a trendline
|
---|
270 | /// </summary>
|
---|
271 | public bool DisplayRSquaredValue
|
---|
272 | {
|
---|
273 | get
|
---|
274 | {
|
---|
275 | return GetXmlNodeBool(DISPLAYRSQUAREDVALUEPATH, false);
|
---|
276 | }
|
---|
277 | set
|
---|
278 | {
|
---|
279 | SetXmlNodeBool(DISPLAYRSQUAREDVALUEPATH,value, false);
|
---|
280 | }
|
---|
281 | }
|
---|
282 | const string DISPLAYEQUATIONPATH = "c:dispEq/@val";
|
---|
283 | /// <summary>
|
---|
284 | /// Display the trendline equation on the chart
|
---|
285 | /// </summary>
|
---|
286 | public bool DisplayEquation
|
---|
287 | {
|
---|
288 | get
|
---|
289 | {
|
---|
290 | return GetXmlNodeBool(DISPLAYEQUATIONPATH, false);
|
---|
291 | }
|
---|
292 | set
|
---|
293 | {
|
---|
294 | SetXmlNodeBool(DISPLAYEQUATIONPATH, value, false);
|
---|
295 | }
|
---|
296 | }
|
---|
297 | }
|
---|
298 | }
|
---|