vb 图表的自定义标签

Custom label for vb chart

#vb 图表的自定义标签#

我有一个这样的数据表

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

我想显示这样的 vb 图表

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

到目前为止我只能显示这个

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

下面是我的代码

        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

你能帮帮我吗

如果设置 .Label、.Font、.LabelBackColor 和 .LabelForeColor 属性,您可以看到点的标签,例如我使用以下代码将图表控件放在窗体上:

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

得到这个:

另外,最好一步从数据库中读取所有数据,下一步将数据添加到图表系列,最后将系列添加到图表。这将打开数据库连接的时间保持在最低限度,并避免每次添加点时更新图表所花费的时间。

 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

我将@Andrew Morton Answer 修改为简化的结果,它非常适合我。我希望这对某些人也有帮助