-
MSChart and DataGridView
Hi I have been trying to work with the new Microsoft MSChart Component. I have been reading the MSChart Samples project found here: http://code.msdn.microsoft.com/mschart
I would like an application that has the ability to drag datapoints with the mouse and looking over the samples tutorial I have successfully achieved this. I would like my application to grab data from an MSAccess database and I understand I have to bind the data to the chart, I have also done this by following a tutorial from the samples.
My question is I would like the chart to be linked with a DataGridView so that the X and Y values are populated with the chartdata, I would like my application to be able to grab the X and Y value directly from when the user drags the datapoint on the chart and update the DataGridView accordingly.
It can then be saved in the MSAccess table.
Here is my code so far:
Code:
Imports System.Windows.Forms.DataVisualization.Charting
Imports System.Data
Imports System.Data.OleDb
Public Class FrmCapacity
Private selectedDataPoint As DataPoint = Nothing
Dim custDS As New DataSet()
'/ <summary>
'/ Mouse Down Event
'/ </summary>
Private Sub Chart1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Chart1.MouseDown
' Call Hit Test Method
Dim hitResult As HitTestResult = Chart1.HitTest(e.X, e.Y)
' Initialize currently selected data point
selectedDataPoint = Nothing
If hitResult.ChartElementType = ChartElementType.DataPoint Then
selectedDataPoint = CType(hitResult.Object, DataPoint)
' Show point value as label
selectedDataPoint.IsValueShownAsLabel = True
' Set cursor shape
Chart1.Cursor = Cursors.SizeNS
End If
End Sub 'Chart1_MouseDown
Private Sub Chart1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Chart1.MouseMove
' Check if data point selected
If Not (selectedDataPoint Is Nothing) Then
' Mouse coordinates should not be outside of the chart
Dim coordinate As Integer = e.Y
If coordinate < 0 Then
coordinate = 0
End If
If coordinate > Chart1.Size.Height - 1 Then
coordinate = Chart1.Size.Height - 1
End If
' Calculate new Y value from current cursor position
Dim yValue As Double = Chart1.ChartAreas("ChartArea1").AxisY.PixelPositionToValue(coordinate)
yValue = Math.Min(yValue, Chart1.ChartAreas("ChartArea1").AxisY.Maximum)
yValue = Math.Max(yValue, Chart1.ChartAreas("ChartArea1").AxisY.Minimum)
' Update selected point Y value
selectedDataPoint.YValues(0) = yValue
' Invalidate chart
Chart1.Invalidate()
Else
' Set different shape of cursor over the data points
Dim hitResult As HitTestResult = Chart1.HitTest(e.X, e.Y)
If hitResult.ChartElementType = ChartElementType.DataPoint Then
Chart1.Cursor = Cursors.Hand
Else
Chart1.Cursor = Cursors.Default
End If
End If
End Sub
'/ <summary>
'/ Mouse Up Event
'/ </summary>
Private Sub Chart1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Chart1.MouseUp
' Initialize currently selected data point
If Not (selectedDataPoint Is Nothing) Then
' Hide point label
selectedDataPoint.IsValueShownAsLabel = False
' reset selected object
selectedDataPoint = Nothing
' Invalidate chart
Chart1.Invalidate()
' Reset cursor style
Chart1.Cursor = Cursors.Default
End If
End Sub 'Chart1_MouseUp
Private Sub FrmCapacity_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Chart1.Series("Series1").ChartType = SeriesChartType.Area
Chart1.Series("Series2").ChartType = SeriesChartType.Column
Chart1.ChartAreas("ChartArea1").AxisX.IsLabelAutoFit = False
' Resolve the address to the Access database
Dim fileNameString As String = "Capacity.mdb"
' Initialize a connection string
Dim myConnectionString As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileNameString
' Define the database query
Dim mySelectQuery As String = "SELECT * FROM Capacity;"
' Create a database connection object using the connection string
Dim myConnection As New OleDbConnection(myConnectionString)
' Create a database command on the connection using query
Dim myCommand As New OleDbCommand(mySelectQuery, myConnection)
' Set chart data source
Chart1.DataSource = myCommand
' Set series members names for the X and Y values
Chart1.Series("Series2").XValueMember = "TodaysDate"
Chart1.Series("Series2").YValueMembers = "CapacityUsed"
Chart1.Series("Series1").XValueMember = "TodaysDate"
Chart1.Series("Series1").YValueMembers = "CapacityFree"
' Data bind to the selected data source
Chart1.DataBind()
End Sub
End Class
any help anyone can give me would be highly appreciated