Drawing Points on C1Chart
One thing I've gotten much better at the longer i've been at ComponentOne is naming my projects. Every new project I create now gets nicely categorized in a proper directory and gets a descriptive name. I have saved about 700 VS2005 and VS2008 projects and some of them are poorly named. I'm not nearly as bad as those guys who have a project named WindowsApplication346, but I get into the habit of naming them like "ChartTest" or "ChartProject" which helps narrow it down to the main C1 control in use but it's vague beyond that. Today I was looking through those old projects for a sample I made years ago for C1Chart in WinForms to help a customer. I never did find the sample I was looking for, but I stumbled upon this one which I thought was kind of neat. It draws points on a C1Chart by clicking and dragging. Sort of like MS Paint. Then I have no idea why I output the points into textboxes on either side, but it looks cool. Looking at the code it's so simple. First I declared a Boolean variable to hold a true value when the user is pressing on the chart with the mouse.
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
Then in the MouseMove event I add points at the mouse coordinates while the user is pressing the mouse. It's that simple.
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
I figured this type of sample could possibly have a use in a real world app, though I'm not quite sure what that would be. Download Sample