'Donwload File Charting http://www.microsoft.com/en-us/download/details.aspx?id=14422
Imports System.Windows.Forms.DataVisualization.Charting
Public Class Form1
Dim DemoTableSource As DataTable = GetTableNetwork()
Dim Chart1 As New System.Windows.Forms.DataVisualization.Charting.Chart()
Dim GridView As New System.Windows.Forms.DataGridView()
Dim btnTracciaGrafico As New System.Windows.Forms.Button
''' <summary>
''' Demo Funzione con cui si può creare una tabella di tipo DataTable.
''' </summary>
Function GetTableDemo() As DataTable
' Crea una nuova DataTable istansa.
Dim table As New DataTable
' Aggiungi le colonne al DataTable.
table.Columns.Add("Età", GetType(Integer))
table.Columns.Add("Nome", GetType(String))
table.Columns.Add("Cognome", GetType(String))
table.Columns.Add("Data Iscrizione", GetType(DateTime))
' Aggiungi 5 righe e compile le giuste colonne del DataTable.
table.Rows.Add(23, "Rossi", "David", DateTime.Now)
table.Rows.Add(52, "Combivent", "Sam", DateTime.Now)
table.Rows.Add(8, "Dilandog", "Christoff", DateTime.Now)
table.Rows.Add(24, "Enebrel", "Janet", DateTime.Now)
table.Rows.Add(99, "Hydralazine", "Melanie", DateTime.Now)
Return table
End Function
''' <summary>
''' Demo Funzione con cui si può creare una tabella di tipo DataTable.
''' </summary>
Function GetTableNetwork() As DataTable
' Crea una nuova DataTable istansa.
Dim table As New DataTable
' Aggiungi le colonne al DataTable.
table.Columns.Add("ProductName", GetType(String))
table.Columns.Add("UnitsInStock", GetType(Integer))
' Aggiungi 2 righe e compile le giuste colonne del DataTable.
table.Rows.Add("Computer", 10)
table.Rows.Add("Notebook", 23)
table.Rows.Add("Acessori", 203)
table.Rows.Add("Telefono", 80)
table.Rows.Add("Macchina", 3)
table.Rows.Add("Cavi", 23)
Return table
End Function
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.MaximizeBox = False
Me.MinimizeBox = False
GridView.DataSource = DemoTableSource
Chart1.Visible = False
Me.Controls.Add(Chart1)
AddHandler Chart1.MouseMove, AddressOf Me.Chart1_MouseMove
Me.Controls.Add(GridView)
GridView.Location = New Point(12, 12)
GridView.Size = New Size(800, 200)
btnTracciaGrafico.Name = "Traccia il Grafico"
btnTracciaGrafico.Text = "&Traccia il Grafico"
Me.Controls.Add(btnTracciaGrafico)
btnTracciaGrafico.Location = New Point(300, 213)
btnTracciaGrafico.Size = New Size(184, 28)
AddHandler btnTracciaGrafico.Click, AddressOf Me.btnTracciaGrafico_Click
End Sub
Private Sub TracciaGraficoNetwork(ByVal DemoTableSource As DataTable)
Chart1.Visible = True
Dim ChartArea1 As ChartArea = New ChartArea()
Dim Legend1 As Legend = New Legend()
Dim Series1 As Series = New Series()
ChartArea1.Name = "ChartArea1"
Chart1.ChartAreas.Add(ChartArea1)
Legend1.Name = "Legend1"
Chart1.Legends.Add(Legend1)
Chart1.Name = "Chart1"
Series1.ChartArea = "ChartArea1"
Series1.Legend = "Legend1"
Series1.Name = "Stok"
Chart1.Series.Add(Series1)
Chart1.Size = New System.Drawing.Size(800, 400)
Chart1.TabIndex = 0
Chart1.Text = "Chart1"
Chart1.Series("Stok").XValueMember = "ProductName"
Chart1.Series("Stok").YValueMembers = "UnitsInStock"
Chart1.DataSource = DemoTableSource
Chart1.Location = New Point(12, 243)
End Sub
Private Sub btnTracciaGrafico_Click(sender As System.Object, e As System.EventArgs)
TracciaGraficoNetwork(DemoTableSource)
End Sub
Private Sub Chart1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim HTR As DataVisualization.Charting.HitTestResult = Chart1.HitTest(e.X, e.Y)
'Controllo sul fatto che sia un DataPoint valido
If Not HTR.ChartElementType = DataVisualization.Charting.ChartElementType.DataPoint Then Exit Sub
'Controllo sul fatto che sia la Serie desiderata ( Serie4 )
'If Not HTR.Series.Name = "Serie4" Then Exit Sub
Dim p As Integer
For Each Serie As Series In Chart1.Series
For x As Integer = 0 To Chart1.Series(p).Points.Count - 1
Chart1.Series(p).Points(x).Label = String.Empty
Next
p += 1
Next
With Chart1.Series("Stok").Points(HTR.PointIndex)
.Font = New System.Drawing.Font("Arial", 10)
.Label = "Quantità - " & .YValues(0)
.LabelBackColor = Color.YellowGreen
.LabelBorderColor = Color.Black
.LabelBorderWidth = 1
.LabelForeColor = Color.Black
.MarkerStyle = DataVisualization.Charting.MarkerStyle.Star10
End With
End Sub
End Class