在VB中使图表中的标签可选择/可编辑



就像在excel中创建图表一样,您可以双击标题,系列名称或轴标签,然后您可以在该空间中键入。我需要做些什么才能在我的图表上做到这一点?不知道从哪里开始。看来我得创建一个自定义标签?

使用图表的HitTest方法。

Imports System.Windows.Forms.DataVisualization.Charting
Public Class Form1
    Private Sub Chart1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles Chart1.MouseDoubleClick
        Dim h As HitTestResult = Chart1.HitTest(e.X, e.Y) 'Perform the HitTest with the mouse position that was clicked
        If h.ChartElementType = ChartElementType.AxisTitle OrElse _
            h.ChartElementType = ChartElementType.Axis Then 'Check the type of the element of the chart that was clicked
            Dim s As String = InputBox("Please enter a new title!", "", h.Axis.Title) 'Prompt for a new title
            If s <> "" Then h.Axis.Title = s 'Assign the new title
        End If
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For i = 1 To 20 'Put some data in the chart to make the axes visible
            Chart1.Series(0).Points.AddXY(i, i ^ 2)
        Next
    End Sub
End Class

您基本上使用HitTest方法的鼠标位置。ChartElementType定义了在该位置单击了什么元素。如果是轴的标题或者轴本身的标题,它会提示你输入一个新的标题,并分配这个标题。

InputBox是相当旧的,不应该真正使用,但我很懒,它的工作:-)

显然,我指的是存在于datavizalization . charts . textannotation中的内容。除了这个,还有更多类型的注释。

textnotation允许:AnchorMoving, Moving, PathEditing, resize, selection, and TextEditing。这些都是我想要的

简单设置:

Dim anno As New DataVisualization.Charting.TextAnnotation
anno.AllowTextEditing = true
anno.AllowSelecting = true
anno.AllowMoving = true
anno.AllowResizing = true
anno.x = 50
anno.y = 50
anno.text = "Your Text"
chart.annotations.add(xAxisAnno)

最新更新