Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/Drawing/Chart/ExcelChartLegend.cs

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

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 7.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.Globalization;
35using System.Text;
36using System.Xml;
37using OfficeOpenXml.Style;
38
39namespace OfficeOpenXml.Drawing.Chart
40{
41    /// <summary>
42    /// Position of the legend
43    /// </summary>
44    public enum eLegendPosition
45    {
46        Top,
47        Left,
48        Right,
49        Bottom,
50        TopRight
51    }
52    /// <summary>
53    /// Chart ledger
54    /// </summary>
55    public class ExcelChartLegend : XmlHelper
56    {
57        ExcelChart _chart;
58        internal ExcelChartLegend(XmlNamespaceManager ns, XmlNode node, ExcelChart chart)
59           : base(ns,node)
60       {
61           _chart=chart;
62           SchemaNodeOrder = new string[] { "legendPos", "layout","overlay", "txPr", "bodyPr", "lstStyle", "spPr" };
63       }
64        const string POSITION_PATH = "c:legendPos/@val";
65        /// <summary>
66        /// Position of the Legend
67        /// </summary>
68        public eLegendPosition Position
69        {
70            get
71            {
72                switch(GetXmlNodeString(POSITION_PATH).ToLower(CultureInfo.InvariantCulture))
73                {
74                    case "t":
75                        return eLegendPosition.Top;
76                    case "b":
77                        return eLegendPosition.Bottom;
78                    case "l":
79                        return eLegendPosition.Left;
80                    case "tr":
81                        return eLegendPosition.TopRight;
82                    default:
83                        return eLegendPosition.Right;
84                }
85            }
86            set
87            {
88                if (TopNode == null) throw(new Exception("Can't set position. Chart has no legend"));
89                switch (value)
90                {
91                    case eLegendPosition.Top:
92                        SetXmlNodeString(POSITION_PATH, "t");
93                        break;
94                    case eLegendPosition.Bottom:
95                        SetXmlNodeString(POSITION_PATH, "b");
96                        break;
97                    case eLegendPosition.Left:
98                        SetXmlNodeString(POSITION_PATH, "l");
99                        break;
100                    case eLegendPosition.TopRight:
101                        SetXmlNodeString(POSITION_PATH, "tr");
102                        break;
103                    default:
104                        SetXmlNodeString(POSITION_PATH, "r");
105                        break;
106                }
107            }
108        }
109        const string OVERLAY_PATH = "c:overlay/@val";
110        /// <summary>
111        /// If the legend overlays other objects
112        /// </summary>
113        public bool Overlay
114        {
115            get
116            {
117                return GetXmlNodeBool(OVERLAY_PATH);
118            }
119            set
120            {
121                if (TopNode == null) throw (new Exception("Can't set overlay. Chart has no legend"));
122                SetXmlNodeBool(OVERLAY_PATH, value);
123            }
124        }
125        ExcelDrawingFill _fill = null;
126        /// <summary>
127        /// Fill style
128        /// </summary>
129        public ExcelDrawingFill Fill
130        {
131            get
132            {
133                if (_fill == null)
134                {
135                    _fill = new ExcelDrawingFill(NameSpaceManager, TopNode, "c:spPr");
136                }
137                return _fill;
138            }
139        }
140        ExcelDrawingBorder _border = null;
141        /// <summary>
142        /// Border style
143        /// </summary>
144        public ExcelDrawingBorder Border
145        {
146            get
147            {
148                if (_border == null)
149                {
150                    _border = new ExcelDrawingBorder(NameSpaceManager, TopNode, "c:spPr/a:ln");
151                }
152                return _border;
153            }
154        }
155        ExcelTextFont _font = null;
156        /// <summary>
157        /// Font properties
158        /// </summary>
159        public ExcelTextFont Font
160        {
161            get
162            {
163                if (_font == null)
164                {
165                    if (TopNode.SelectSingleNode("c:txPr",NameSpaceManager) == null)
166                    {
167                        CreateNode("c:txPr/a:bodyPr");
168                        CreateNode("c:txPr/a:lstStyle");
169                    }
170                    _font = new ExcelTextFont(NameSpaceManager, TopNode, "c:txPr/a:p/a:pPr/a:defRPr", new string[] { "legendPos", "layout", "pPr", "defRPr", "solidFill", "uFill", "latin", "cs", "r", "rPr", "t" });
171                }
172                return _font;
173            }
174        }
175        /// <summary>
176        /// Remove the legend
177        /// </summary>
178        public void Remove()
179        {
180            if (TopNode == null) return;
181            TopNode.ParentNode.RemoveChild(TopNode);
182            TopNode = null;
183        }
184        /// <summary>
185        /// Add a legend to the chart
186        /// </summary>
187        public void Add()
188        {
189            if(TopNode!=null) return;
190
191            //XmlHelper xml = new XmlHelper(NameSpaceManager, _chart.ChartXml);
192            XmlHelper xml = XmlHelperFactory.Create(NameSpaceManager, _chart.ChartXml);
193            xml.SchemaNodeOrder=_chart.SchemaNodeOrder;
194
195            xml.CreateNode("c:chartSpace/c:chart/c:legend");
196            TopNode = _chart.ChartXml.SelectSingleNode("c:chartSpace/c:chart/c:legend", NameSpaceManager);
197            TopNode.InnerXml="<c:legendPos val=\"r\" /><c:layout />";                       
198        }
199    }
200}
Note: See TracBrowser for help on using the repository browser.