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/ExcelChartAxis.cs @ 12074

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

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 24.1 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 OfficeOpenXml.Style;
37using System.Globalization;
38namespace OfficeOpenXml.Drawing.Chart
39{
40    /// <summary>
41    /// Position of the axis.
42    /// </summary>
43    public enum eAxisPosition
44    {
45        Left = 0,
46        Bottom = 1,
47        Right = 2,
48        Top = 3
49    }
50    /// <summary>
51    /// Position of the Y-Axis
52    /// </summary>
53    public enum eYAxisPosition
54    {
55        Left = 0,
56        Right = 2,
57    }
58    /// <summary>
59    /// Position of the X-Axis
60    /// </summary>
61    public enum eXAxisPosition
62    {
63        Bottom = 1,
64        Top = 3
65    }
66    /// <summary>
67    /// Axis orientaion
68    /// </summary>
69    public enum eAxisOrientation
70    {
71        MaxMin,
72        MinMax
73    }
74    /// <summary>
75    /// How the axis are crossed
76    /// </summary>
77    public enum eCrossBetween
78    {
79        /// <summary>
80        /// Specifies the value axis shall cross the category axis between data markers
81        /// </summary>
82        Between,
83        /// <summary>
84        /// Specifies the value axis shall cross the category axis at the midpoint of a category.
85        /// </summary>
86        MidCat
87    }
88    /// <summary>
89    /// Where the axis cross.
90    /// </summary>
91    public enum eCrosses
92    {
93        /// <summary>
94        /// (Axis Crosses at Zero) The category axis crosses at the zero point of the valueaxis (if possible), or the minimum value (if theminimum is greater than zero) or the maximum (if the maximum is less than zero).
95        /// </summary>
96        AutoZero,
97        /// <summary>
98        /// The axis crosses at the maximum value
99        /// </summary>
100        Max,
101        /// <summary>
102        /// (Axis crosses at the minimum value of the chart.
103        /// </summary>
104        Min
105    }
106    /// <summary>
107    /// Tickmarks
108    /// </summary>
109    public enum eAxisTickMark
110    {
111        /// <summary>
112        /// Specifies the tick marks shall cross the axis.
113        /// </summary>
114        Cross,   
115        /// <summary>
116        /// Specifies the tick marks shall be inside the plot area.
117        /// </summary>
118        In,     
119        /// <summary>
120        /// Specifies there shall be no tick marks.
121        /// </summary>
122        None,   
123        /// <summary>
124        /// Specifies the tick marks shall be outside the plot area.
125        /// </summary>
126        Out
127    }
128    public enum eBuildInUnits : long
129    {
130        hundreds = 100,
131        thousands = 1000,
132        tenThousands = 10000,
133        hundredThousands = 100000,
134        millions = 1000000,
135        tenMillions = 10000000,
136        hundredMillions = 100000000,
137        billions = 1000000000,
138        trillions = 1000000000000
139    }
140    /// <summary>
141    /// An axis for a chart
142    /// </summary>
143    public sealed class ExcelChartAxis : XmlHelper
144    {
145        /// <summary>
146        /// Type of axis
147        /// </summary>
148        internal enum eAxisType
149        {
150            /// <summary>
151            /// Value axis
152            /// </summary>
153            Val,
154            /// <summary>
155            /// Category axis
156            /// </summary>
157            Cat,
158            /// <summary>
159            /// Date axis
160            /// </summary>
161            Date,
162            /// <summary>
163            /// Series axis
164            /// </summary>
165            Serie
166        }
167        internal ExcelChartAxis(XmlNamespaceManager nameSpaceManager, XmlNode topNode) :
168            base(nameSpaceManager, topNode)
169        {
170            SchemaNodeOrder = new string[] { "axId", "scaling", "logBase", "orientation", "max", "min", "delete", "axPos", "majorGridlines", "title", "numFmt", "majorTickMark", "minorTickMark", "tickLblPos", "spPr", "txPr", "crossAx", "crossesAt", "crosses", "crossBetween", "auto", "lblOffset", "majorUnit", "minorUnit", "dispUnits", "spPr", "txPr" };
171        }
172        internal string Id
173        {
174            get
175            {
176                return GetXmlNodeString("c:axId/@val");
177            }
178        }
179        const string _majorTickMark = "c:majorTickMark/@val";
180        /// <summary>
181        /// majorTickMark
182        /// This element specifies the major tick marks for the axis.
183        /// </summary>
184        public eAxisTickMark MajorTickMark
185        {
186            get
187            {
188                var v=GetXmlNodeString(_majorTickMark);
189                if(string.IsNullOrEmpty(v))
190                {
191                    return eAxisTickMark.Cross;
192                }
193                else
194                {
195                    try
196                    {
197                        return (eAxisTickMark)Enum.Parse( typeof( eAxisTickMark ), v );
198                    }
199                    catch
200                    {
201                        return eAxisTickMark.Cross;
202                    }
203                }
204            }
205            set
206            {
207                SetXmlNodeString( _majorTickMark, value.ToString().ToLower(CultureInfo.InvariantCulture) );
208            }
209        }
210
211        const string _minorTickMark = "c:minorTickMark/@val";
212        /// <summary>
213        /// minorTickMark
214        /// This element specifies the minor tick marks for the axis.
215        /// </summary>
216        public eAxisTickMark MinorTickMark
217        {
218            get
219            {
220                var v=GetXmlNodeString(_minorTickMark);
221                if(string.IsNullOrEmpty(v))
222                {
223                    return eAxisTickMark.Cross;
224                }
225                else
226                {
227                    try
228                    {
229                        return (eAxisTickMark)Enum.Parse(typeof(eAxisTickMark), v );
230                    }
231                    catch
232                    {
233                        return eAxisTickMark.Cross;
234                    }
235                }
236            }
237            set
238            {
239                SetXmlNodeString(_minorTickMark, value.ToString().ToLower(CultureInfo.InvariantCulture));
240            }
241        }
242         /// <summary>
243        /// Type of axis
244        /// </summary>
245        internal eAxisType AxisType
246        {
247            get
248            {
249                try
250                {
251                    return (eAxisType)Enum.Parse(typeof(eAxisType), TopNode.LocalName.Substring(0,3), true);
252                }
253                catch
254                {
255                    return eAxisType.Val;
256                }
257            }
258        }
259        private string AXIS_POSITION_PATH = "c:axPos/@val";
260        /// <summary>
261        /// Where the axis is located
262        /// </summary>
263        public eAxisPosition AxisPosition
264        {
265            get
266            {               
267                switch(GetXmlNodeString(AXIS_POSITION_PATH))
268                {
269                    case "b":
270                        return eAxisPosition.Bottom;
271                    case "r":
272                        return eAxisPosition.Right;
273                    case "t":
274                        return eAxisPosition.Top;
275                    default:
276                        return eAxisPosition.Left;
277                }
278            }
279            internal set
280            {
281                SetXmlNodeString(AXIS_POSITION_PATH, value.ToString().ToLower(CultureInfo.InvariantCulture).Substring(0,1));
282            }
283        }
284        const string _crossesPath = "c:crosses/@val";
285        /// <summary>
286        /// Where the axis cross
287        /// </summary>
288        public eCrosses Crosses
289        {
290            get
291            {
292                var v=GetXmlNodeString(_crossesPath);
293                if (string.IsNullOrEmpty(v))
294                {
295                    return eCrosses.AutoZero;
296                }
297                else
298                {
299                    try
300                    {
301                        return (eCrosses)Enum.Parse(typeof(eCrosses), v, true);
302                    }
303                    catch
304                    {
305                        return eCrosses.AutoZero;
306                    }
307                }
308            }
309            set
310            {
311                var v = value.ToString();
312                v = v.Substring(0, 1).ToLower(CultureInfo.InvariantCulture) + v.Substring(1, v.Length - 1);
313                SetXmlNodeString(_crossesPath, v);
314            }
315
316        }
317        const string _crossBetweenPath = "c:crossBetween/@val";
318        /// <summary>
319        /// How the axis are crossed
320        /// </summary>
321        public eCrossBetween CrossBetween
322        {
323            get
324            {
325                var v=GetXmlNodeString(_crossBetweenPath);
326                if(string.IsNullOrEmpty(v))
327                {
328                    return eCrossBetween.Between;
329                }
330                else
331                {
332                    try
333                    {
334                        return (eCrossBetween)Enum.Parse(typeof(eCrossBetween), v, true);
335                    }
336                    catch
337                    {
338                        return eCrossBetween.Between;
339                    }
340                }
341            }
342            set
343            {
344                var v = value.ToString();
345                v = v.Substring(0, 1).ToLower(CultureInfo.InvariantCulture) + v.Substring(1);
346                SetXmlNodeString(_crossBetweenPath, v);
347            }
348        }
349        const string _crossesAtPath = "c:crossesAt/@val";
350        /// <summary>
351        /// The value where the axis cross.
352        /// Null is automatic
353        /// </summary>
354        public double? CrossesAt
355        {
356            get
357            {
358                return GetXmlNodeDoubleNull(_crossesAtPath);
359            }
360            set
361            {
362                if (value == null)
363                {
364                    DeleteNode(_crossesAtPath);
365                }
366                else
367                {
368                    SetXmlNodeString(_crossesAtPath, ((double)value).ToString(CultureInfo.InvariantCulture));
369                }
370            }
371        }
372        const string _formatPath = "c:numFmt/@formatCode";
373        /// <summary>
374        /// Numberformat
375        /// </summary>
376        public string Format
377        {
378            get
379            {
380                return GetXmlNodeString(_formatPath);
381            }
382            set
383            {
384                SetXmlNodeString(_formatPath,value);
385            }
386        }
387
388        const string _lblPos = "c:tickLblPos/@val";
389        /// <summary>
390        /// Position of the labels
391        /// </summary>
392        public eTickLabelPosition LabelPosition
393        {
394            get
395            {
396                var v=GetXmlNodeString(_lblPos);
397                if (string.IsNullOrEmpty(v))
398                {
399                    return eTickLabelPosition.NextTo;
400                }
401                else
402                {
403                    try
404                    {
405                        return (eTickLabelPosition)Enum.Parse(typeof(eTickLabelPosition), v, true);
406                    }
407                    catch
408                    {
409                        return eTickLabelPosition.NextTo;
410                    }
411                }
412            }
413            set
414            {
415                string lp = value.ToString();
416                SetXmlNodeString(_lblPos, lp.Substring(0, 1).ToLower(CultureInfo.InvariantCulture) + lp.Substring(1, lp.Length - 1));
417            }
418        }
419        ExcelDrawingFill _fill = null;
420        /// <summary>
421        /// Access to fill properties
422        /// </summary>
423        public ExcelDrawingFill Fill
424        {
425            get
426            {
427                if (_fill == null)
428                {
429                    _fill = new ExcelDrawingFill(NameSpaceManager, TopNode, "c:spPr");
430                }
431                return _fill;
432            }
433        }
434        ExcelDrawingBorder _border = null;
435        /// <summary>
436        /// Access to border properties
437        /// </summary>
438        public ExcelDrawingBorder Border
439        {
440            get
441            {
442                if (_border == null)
443                {
444                    _border = new ExcelDrawingBorder(NameSpaceManager, TopNode, "c:spPr/a:ln");
445                }
446                return _border;
447            }
448        }
449        ExcelTextFont _font = null;
450        /// <summary>
451        /// Access to font properties
452        /// </summary>
453        public ExcelTextFont Font
454        {
455            get
456            {
457                if (_font == null)
458                {
459                    if (TopNode.SelectSingleNode("c:txPr", NameSpaceManager) == null)
460                    {
461                        CreateNode("c:txPr/a:bodyPr");
462                        CreateNode("c:txPr/a:lstStyle");
463                    }
464                    _font = new ExcelTextFont(NameSpaceManager, TopNode, "c:txPr/a:p/a:pPr/a:defRPr", new string[] { "pPr", "defRPr", "solidFill", "uFill", "latin", "cs", "r", "rPr", "t" });
465                }
466                return _font;
467            }
468        }
469        /// <summary>
470        /// If the axis is deleted
471        /// </summary>
472        public bool Deleted
473        {
474            get
475            {
476                return GetXmlNodeBool("c:delete/@val");
477            }
478            set
479            {
480                SetXmlNodeBool("c:delete/@val", value);
481            }
482        }
483        const string _ticLblPos_Path = "c:tickLblPos/@val";
484        /// <summary>
485        /// Position of the Lables
486        /// </summary>
487        public eTickLabelPosition TickLabelPosition
488        {
489            get
490            {
491                string v = GetXmlNodeString(_ticLblPos_Path);
492                if (v == "")
493                {
494                    return eTickLabelPosition.None;
495                }
496                else
497                {
498                    return (eTickLabelPosition)Enum.Parse(typeof(eTickLabelPosition), v, true);
499                }
500            }
501            set
502            {
503                string v = value.ToString();
504                v=v.Substring(0, 1).ToLower(CultureInfo.InvariantCulture) + v.Substring(1, v.Length - 1);
505                SetXmlNodeString(_ticLblPos_Path,v);
506            }
507        }
508        const string _displayUnitPath = "c:dispUnits/c:builtInUnit/@val";
509        const string _custUnitPath = "c:dispUnits/c:custUnit/@val";
510        public double DisplayUnit
511        {
512            get
513            {
514                string v = GetXmlNodeString(_displayUnitPath);
515                if (string.IsNullOrEmpty(v))
516                {
517                    var c = GetXmlNodeDoubleNull(_custUnitPath);
518                    if (c == null)
519                    {
520                        return 0;
521                    }
522                    else
523                    {
524                        return c.Value;
525                    }
526                }
527                else
528                {
529                    try
530                    {
531                        return (double)(long)Enum.Parse(typeof(eBuildInUnits), v, true);
532                    }
533                    catch
534                    {
535                        return 0;
536                    }
537                }
538            }
539            set
540            {
541                if (AxisType == eAxisType.Val && value>=0)
542                {
543                    foreach(var v in Enum.GetValues(typeof(eBuildInUnits)))
544                    {
545                        if((double)(long)v==value)
546                        {
547                            DeleteNode(_custUnitPath);
548                            SetXmlNodeString(_displayUnitPath, ((eBuildInUnits)value).ToString());
549                            return;
550                        }
551                    }
552                    DeleteNode(_displayUnitPath);
553                    if(value!=0)
554                    {
555                        SetXmlNodeString(_custUnitPath, value.ToString(CultureInfo.InvariantCulture));
556                    }
557                }
558            }
559        }       
560        ExcelChartTitle _title = null;
561        /// <summary>
562        /// Chart axis title
563        /// </summary>
564        public ExcelChartTitle Title
565        {
566            get
567            {
568                if (_title == null)
569                {
570                    var node = TopNode.SelectSingleNode("c:title", NameSpaceManager);
571                    if (node == null)
572                    {
573                        CreateNode("c:title");
574                        node = TopNode.SelectSingleNode("c:title", NameSpaceManager);
575                        node.InnerXml = "<c:tx><c:rich><a:bodyPr /><a:lstStyle /><a:p><a:r><a:t /></a:r></a:p></c:rich></c:tx><c:layout /><c:overlay val=\"0\" />";
576                    }
577                    _title = new ExcelChartTitle(NameSpaceManager, TopNode);
578                }
579                return _title;
580            }           
581        }
582        #region "Scaling"
583        const string _minValuePath = "c:scaling/c:min/@val";
584        /// <summary>
585        /// Minimum value for the axis.
586        /// Null is automatic
587        /// </summary>
588        public double? MinValue
589        {
590            get
591            {
592                return GetXmlNodeDoubleNull(_minValuePath);
593            }
594            set
595            {
596                if (value == null)
597                {
598                    DeleteNode(_minValuePath);
599                }
600                else
601                {
602                    SetXmlNodeString(_minValuePath, ((double)value).ToString(CultureInfo.InvariantCulture));
603                }
604            }
605        }
606        const string _maxValuePath = "c:scaling/c:max/@val";
607        /// <summary>
608        /// Max value for the axis.
609        /// Null is automatic
610        /// </summary>
611        public double? MaxValue
612        {
613            get
614            {
615                return GetXmlNodeDoubleNull(_maxValuePath);
616            }
617            set
618            {
619                if (value == null)
620                {
621                    DeleteNode(_maxValuePath);
622                }
623                else
624                {
625                    SetXmlNodeString(_maxValuePath, ((double)value).ToString(CultureInfo.InvariantCulture));
626                }
627            }
628        }
629        const string _majorUnitPath = "c:majorUnit/@val";
630        const string _majorUnitCatPath = "c:tickLblSkip/@val";
631        /// <summary>
632        /// Major unit for the axis.
633        /// Null is automatic
634        /// </summary>
635        public double? MajorUnit
636        {
637            get
638            {
639                if (AxisType == eAxisType.Cat)
640                {
641                    return GetXmlNodeDoubleNull(_majorUnitCatPath);
642                }
643                else
644                {
645                    return GetXmlNodeDoubleNull(_majorUnitPath);
646                }
647            }
648            set
649            {
650                if (value == null)
651                {
652                    DeleteNode(_majorUnitPath);
653                    DeleteNode(_majorUnitCatPath);
654                }
655                else
656                {
657                    if (AxisType == eAxisType.Cat)
658                    {
659                        SetXmlNodeString(_majorUnitCatPath, ((double)value).ToString(CultureInfo.InvariantCulture));
660                    }
661                    else
662                    {
663                        SetXmlNodeString(_majorUnitPath, ((double)value).ToString(CultureInfo.InvariantCulture));
664                    }
665                }
666            }
667        }
668        const string _minorUnitPath = "c:minorUnit/@val";
669        const string _minorUnitCatPath = "c:tickMarkSkip/@val";
670        /// <summary>
671        /// Minor unit for the axis.
672        /// Null is automatic
673        /// </summary>
674        public double? MinorUnit
675        {
676            get
677            {
678                if (AxisType == eAxisType.Cat)
679                {
680                    return GetXmlNodeDoubleNull(_minorUnitCatPath);
681                }
682                else
683                {
684                    return GetXmlNodeDoubleNull(_minorUnitPath);
685                }
686            }
687            set
688            {
689                if (value == null)
690                {
691                    DeleteNode(_minorUnitPath);
692                    DeleteNode(_minorUnitCatPath);
693                }
694                else
695                {
696                    if (AxisType == eAxisType.Cat)
697                    {
698                        SetXmlNodeString(_minorUnitCatPath, ((double)value).ToString(CultureInfo.InvariantCulture));
699                    }
700                    else
701                    {
702                        SetXmlNodeString(_minorUnitPath, ((double)value).ToString(CultureInfo.InvariantCulture));
703                    }
704                }
705            }
706        }
707        const string _logbasePath = "c:scaling/c:logBase/@val";
708        /// <summary>
709        /// The base for a logaritmic scale
710        /// Null for a normal scale
711        /// </summary>
712        public double? LogBase
713        {
714            get
715            {
716                return GetXmlNodeDoubleNull(_logbasePath);
717            }
718            set
719            {
720                if (value == null)
721                {
722                    DeleteNode(_logbasePath);
723                }
724                else
725                {
726                    double v = ((double)value);
727                    if (v < 2 || v > 1000)
728                    {
729                        throw(new ArgumentOutOfRangeException("Value must be between 2 and 1000"));
730                    }
731                    SetXmlNodeString(_logbasePath, v.ToString("0.0", CultureInfo.InvariantCulture));
732                }
733            }
734        }
735        const string _orientationPath = "c:scaling/c:orientation/@val";
736        /// <summary>
737        ///
738        /// </summary>
739        public eAxisOrientation Orientation
740        {
741            get
742            {
743                string v = GetXmlNodeString(_orientationPath);
744                if (v == "")
745                {
746                    return eAxisOrientation.MinMax;
747                }
748                else
749                {
750                    return (eAxisOrientation)Enum.Parse(typeof(eAxisOrientation), v, true);
751                }
752            }
753            set
754            {
755                string s=value.ToString();
756                s=s.Substring(0,1).ToLower(CultureInfo.InvariantCulture) + s.Substring(1,s.Length-1);
757                SetXmlNodeString(_orientationPath, s);
758            }
759        }
760        #endregion
761    }
762}
Note: See TracBrowser for help on using the repository browser.