简介
这是一个附加的Microsoft Web图表控制。这是一个新的系列类型,绘制Bezier曲线,根据你传递的点。使用代码
我最近需要在一个web应用程序的贝塞尔图。微软的网络图控制(这实际上是登打士图表控件)是一个功能强大的控制,所以我说我需要的功能。贝塞尔曲线本身的计算,我用从Tolga Birdal CodeProject上的文章中的代码,你可以找到他的工作。我自己的代码封装在一个类,称为CalcBezierCurve。然后,我创建了一个新的类BezierSeries从System.Web.UI.DataVisualization.Charting.Series继承。所以,你可以使用它在任何Web图表的控制图表。/// <span class="code-SummaryComment"><summary>
</span>///<span class="code-comment"> You can use this Series like any other series in the MS web charting control
</span>/// <span class="code-SummaryComment"></summary>
</span>public class BezierSeries : Series
{
#region fields
private int _pointsOnCurve = 1000;
private List<DataPoint> _bezierPoints = new List<DataPoint>();
#endregion
#region properties
/// <span class="code-SummaryComment"><summary>
</span> ///<span class="code-comment"> Defines how many points the resulting curve will have;
</span> ///<span class="code-comment"> min = 2, must eb an even number
</span> /// <span class="code-SummaryComment"></summary>
</span> public int PointsOnCurve
{
get { return _pointsOnCurve; }
set
{
//<span class="code-comment">min value is 2
</span> if (value < 2)
{
value = 2;
}
//<span class="code-comment">it must be an even number
</span> if (value % 2 == 1)
{
value++;
}
_pointsOnCurve = value;
}
}
/// <span class="code-SummaryComment"><summary>
</span> ///<span class="code-comment"> Points that should be used to calculate the bezier graph
</span> /// <span class="code-SummaryComment"></summary>
</span> public List<DataPoint> BezierPoints
{
get
{
return _bezierPoints;
}
set
{
if (value != null)
{
_bezierPoints = value;
List<double> ptList = new List<double>();
//<span class="code-comment">convert bezier points to flat list
</span> foreach (DataPoint point in value)
{
ptList.Add(point.XValue);
ptList.Add(point.YValues[0]);
}
//<span class="code-comment">bezier curve calculation
</span> 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);
//<span class="code-comment">bezier curve points
</span> this.ChartType = SeriesChartType.Line;
for (int i = 0; i < p.Count(); i = i + 2)
{
this.Points.AddXY(p[i], p[i + 1]);
}
}
}
}
#endregion
}
的用法很简单(我假设你有一个与它称为Chart1的一个图表控件的Web窗体):
{C}
是这样的结果:
{S0}历史2011-03-17 V 1.0:初始版本。