Home > other >  Custom label for vb chart
Custom label for vb chart

Time:12-21

#Custom label for vb chart#

I have a datatable like this

XValue YValue Title
1 1 A
2 2 B
3 3 C

I want to display a vb chart like this

6|
5|
4|
3|      *C
2|    *B
1|  *A
0|______________________
  0 1 2 3 4 5 6 7 5 8 9

so far i can only display this

6|
5|
4|
3|      *3
2|    *2
1|  *1
0|______________________
  0 1 2 3 4 5 6 7 5 8 9

this is my code below

        Chart20.ChartAreas(0).AxisX.Minimum = 60
        Chart20.ChartAreas(0).AxisX.Maximum = 220

        Chart20.Series("TEMP").Points.Clear()

        Try
            CONN.Open()
            COMMAND = CONN.CreateCommand

            QUERY = "SELECT XValue,YValue,Title FROM vitals"
            COMMAND = New MySqlCommand(QUERY, CONN)
            READER = COMMAND.ExecuteReader

            While READER.Read
                    Chart20.Series("TEMP").Points.AddXY(READER.GetString("YValue"), READER.GetString("XValue"))
            End While

            CONN.Close()
        Catch ex As Exception
            CONN.Close()
            MsgBox(ex.ToString, MsgBoxStyle.Critical)
        End Try

Can you help me out pls

CodePudding user response:

If you set the .Label, .Font, .LabelBackColor, and .LabelForeColor properties you can see the labels of the points, e.g. I put a Chart control on a Form with this code:

Imports System.Windows.Forms.DataVisualization.Charting

Public Class Form1

    Class Datum
        Property X As Integer
        Property Y As Integer
        Property Title As String

        Sub New(x As Integer, y As Integer, t As String)
            Me.X = x
            Me.Y = y
            Me.Title = t
        End Sub

    End Class

    Sub ShowPointLabels()
        Dim data = New List(Of Datum) From {New Datum(1, 1, "A"),
                                            New Datum(2, 2, "B"),
                                            New Datum(3, 3, "C")}

        Chart1.Series.Clear()
        Dim ds As New Series("Sample")
        ds.ChartType = SeriesChartType.Point

        Dim fnt = New Font("Arial", 12)

        For Each d In data
            Dim n = ds.Points.AddXY(d.X, d.Y)
            Dim p = ds.Points(n)
            p.Font = fnt
            p.Label = d.Title
            p.LabelBackColor = Color.PaleGoldenrod
            p.LabelForeColor = Color.DarkBlue
            p.LabelToolTip = "This is the label for " & d.Title

        Next

        Chart1.Series.Add(ds)

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ShowPointLabels()

    End Sub

End Class

To get this:

Chart with data labels

Also, it would be better to read all the data from the database in one step, in the next step add the data to the chart series, and finally add the series to the chart. That keeps the time that the connection to the database is open to a minimum and avoids the time taken to update the chart every time a point is added.

CodePudding user response:

 Sub ShowPointLabels()

        Chart20.Series("TEMP").Points.Clear()

        Try
            CONN.Open()
            COMMAND = CONN.CreateCommand

            QUERY = "SELECT * FROM vitals"
            COMMAND = New MySqlCommand(QUERY, CONN)
            READER = COMMAND.ExecuteReader

            For Each d In READER
                Chart20.Series("TEMP").Points(Chart20.Series("TEMP").Points.AddXY(READER.GetString("XValue"), READER.GetString("YValue"))).Label = READER.GetString("Title")
            Next

            CONN.Close()
        Catch ex As Exception
            CONN.Close()
            MsgBox(ex.ToString, MsgBoxStyle.Critical)
        End Try

    End Sub

I modified @Andrew Morton Answer to a simplified result and it works perfectly for me. I hope this helps somebody as well

  • Related