← 返回所有博客文章

本篇文章将展示如何通过点击或拖拽在C1Chart 画点。类似于我们经常使用的画板。是不是一个很酷的功能呢?

通过查看代码,我们发现实现这个功能非常的简单。首先,我声明了一个Boolean 变量,当用户点击图表时即设置改变量值为 true。

Dim drawing As Boolean

Private Sub C1Chart1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles C1Chart1.MouseDown

    drawing = True

End Sub

Private Sub C1Chart1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles C1Chart1.MouseUp

    drawing = False

End Sub

通过MouseMove 事件添加点的代码如下:

Private Sub C1Chart1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles C1Chart1.MouseMove

    If drawing Then

        Dim p As New PointF

        'convert client coords to point coords

        C1Chart1.ChartGroups(0).CoordToDataCoord(e.X, e.Y, p.X, p.Y)

        'add point

        C1Chart1.ChartGroups(0).ChartData.SeriesList(0).PointData.Add(p)

    End If

End Sub

Demo 下载:

Download Sample