在上面的两篇文章中,我们分别讨论了C1Chart在WinForm平台下的同步(一):滚动条同步和C1Chart在WinForm平台下的同步(二):缩放同步,我们讨论了如何在运行时保持两个C1Chart的滚动条同步和缩放同步。
本文是本系列的最后一篇,在前两个的基础上,继续深入讨论在WinForm平台下,保持拖拽标签Mark的同步。同样的,在文章的最后,会附上本文所用到的Demo。
首先,都是一样的,在设计界面拖拽两个C1Chart,保持大小和位置的一致,以及给两个C1Chart导入相同的数据。
代码参考:
c1Chart1.SaveChartToFile("Chart1_Data.xml"); c1Chart2.LoadChartFromFile("Chart1_Data.xml");
接着,给C1Chart添加X轴的ValueLabel,让其显示成可移动的带三角标的Marker的样子,如图:
红色竖直带三角符号的线就是我们需要的Marker。有客户问,这条线是如何添加上去的,有没有内置的接口。
其实,这就是我们经常用到的AxisX的ValueLabels,只是将其Appearance设置为TriangleMarker,并且将Moveable设置为true,这样就可以在X轴上显示一条Marker的线。
注意:要将AxisX.AnnoMethod 属性设置为AnnotationMethodEnum.Mixed,我们设的Marker线才能显示出来。
代码参考:
this.c1Chart1.ChartArea.AxisX.ValueLabels.Add(1, "Marker"); this.c1Chart1.ChartArea.AxisX.ValueLabels[0].Appearance = ValueLabelAppearanceEnum.TriangleMarker; this.c1Chart1.ChartArea.AxisX.ValueLabels[0].Moveable = true; this.c1Chart1.ChartArea.AxisX.ValueLabels[0].GridLine = true; this.c1Chart1.ChartArea.AxisX.ValueLabels[0].Color = Color.Red; c1Chart1.ChartArea.AxisX.AnnoMethod = AnnotationMethodEnum.Mixed;
同样,通过LayoutLabels事件,获取其中一个的ValueLabel的NumericVaue,赋值给另一个C1Chart。
调用该事件代码:
this.c1Chart1.LayoutLabels += new EventHandler(c1Chart1_LayoutLabels); this.c1Chart2.LayoutLabels += new EventHandler(c1Chart2_LayoutLabels);
在该事件中同步的处理代码:
void c1Chart1_LayoutLabels(object sender, EventArgs e) { double currentValueLabel = this.c1Chart1.ChartArea.AxisX.ValueLabels[0].NumericValue; this.c1Chart2.ChartArea.AxisX.ValueLabels[0].NumericValue = currentValueLabel; this.c1Chart2.Refresh(); } void c1Chart2_LayoutLabels(object sender, EventArgs e) { double currentValueLabel = this.c1Chart2.ChartArea.AxisX.ValueLabels[0].NumericValue; this.c1Chart1.ChartArea.AxisX.ValueLabels[0].NumericValue = currentValueLabel; this.c1Chart1.Refresh(); }
通过以上代码,就能保证两个C1Chart在运行时,TriangleMarker保持同步。
例如,运行时,鼠标在C1Chart1上拖拽TriangleMarker发生了移动,C1Chart2会自动移动TriangleMarker保持同步。C1Chart2中也是同理。
最后,附上同步的Demo。
C1Chart_KeepMark.zip (12.50 kb)
如果你对C1Chart感兴趣,请到我们的官网下载最新版本:/developer/componentone-winform/controls/chart
如果你有疑问,可以到GCDN论坛获得技术支持:http://gcdn.grapecity.com.cn/showforum-68.html
本系列同步文章参考: