How to Draw A GraphPaper Type Grid Using System.Windows.Forms.DataVisualization.Charting Control in VB.Net

  • Thread starter Thread starter CoralSpringsTony1
  • Start date Start date
C

CoralSpringsTony1

Guest
I am using Visual Studio 2017 in VB.Net. I am using code from another question answered by TnTinMN



titled "Draw lines on Charts with Mouse Movements in Visual Basic " My idea is to have a graphpaper background that I can add lines with mouse movements to make a rectangle, and add text inside a rectangles I made, Then save everything to a .jpg. Using his example, I made the graph, added text, added lines started at mouse point. The problem is that his example makes a graph with just two columns and is graduated. I only want a graph made up of many columns and many rows as on a piece of graph paper. Is that possible to do? Thank you for your help. here is the code Is started with ...

'Add Project Reference to: System.Windows.Forms.DataVisualization
Imports System.Windows.Forms.DataVisualization.Charting

Public Class Form1
Dim WithEvents Chart1 As New Chart
Dim ChartArea1 As New System.Windows.Forms.DataVisualization.Charting.ChartArea()
Dim Legend1 As New System.Windows.Forms.DataVisualization.Charting.Legend()
Dim Series1 As New System.Windows.Forms.DataVisualization.Charting.Series()

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
With Chart1
.ChartAreas.Add(ChartArea1)
.Legends.Add(Legend1)
.Location = New System.Drawing.Point(5, 5)
.Name = "Chart1"
.Series.Add(Series1)
.Size = New System.Drawing.Size(300, 200)
.Text = "Chart1"
.BackColor = Color.Aquamarine
.Parent = Me
End With
With Series1
.ChartArea = "ChartArea1" : .Legend = "Legend1" : .Name = "Series1"
.ChartType = SeriesChartType.Point Or SeriesChartType.Line
.Points.AddXY(0, 0) : .Points.AddXY(3, 3)
.MarkerStyle = MarkerStyle.Diamond : .MarkerSize = 10
.XAxisType = AxisType.Primary : .YAxisType = AxisType.Primary
.XValueType = ChartValueType.Single : .YValueType = ChartValueType.Single
End With
ChartArea1.Name = "ChartArea1"
Legend1.Name = "Legend1"
End Sub

Private Structure LineEndPoints
Dim StartPoint As Point
Dim endPont As Point
End Structure

Private LinesList As New List(Of LineEndPoints)
Private FirstPoint As Point
Private TempPoint As Point

Private Sub Chart1_MouseDown( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Chart1.MouseDown

FirstPoint = New Point(e.X, e.Y)
TempPoint = New Point(e.X, e.Y)
End Sub

Private Sub Chart1_MouseMove( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Chart1.MouseMove
If Not FirstPoint = Nothing Then
Dim g As Graphics = Chart1.CreateGraphics
Dim ErasePen As Pen = New Pen(Me.BackColor)
g.DrawLine(ErasePen, FirstPoint, TempPoint)
TempPoint = New Point(e.X, e.Y)
Me.Refresh()
End If
End Sub

Private Sub Chart1_MouseUp( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Chart1.MouseUp
Dim Line As New LineEndPoints With {.StartPoint = FirstPoint, .endPont = New Point(e.X, e.Y)}
LinesList.Add(Line)
FirstPoint = Nothing
Me.Refresh()
End Sub

Private Sub Chart1_Paint( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles Chart1.Paint
For Each line As LineEndPoints In LinesList
e.Graphics.DrawLine(Pens.Black, line.StartPoint, line.endPont)
Next
If Not FirstPoint = Nothing Then
e.Graphics.DrawLine(Pens.Black, FirstPoint, TempPoint)
End If
End Sub

End Class

Continue reading...
 
Back
Top