Click here to Skip to main content
15,896,207 members
Articles / Web Development / ASP.NET

Bezier curve for the Microsoft Web Chart control

Rate me:
Please Sign up or sign in to vote.
4.67/5 (5 votes)
17 Mar 2011CPOL 34.2K   548   19  
Adds a new series that can be used in the MS Web Chart control which will draw a Bezier curve for the construction points you define.
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.DataVisualization.Charting;

namespace BezierCurve
{
	/*
	 * This class is a work of Rainer Halanek, Copyright 2011
	 * 
	 * It is licensed under the Code Project Open License (CPOL), see http://www.codeproject.com/info/cpol10.aspx
	 * 
	 * That grants you the following things:
	 *		Source Code and Executable Files can be used in commercial applications;
	 *		Source Code and Executable Files can be redistributed; and
	 *		Source Code can be modified to create derivative works.
	 *		No claim of suitability, guarantee, or any warranty whatsoever is provided. The software is provided "as-is".
	 *		The Article(s) accompanying the Work may not be distributed or republished without the Author's consent
	 *		
	 * I also use code from a CodeProject article by Tolga Birdal which is also licensed under the CPOL
	 * you can find his work here: http://www.codeproject.com/KB/recipes/BezirCurves.aspx
	 * 
	 * Please do not remove my copyright if you use that class!
	 * 
	 */

	/// <summary>
	/// You can use this Series like any other series in the MS web charting control
	/// </summary>
	public class BezierSeries : Series
	{
		#region fields

		private int _pointsOnCurve = 1000;
		private List<DataPoint> _bezierPoints = new List<DataPoint>();

		#endregion

		#region properties

		/// <summary>
		/// Defines how many points the resulting curve will have;
		/// min = 2, must eb an even number
		/// </summary>
		public int PointsOnCurve
		{
			get { return _pointsOnCurve; }
			set
			{
				//min value is 2
				if (value < 2)
				{
					value = 2;
				}

				//it must be an even number
				if (value % 2 == 1)
				{
					value++;
				}

				_pointsOnCurve = value;
			}
		}

		/// <summary>
		/// Points that should be used to calculate the bezier graph
		/// </summary>
		public List<DataPoint> BezierPoints
		{
			get
			{
				return _bezierPoints;
			}
			set
			{
				if (value != null)
				{
					_bezierPoints = value;

					List<double> ptList = new List<double>();

					//convert bezier points to flat list
					foreach (DataPoint point in value)
					{
						ptList.Add(point.XValue);
						ptList.Add(point.YValues[0]);
					}

					//bezier curve calculation			
					CalcBezierCurve bc = new CalcBezierCurve();
					double[] ptind = new double[ptList.Count];
					double[] p = new double[PointsOnCurve];
					ptList.CopyTo(ptind, 0);
					bc.Bezier2D(ptind, (PointsOnCurve) / 2, p);

					//bezier curve points
					this.ChartType = SeriesChartType.Line;

					for (int i = 0; i < p.Count(); i = i + 2)
					{
						this.Points.AddXY(p[i], p[i + 1]);
					}
				}
			}
		}

		#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)
Austria Austria
Born and living in Vienna, Austria. Started with Pascal in 1993 and MS-DOS 5.0. After that a little C++ in OS/2 and loads of VBA with Access in Windows 95,98, NT. To get more professionel I started C# in 2002 and did some MCP exams on that. After working for my own company I got hired by different companies. Currently I'm employed at the Federal Chambers of Commerce as a Senior Software Engineer.

Comments and Discussions