想要实现的效果:
实现方式一:
使用DataBreaking实现
(1)曲线启用DataBreaking
_chart.ViewXY.PointLineSeries[0].DataBreaking.Enabled = true;
(2)向曲线添加数据点时设置如下:
要中断的位置对应的Y轴值是double.NaN,X轴值按正常方式设置
//Convert 'Now' to X value
double _previousX = _chart.ViewXY.XAxes[0].DateTimeToAxisValue(lasttime);
//Store the X value
points[0].X = _previousX;
if (statusStop)
{
//Randomize and store Y value
points[0].Y = double.NaN;
}
else
{
//Randomize and store Y value
points[0].Y = CalculateYValue();
}
//Add the new point into end of first PointLineSeries
_chart.ViewXY.PointLineSeries[0].AddPoints(points, false);
实现方式二:
使用ClipArea实现:
/// <summary>
/// 当前的状态:运行状态,或暂停状态
/// </summary>
bool statusStop = false;
/// <summary>
/// 中断对象列表
/// </summary>
List<ClipArea> clips = new List<ClipArea>();
private void btnStartOrPause_Click(object sender, EventArgs e)
{
if ((sender as Button).Text == "开始")
{
this.timer1.Enabled = true;
(sender as Button).Text = "停止";
DateTime time = DateTime.Now;
double d1 = _chart.ViewXY.XAxes[0].DateTimeToAxisValue(lasttime);
double d2 = _chart.ViewXY.XAxes[0].DateTimeToAxisValue(time);
_chart.BeginUpdate();
ClipArea clipArea = new ClipArea(d1, d2, ClipDirection.X);
clips.Add(clipArea);
_chart.ViewXY.PointLineSeries[0].SetClipAreas(clips.ToArray());
_chart.EndUpdate();
}
else
{
this.timer1.Enabled = false;
(sender as Button).Text = "开始";
}
}