Click here to Skip to main content
15,878,871 members
Articles / Desktop Programming / WPF

Equation Calculator with Graphing

Rate me:
Please Sign up or sign in to vote.
4.92/5 (69 votes)
25 Nov 2010CPOL9 min read 132.2K   4.2K   158  
Equation Calculator with Graphing
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Xml;
using CommonUtils;
using System.ComponentModel;
using System.Windows.Media;
using System.Windows;
using System.Windows.Controls;

namespace Calculator
{
	public class GraphItem : CommonUtils.IXmlSerialize, INotifyPropertyChanged
	{
		bool m_isEnabled = false;
		string m_equation = string.Empty;
		Brush m_color;
		public bool IsSelected {get; set;}
		public bool IsEnabled
		{
			get { return m_isEnabled; }
			set
			{
				if (m_isEnabled != value)
				{
					m_isEnabled = value;
					NotifyChange("IsEnabled");
				}
			}
		}
		public string Equation
		{
			get { return m_equation;}
			set
			{
				m_equation = value;
				NotifyChange("Equation");
			}
		}
		public string Vars {get; set;}
		public Brush GraphColor 
		{
			get { return m_color; }
			set
			{
				m_color = value;
				NotifyChange("GraphColor");
			}
		}

		public GraphItem()
		{
			GraphColor = Brushes.Beige;
		}
		public GraphItem(XmlElement node)
		{
			Read(node);
		}
		public void Read(XmlElement node)
		{
			try
			{
				if (node != null && node.Name == "graphitem")
				{
					IsEnabled = node.GetBool("enable", true);
					Equation = node.GetString("eq", string.Empty);
					Vars = node.GetString("vars", string.Empty);
					GraphColor = node.GetBrush("color", Brushes.Blue);
				}
			}
			catch { }
		}
		public void Write(XmlTextWriter wr)
		{
			wr.WriteStartElement("graphitem");
			wr.WriteAttribute("enable", IsEnabled);
			wr.WriteAttribute("eq", Equation);
			wr.WriteAttribute("vars", Vars);
			wr.WriteAttribute("color", GraphColor);
			wr.WriteEndElement();
		}

		#region INotifyPropertyChanged Members
		void NotifyChange(string property)
		{
			if (PropertyChanged != null)
				PropertyChanged(this, new PropertyChangedEventArgs(property));
		}
		public event PropertyChangedEventHandler PropertyChanged;

		#endregion
	}

	public class NewGraphItem : GraphItem
	{
	}

	public class GraphItemCollection : ObservableCollection<GraphItem>, CommonUtils.IXmlSerialize
	{
		public const string xmlTag = "graphitems";
		public new void Add(GraphItem item)
		{
			RemoveNewItem();
			base.Add(item);
			AddNewItem();
		}
		public void Read(XmlElement node)
		{
			try
			{
				if (node != null && node.Name == xmlTag)
				{
					Clear();
					foreach (XmlNode child in node.ChildNodes)
					{
						XmlElement element = child as XmlElement;
						if (element == null)
							continue;
						base.Add(new GraphItem(element));
					}
					AddNewItem();
				}
			}
			catch { }
		}
		public void Write(XmlTextWriter wr)
		{
			wr.WriteStartElement("graphitems");
			foreach(IXmlSerialize item in this)
			{
				if (item is NewGraphItem)
					continue;
				item.Write(wr);
			}
			wr.WriteEndElement();
		}
		void RemoveNewItem()
		{
			for (int index = Count - 1; index >= 0; index--)
			{
				if (this[index] is NewGraphItem)
					RemoveAt(index);
			}
		}
		void AddNewItem()
		{
			base.Add(new NewGraphItem());
		}
	}
}

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)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions