Chart控件获得鼠标当前图表坐标值问题!
resultAsHitTestResult=Chart1.HitTest(e.X,e.Y)lblPointValue.Text=$"{result.Object.Axis...
result As HitTestResult = Chart1.HitTest(e.X, e.Y)
lblPointValue.Text = $"{result.Object.AxisLabel},{result.Object.YValues(0)}"
VB.net中没有 $这个用法这个(result.Object.AxisLabel},{result.Object.YValues(0)}")用前还要判断,我判断后总是空值,一会我用小号上传个附件,请帮忙完善下,多谢 展开
lblPointValue.Text = $"{result.Object.AxisLabel},{result.Object.YValues(0)}"
VB.net中没有 $这个用法这个(result.Object.AxisLabel},{result.Object.YValues(0)}")用前还要判断,我判断后总是空值,一会我用小号上传个附件,请帮忙完善下,多谢 展开
2个回答
展开全部
Public Class Form1
Private Sub Chart1_MouseMove(sender As Object, e As MouseEventArgs) Handles Chart1.MouseMove
Dim mousePoint As New Point(e.X, e.Y)
Chart1.ChartAreas(0).CursorX.SetCursorPixelPosition(mousePoint, True)
Chart1.ChartAreas(0).CursorY.SetCursorPixelPosition(mousePoint, True)
Chart1.ChartAreas(0).CursorX.LineColor = Color.Orange
Chart1.ChartAreas(0).CursorY.LineColor = Color.DarkOrange
Chart1.ChartAreas(0).CursorX.LineWidth = 2
Chart1.ChartAreas(0).CursorY.LineWidth = 2
Chart1.ChartAreas(0).CursorX.AutoScroll = True
'================================================
Chart1.ChartAreas(1).CursorX.SetCursorPixelPosition(mousePoint, True)
Chart1.ChartAreas(1).CursorY.SetCursorPixelPosition(mousePoint, True)
Chart1.ChartAreas(1).CursorX.LineColor = Color.Orange
Chart1.ChartAreas(1).CursorY.LineColor = Color.DarkOrange
Chart1.ChartAreas(1).CursorX.LineWidth = 2
Chart1.ChartAreas(1).CursorY.LineWidth = 2
Chart1.ChartAreas(1).CursorX.AutoScroll = True
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt As New DataTable
'表增加¨月份、参数1¨参数2 三列
dt.Columns.Add("月份")
dt.Columns.Add("参数1")
dt.Columns.Add("参数2")
Dim dr As DataRow
For i As Integer = 1 To 100
'新增行
dr = dt.NewRow()
'月份 1-12月
dr.Item(0) = i & "月"
Randomize()
'参数1¨
dr.Item(1) = Int(50 * Rnd(8))
Randomize()
'参数2
dr.Item(2) = 600 + Int(2000 * Rnd(7))
dt.Rows.Add(dr)
Next
dr = Nothing
With Me.Chart1
.DataSource = dt 'dt作为achart1的数据源
.Series.Clear()
.Legends.Clear()
.ChartAreas.Clear()
.ChartAreas.Add("参数1")
.ChartAreas.Add("参数2")
.Legends.Add("参数1")
.Legends.Add("参数2")
.Series.Add("参数1")
.Series.Add("参数2")
.Series("参数2").ChartArea = "参数2" '指定Series所属ChartArea
.Series("参数2").Legend = "参数2" '指定Legend所属Series
.Series("参数2").LegendToolTip = "参数1图例"
.Series("参数1").LegendToolTip = "参数1图例"
.Series("参数1").IsValueShownAsLabel = False '标签显示数据值
.Series("参数2").IsValueShownAsLabel = False '标签显示数据值
.Legends("参数2").DockedToChartArea = "参数1" '指定Legend所属ChartArea
.Legends("参数2").DockedToChartArea = "参数2"
'.ChartAreas("参数2").Area3DStyle.Enable3D = True '启用3D样式
End With
Chart1.Series.Add(1)
With (Chart1.Series(0))
'指定x/y轴数y据Y列
.YValueMembers = "参数1"
.XValueMember = "月份"
'图表类型
.ChartType = DataVisualization.Charting.SeriesChartType.Line '曲¨2线?类¤¨¤型¨a
End With
Me.Chart1.DataBind() '绑?¨?定?§数oy据Y源??
With Me.Chart1.Series("参数1")
Dim s1 As Integer
For i As Integer = 0 To .Points.Count - 1
s1 = s1 + Val(.Points(i).GetValueByName("y"))
.Points(i).ToolTip = .Points(i).AxisLabel & .Points(i).GetValueByName("y")
Next
'图a?例¤y显?示o?总á¨1收o?入¨?
' Me.Chart1.Legends("参数1").Title = "总á¨1收o?入¨?"
' .LegendText = s1.ToString
End With
With Chart1.Series(1)
.YValueMembers = "参数2"
.XValueMember = "月份"
.ChartType = DataVisualization.Charting.SeriesChartType.Line
End With
With Me.Chart1.Series("参数1")
.IsValueShownAsLabel = True
For i As Integer = 0 To .Points.Count - 1
.Points(i).ToolTip = .Points(i).AxisLabel & .Points(i).GetValueByName("y")
.Points(i).LegendText = .Points(i).AxisLabel
.Points(i).Label = "#PERCENT"
.SmartLabelStyle.AllowOutsidePlotArea = True
Next
End With
Me.Chart1.AlignDataPointsByAxisLabel("参数2")
With Me.Chart1.Legends("参数2")
.LegendStyle = DataVisualization.Charting.LegendStyle.Column
.Title = "参数2"
End With
End Sub
End Class
追问
多谢 已实现,必需点在曲线上哪个点上才能出现数据,问下怎样实现鼠标移动到4月15日(X轴垂线上)就读出701069这个Y值,就一根线 没必要点到点上才出现数据 还有曲线点太多 想点到点上时也太难了
追答
你都说曲线多了,不定到点,你怎么知道是要哪个曲线的数据?
result As HitTestResult = Chart1.HitTest(e.X, e.Y)
result 里面包含很多数据,你先把里面的数据搞明白吧
来自:求助得到的回答
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询