Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

OpenWPFChart: assembling charts from components: Part I - Parts

, 19 Mar 2009
Provides the component model along with base components to assemble charts.
SourceCode.zip
trunk
ChartControls
ChartControls.csproj.user
OpenWPFChart.Chart.Icon.png
OpenWPFChart.WellLogChart.Icon.png
Properties
Themes
ChartHelpers
AxisPropertiesDialog
ChartHelpers.csproj.user
ChartScaleControl
FontChooser
ItemPropertiesDialog
Properties
ChartParts
Axes
ChartParts.csproj.user
Grid
Items
Data
DataView
Elements
Visuals
NumericalRecipes
Properties
Scales
ChartParts.vsmdi
SampleDataFiles
WellLog
Samples
ControlSamples
ColumnChartControlSample
Properties
Settings.settings
CurveChartControlSample
Properties
Settings.settings
WellLogControlSample
Properties
Settings.settings
DirectCompositionSamples
BasicSample
Properties
Settings.settings
TemperatureSample
Properties
Settings.settings
WellLogSample
Properties
Settings.settings
// <copyright file="SampledCurveData.cs" company="Oleg V. Polikarpotchkin">
// Copyright © 2008 Oleg V. Polikarpotchkin. All Right Reserved
// </copyright>
// <author>Oleg V. Polikarpotchkin</author>
// <email>ov-p@yandex.ru</email>
// <date>2008-12-23</date>
// <summary>OpenWPFChart library. SampledCurveData class contains set of curve points.</summary>
// <revision>$Id: SampledCurveData.cs 18093 2009-03-16 04:15:06Z unknown $</revision>

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;

namespace OpenWPFChart.Parts
{
	/// <summary>
	/// <see cref="SampledCurveData{TAbs, TOrd}"/> class contains set of curve points of 
	/// <see cref="DataPoint{TAbs, TOrd}"/> type.
	/// </summary>
	/// <typeparam name="TAbs">The type of the abscissa.</typeparam>
	/// <typeparam name="TOrd">The type of the ordinate.</typeparam>
	/// <remarks>
	/// The point collection should be non-strictly ordered by abscissas either 
	/// ascending or descending.
	/// </remarks>
	public class SampledCurveData<TAbs, TOrd> : ItemData
	{
		#region Points
		// TODO Change type of SampledCurveData.Points property to ObservableCollection and supply appropriate TypeConverter?
		IEnumerable<DataPoint<TAbs, TOrd>> points;
		/// <summary>
		/// Gets or sets the <see cref="Points"/> property.
		/// </summary>
		/// <value><see cref="DataPoint{TAbs, TOrd}"/> iterator.</value>
		public IEnumerable<DataPoint<TAbs, TOrd>> Points
		{
			get { return points; }
			set
			{
				if (points != value)
				{
					if (points != null)
					{
						INotifyCollectionChanged iNotifyCollectionChanged
							= points as INotifyCollectionChanged;
						if (iNotifyCollectionChanged != null)
							iNotifyCollectionChanged.CollectionChanged -= CollectionChanged;
					}

					if (value != null)
					{ // Check the value.
						// Has the collection IComparable?
						foreach (Type item in typeof(TAbs).GetInterfaces())
						{
							if (item is IComparable<TAbs>)
							{
								// Check whether the specified point collection is non-strictly ordered.
								if (!isNonStrictlyOrdered(value))
									throw new ArgumentException("Points must be ordered by abscissa", "value");
							}
						}
					}

					points = value;
					if (points != null)
					{
						INotifyCollectionChanged iNotifyCollectionChanged
							= points as INotifyCollectionChanged;
						if (iNotifyCollectionChanged != null)
							iNotifyCollectionChanged.CollectionChanged += CollectionChanged;
					}
					NotifyPropertyChanged("Points");
				}
			}
		}

		/// <summary>
		/// Point collection changed in some way.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.Collections.Specialized.NotifyCollectionChangedEventArgs"/> instance containing the event data.</param>
		private void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
		{
			NotifyPropertyChanged("Points");
		}

		/// <summary>
		/// Determines whether the specified point collection is non-strictly ordered (ascending or descending).
		/// </summary>
		/// <param name="points">The points.</param>
		/// <returns>
		/// 	<c>true</c> if the specified points is ordered (non-strictly); otherwise, <c>false</c>.
		/// </returns>
		static bool isNonStrictlyOrdered(IEnumerable<DataPoint<TAbs, TOrd>> points)
		{
			bool? ordering = null;
			DataPoint<TAbs, TOrd> prevPoint = new DataPoint<TAbs,TOrd>();
			int i = 0;
			IEnumerator<DataPoint<TAbs, TOrd>> en = points.GetEnumerator();
			while (en.MoveNext())
			{
				DataPoint<TAbs, TOrd> point = en.Current;
				if (i++ > 0)
				{
					int comparison = ((IComparable<TAbs>)prevPoint.X).CompareTo(point.X);
					if (comparison != 0)
					{
						if (ordering.HasValue)
						{
							if (ordering.Value != comparison < 0)
								return false;
						}
						else
						{
							ordering = comparison < 0;
						}
					}
				}
				prevPoint = point;
			}
			return true;
		}
		#endregion Points

		/// <inheritdoc />
		public override Type TypeOfAbscissa { get { return typeof(TAbs); } }

		/// <inheritdoc />
		public override Type TypeOfOrdinate { get { return typeof(TOrd); } }

		/// <inheritdoc />
		public override bool IsAbscissasEqual(ItemData item)
		{
			SampledCurveData<TAbs, TOrd> typed = item as SampledCurveData<TAbs, TOrd>;
			if (typed == null)
				return false;

			List<DataPoint<TAbs, TOrd>> points = Points.ToList();
			List<DataPoint<TAbs, TOrd>> otherPoints = typed.Points.ToList();
			if (points.Count != otherPoints.Count)
				return false;
			for (int i = 0; i < points.Count; ++i)
			{
				if (!(points[i].X.Equals(otherPoints[i].X)))
					return false;
			}
			return true;
		}

		/// <inheritdoc />
		public override bool IsOrdinatesEqual(ItemData item)
		{
			SampledCurveData<TAbs, TOrd> typed = item as SampledCurveData<TAbs, TOrd>;
			if (typed == null)
				return false;

			List<DataPoint<TAbs, TOrd>> points = Points.ToList();
			List<DataPoint<TAbs, TOrd>> otherPoints = typed.Points.ToList();
			if (points.Count != otherPoints.Count)
				return false;
			for (int i = 0; i < points.Count; ++i)
			{
				if (!(points[i].Y.Equals(otherPoints[i].Y)))
					return false;
			}
			return true;
		}
	}
}

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)

Share

About the Author

Oleg V. Polikarpotchkin
Team Leader
Russian Federation Russian Federation
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.140821.2 | Last Updated 19 Mar 2009
Article Copyright 2009 by Oleg V. Polikarpotchkin
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid