Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/Drawing/Chart/ExcelChartSeries.cs @ 12074

Last change on this file since 12074 was 12074, checked in by sraggl, 10 years ago

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 16.9 KB
Line 
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 *******************************************************************************/
32using System;
33using System.Collections.Generic;
34using System.Text;
35using System.Xml;
36using System.Collections;
37using OfficeOpenXml.Table.PivotTable;
38namespace OfficeOpenXml.Drawing.Chart
39{
40    public sealed class ExcelBubbleChartSeries : ExcelChartSeries
41    {
42        internal ExcelBubbleChartSeries(ExcelChart chart, XmlNamespaceManager ns, XmlNode node, bool isPivot)
43           : base(chart,ns,node, isPivot)
44        {
45            //_chartSeries = new ExcelChartSeries(this, _drawings.NameSpaceManager, _chartNode, isPivot);
46        }
47        public ExcelChartSerie Add(ExcelRangeBase Serie, ExcelRangeBase XSerie, ExcelRangeBase BubbleSize)
48        {
49            return base.AddSeries(Serie.FullAddressAbsolute, XSerie.FullAddressAbsolute, BubbleSize.FullAddressAbsolute);
50        }
51        public ExcelChartSerie Add(string SerieAddress, string XSerieAddress, string BubbleSizeAddress)
52        {
53            return base.AddSeries(SerieAddress, XSerieAddress, BubbleSizeAddress);
54        }
55    }
56    /// <summary>
57   /// Collection class for chart series
58   /// </summary>
59    public class ExcelChartSeries : XmlHelper, IEnumerable
60    {
61       List<ExcelChartSerie> _list=new List<ExcelChartSerie>();
62       internal ExcelChart _chart;
63       XmlNode _node;
64       XmlNamespaceManager _ns;
65       internal ExcelChartSeries(ExcelChart chart, XmlNamespaceManager ns, XmlNode node, bool isPivot)
66           : base(ns,node)
67       {
68           _ns = ns;
69           _chart=chart;
70           _node=node;
71           _isPivot = isPivot;
72           SchemaNodeOrder = new string[] { "view3D", "plotArea", "barDir", "grouping", "scatterStyle", "varyColors", "ser", "explosion", "dLbls", "firstSliceAng", "holeSize", "shape", "legend", "axId" };
73           foreach(XmlNode n in node.SelectNodes("c:ser",ns))
74           {
75               ExcelChartSerie s;
76               if (chart.ChartNode.LocalName == "scatterChart")
77               {
78                   s = new ExcelScatterChartSerie(this, ns, n, isPivot);
79               }
80               else if (chart.ChartNode.LocalName == "lineChart")
81               {
82                   s = new ExcelLineChartSerie(this, ns, n, isPivot);
83               }
84               else if (chart.ChartNode.LocalName == "pieChart" ||
85                        chart.ChartNode.LocalName == "ofPieChart" ||
86                        chart.ChartNode.LocalName == "pie3DChart" ||
87                        chart.ChartNode.LocalName == "doughnutChart")                                                                       
88               {
89                   s = new ExcelPieChartSerie(this, ns, n, isPivot);
90               }
91               else
92               {
93                   s = new ExcelChartSerie(this, ns, n, isPivot);
94               }
95               _list.Add(s);
96           }
97       }
98
99       #region IEnumerable Members
100
101       public IEnumerator GetEnumerator()
102       {
103           return (_list.GetEnumerator());
104       }
105       /// <summary>
106       /// Returns the serie at the specified position. 
107       /// </summary>
108       /// <param name="PositionID">The position of the series.</param>
109       /// <returns></returns>
110       public ExcelChartSerie this[int PositionID]
111       {
112           get
113           {
114               return (_list[PositionID]);
115           }
116       }
117       public int Count
118       {
119           get
120           {
121               return _list.Count;
122           }
123       }
124       /// <summary>
125       /// Delete the chart at the specific position
126       /// </summary>
127       /// <param name="PositionID">Zero based</param>
128       public void Delete(int PositionID)
129       {
130           ExcelChartSerie ser = _list[PositionID];
131           ser.TopNode.ParentNode.RemoveChild(ser.TopNode);
132           _list.RemoveAt(PositionID);
133       }
134       #endregion
135       /// <summary>
136       /// A reference to the chart object
137       /// </summary>
138       public ExcelChart Chart
139       {
140           get
141           {
142               return _chart;
143           }
144       }
145       #region "Add Series"
146
147       /// <summary>
148       /// Add a new serie to the chart. Do not apply to pivotcharts.
149       /// </summary>
150       /// <param name="Serie">The Y-Axis range</param>
151       /// <param name="XSerie">The X-Axis range</param>
152       /// <returns></returns>
153       public virtual ExcelChartSerie Add(ExcelRangeBase Serie, ExcelRangeBase XSerie)
154       {
155           if (_chart.PivotTableSource != null)
156           {
157               throw (new InvalidOperationException("Can't add a serie to a pivotchart"));
158           }
159           return AddSeries(Serie.FullAddressAbsolute, XSerie.FullAddressAbsolute,"");
160       }
161       /// <summary>
162       /// Add a new serie to the chart.Do not apply to pivotcharts.
163       /// </summary>
164       /// <param name="SerieAddress">The Y-Axis range</param>
165       /// <param name="XSerieAddress">The X-Axis range</param>
166       /// <returns></returns>
167       public virtual ExcelChartSerie Add(string SerieAddress, string XSerieAddress)
168       {
169           if (_chart.PivotTableSource != null)
170           {
171               throw (new InvalidOperationException("Can't add a serie to a pivotchart"));
172           }
173           return AddSeries(SerieAddress, XSerieAddress, "");
174       }
175       internal protected ExcelChartSerie AddSeries(string SeriesAddress, string XSeriesAddress, string bubbleSizeAddress)
176        {
177               XmlElement ser = _node.OwnerDocument.CreateElement("ser", ExcelPackage.schemaChart);
178               XmlNodeList node = _node.SelectNodes("c:ser", _ns);
179               if (node.Count > 0)
180               {
181                   _node.InsertAfter(ser, node[node.Count-1]);
182               }
183               else
184               {
185                   InserAfter(_node, "c:varyColors,c:grouping,c:barDir,c:scatterStyle", ser);
186                }
187               int idx = FindIndex();
188               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));
189               ExcelChartSerie serie;
190               switch (Chart.ChartType)
191               {
192                   case eChartType.Bubble:
193                   case eChartType.Bubble3DEffect:
194                       serie = new ExcelBubbleChartSerie(this, NameSpaceManager, ser, _isPivot)
195                       {
196                           Bubble3D=Chart.ChartType==eChartType.Bubble3DEffect,
197                           Series = SeriesAddress,
198                           XSeries = XSeriesAddress,
199                           BubbleSize = bubbleSizeAddress                           
200                       };
201                       break;
202                   case eChartType.XYScatter:
203                   case eChartType.XYScatterLines:
204                   case eChartType.XYScatterLinesNoMarkers:
205                   case eChartType.XYScatterSmooth:
206                   case eChartType.XYScatterSmoothNoMarkers:
207                       serie = new ExcelScatterChartSerie(this, NameSpaceManager, ser, _isPivot);
208                       break;
209                   case eChartType.Radar:
210                   case eChartType.RadarFilled:
211                   case eChartType.RadarMarkers:
212                       serie = new ExcelRadarChartSerie(this, NameSpaceManager, ser, _isPivot);
213                       break;
214                   case eChartType.Surface:
215                   case eChartType.SurfaceTopView:
216                   case eChartType.SurfaceTopViewWireframe:
217                   case eChartType.SurfaceWireframe:
218                       serie = new ExcelSurfaceChartSerie(this, NameSpaceManager, ser, _isPivot);
219                       break;
220                   case eChartType.Pie:
221                   case eChartType.Pie3D:
222                   case eChartType.PieExploded:
223                   case eChartType.PieExploded3D:
224                   case eChartType.PieOfPie:
225                   case eChartType.Doughnut:
226                   case eChartType.DoughnutExploded:
227                   case eChartType.BarOfPie:
228                       serie = new ExcelPieChartSerie(this, NameSpaceManager, ser, _isPivot);
229                       break;
230                   case eChartType.Line:
231                   case eChartType.LineMarkers:
232                   case eChartType.LineMarkersStacked:
233                   case eChartType.LineMarkersStacked100:
234                   case eChartType.LineStacked:
235                   case eChartType.LineStacked100:
236                       serie = new ExcelLineChartSerie(this, NameSpaceManager, ser, _isPivot);
237                       if (Chart.ChartType == eChartType.LineMarkers ||
238                           Chart.ChartType == eChartType.LineMarkersStacked ||
239                           Chart.ChartType == eChartType.LineMarkersStacked100)
240                       {
241                           ((ExcelLineChartSerie)serie).Marker = eMarkerStyle.Square;
242                       }
243                       ((ExcelLineChartSerie)serie).Smooth = ((ExcelLineChart)Chart).Smooth;
244                       break;
245                   case eChartType.BarClustered:
246                   case eChartType.BarStacked:
247                   case eChartType.BarStacked100:
248                   case eChartType.ColumnClustered:
249                   case eChartType.ColumnStacked:
250                   case eChartType.ColumnStacked100:
251                   case eChartType.BarClustered3D:
252                   case eChartType.BarStacked3D:
253                   case eChartType.BarStacked1003D:
254                   case eChartType.ColumnClustered3D:
255                   case eChartType.ColumnStacked3D:
256                   case eChartType.ColumnStacked1003D:
257                   case eChartType.ConeBarClustered:
258                   case eChartType.ConeBarStacked:
259                   case eChartType.ConeBarStacked100:
260                   case eChartType.ConeCol:
261                   case eChartType.ConeColClustered:
262                   case eChartType.ConeColStacked:
263                   case eChartType.ConeColStacked100:
264                   case eChartType.CylinderBarClustered:
265                   case eChartType.CylinderBarStacked:
266                   case eChartType.CylinderBarStacked100:
267                   case eChartType.CylinderCol:
268                   case eChartType.CylinderColClustered:
269                   case eChartType.CylinderColStacked:
270                   case eChartType.CylinderColStacked100:
271                   case eChartType.PyramidBarClustered:
272                   case eChartType.PyramidBarStacked:
273                   case eChartType.PyramidBarStacked100:
274                   case eChartType.PyramidCol:
275                   case eChartType.PyramidColClustered:
276                   case eChartType.PyramidColStacked:
277                   case eChartType.PyramidColStacked100:
278                       serie = new ExcelBarChartSerie(this, NameSpaceManager, ser, _isPivot);
279                       ((ExcelBarChartSerie)serie).InvertIfNegative=false;
280                       break;
281                   default:
282                       serie = new ExcelChartSerie(this, NameSpaceManager, ser, _isPivot);
283                       break;
284               }               
285               serie.Series = SeriesAddress;
286               serie.XSeries = XSeriesAddress;                     
287           _list.Add(serie);
288               return serie;
289        }
290       bool _isPivot;
291       internal void AddPivotSerie(ExcelPivotTable pivotTableSource)
292       {
293           var r=pivotTableSource.WorkSheet.Cells[pivotTableSource.Address.Address];
294           _isPivot = true;
295           AddSeries(r.Offset(0, 1, r._toRow - r._fromRow + 1, 1).FullAddressAbsolute, r.Offset(0, 0, r._toRow - r._fromRow + 1, 1).FullAddressAbsolute,"");
296       }
297       private int FindIndex()
298       {   
299           int ret = 0, newID=0;
300           if (_chart.PlotArea.ChartTypes.Count > 1)
301           {
302               foreach (var chart in _chart.PlotArea.ChartTypes)
303               {
304                   if (newID>0)
305                   {
306                       foreach (ExcelChartSerie serie in chart.Series)
307                       {
308                           serie.SetID((++newID).ToString());
309                       }
310                   }
311                   else
312                   {
313                       if (chart == _chart)
314                       {
315                           ret += _list.Count + 1;
316                           newID=ret;
317                       }
318                       else
319                       {
320                           ret += chart.Series.Count;
321                       }
322                   }
323               }
324               return ret-1;
325           }
326           else
327           {
328               return _list.Count;
329           }
330       }
331       #endregion
332       #region "Xml init Functions"
333       private string AddMarker(eChartType chartType)
334       {
335           if (chartType == eChartType.Line ||
336               chartType == eChartType.LineStacked ||
337               chartType == eChartType.LineStacked100 ||
338               chartType == eChartType.XYScatterLines ||
339               chartType == eChartType.XYScatterSmooth ||
340               chartType == eChartType.XYScatterLinesNoMarkers ||
341               chartType == eChartType.XYScatterSmoothNoMarkers)
342           {
343               return "<c:marker><c:symbol val=\"none\" /></c:marker>";
344           }
345           else
346           {
347               return "";
348           }
349       }
350       private string AddScatterPoint(eChartType chartType)
351       {
352           if (chartType == eChartType.XYScatter)
353           {
354               return "<c:spPr><a:ln w=\"28575\"><a:noFill /></a:ln></c:spPr>";
355           }
356           else
357           {
358               return "";
359           }
360       }
361       private string AddAxisNodes(eChartType chartType)
362       {
363           if ( chartType == eChartType.XYScatter ||
364                chartType == eChartType.XYScatterLines ||
365                chartType == eChartType.XYScatterLinesNoMarkers ||
366                chartType == eChartType.XYScatterSmooth ||
367                chartType == eChartType.XYScatterSmoothNoMarkers ||
368                chartType == eChartType.Bubble ||
369                chartType == eChartType.Bubble3DEffect)
370           {
371               return "<c:xVal /><c:yVal />";
372           }
373           else
374           {
375               return "<c:val />";
376           }
377       }
378
379       private string AddExplosion(eChartType chartType)
380       {
381           if (chartType == eChartType.PieExploded3D ||
382              chartType == eChartType.PieExploded ||
383               chartType == eChartType.DoughnutExploded)
384           {
385               return "<c:explosion val=\"25\" />"; //Default 25;
386           }
387           else
388           {
389               return "";
390           }
391       }
392       private string AddSmooth(eChartType chartType)
393       {
394           if (chartType == eChartType.XYScatterSmooth ||
395              chartType == eChartType.XYScatterSmoothNoMarkers)
396           {
397               return "<c:smooth val=\"1\" />"; //Default 25;
398           }
399           else
400           {
401               return "";
402           }
403       }
404        #endregion
405    }
406}
Note: See TracBrowser for help on using the repository browser.