Click here to Skip to main content
15,896,328 members
Articles / Desktop Programming / Windows Forms

Making SVG Charts with Common Objects

Rate me:
Please Sign up or sign in to vote.
4.79/5 (15 votes)
22 Jun 2010CPOL4 min read 86.3K   3.4K   70  
This article explains how to create some interesting charts in SVG documents
//AUTHOR	: GERARD CASTELLÓ
//DATE		: 17/JUN/2010

using System;
using System.Drawing;
using System.Xml;

namespace SVGObjects
{
    public class Polygon : SVGTranform
    {
        #region Attributes

        private SVGPoint[] points;
        private Color color;
        private bool border;

        #endregion

        #region Properties

        public SVGPoint[] Points
        {
            get
            {
                return this.points;
            }
        }

        public Color Color
        {
            get
            {
                return this.color;
            }
            set
            {
                this.color = value;
            }
        }

        public bool Border
        {
            get
            {
                return this.border;
            }
            set
            {
                this.border = value;
            }
        }

        #endregion

        #region Constructor

        public Polygon()
        {
            this.points = new SVGPoint[0];
            this.color = Color.White;
        }

        #endregion

        #region Methods
        
        /// <summary>
        /// Add a point to the array
        /// </summary>
        /// <param name="_xvalue">double</param>
        /// <param name="_yvalue">double</param>
        public void AddPoint(double _xvalue, double _yvalue)
        {
            Array.Resize(ref this.points, this.points.Length + 1);
            SVGPoint point = new SVGPoint(Math.Round(_xvalue, 2), Math.Round( _yvalue, 2));
            this.points[this.points.Length - 1] = point;            
        }

        /// <summary>
        /// Add a point to the array
        /// </summary>
        /// <param name="_point">SVGPoint</param>
        public void AddPoint(SVGPoint _point)
        {
            this.AddPoint(_point.X, _point.Y);
        }

        /// <summary>
        /// Add points to the current array
        /// </summary>
        /// <param name="_points">Point[]</param>
        public void AddPoint(SVGPoint[] _points)
        {
            int pointLength = this.points.Length;
            Array.Resize(ref this.points, this.points.Length + _points.Length);
            for (int i = 0; i < _points.Length; i++)
            {
                this.points[pointLength] = _points[i];
                pointLength++;
            }
        }

        /// <summary>
        /// Get a XmlNode with all details of this object
        /// </summary>
        /// <returns>XmlNode</returns>
        public XmlNode ToXml()
        {
            XmlDocument xmlPolygon = new XmlDocument();
            xmlPolygon.LoadXml("<polygon></polygon>");
            XmlAttribute xmlColor = xmlPolygon.CreateAttribute("color");
            XmlAttribute xmlPoints = xmlPolygon.CreateAttribute("points");
            XmlAttribute xmlBorder = xmlPolygon.CreateAttribute("border");
            xmlColor.Value = ColorTranslator.ToHtml(this.color);
            xmlBorder.Value = this.Border.ToString().ToLower();

            if (this.points.Length > 0)
            {
                xmlPoints.Value = string.Empty;
                System.Globalization.CultureInfo ci = SVGObject.CI;

                if (this.points.Length > 0)
                {
                    int i = 0;
                    while (i < this.points.Length - 1)
                    {
                        xmlPoints.Value += string.Format(ci.NumberFormat, "{0},{1} ", this.points[i].X, this.points[i].Y);
                        i++;
                    }
                    xmlPoints.Value += string.Format(ci.NumberFormat, "{0},{1}", this.points[i].X, this.points[i].Y);
                }
            }

            xmlPolygon.FirstChild.Attributes.Append(xmlColor);
            xmlPolygon.FirstChild.Attributes.Append(xmlPoints);
            xmlPolygon.FirstChild.Attributes.Append(xmlBorder);
            return xmlPolygon.FirstChild;
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Spain Spain
http://www.linkedin.com/in/gerard-castello-viader
https://github.com/gcastellov

Comments and Discussions