我正在尝试在.NET中创建一个图表,该图表可以显示有关由两个行注释指定的范围的数据。
Private Sub Chart_MouseMove(sender As Object, e As MouseEventArgs) Handles Chart.MouseMove
Dim DataPointList = Chart.Series("default").Points.ToArray
Dim TotalSecs As Int64 = (DataPointList.Length - 1) * 86400
Dim Coord As ChartCoordinates = GetAxisValuesFromMouse(e.X, e.Y)
If RangeSpecified = False Then
Chart.Annotations("StartDotAnnotation").X = Coord.X
Chart.Annotations("StartDotAnnotation").Y = TangentCalculator(Coord.X) ' Attach Line Annotation to mouse movement
Chart.Annotations("StartLineAnnotation").X = Coord.X + 0.1 ' Seems to cause error without + 0.1
Chart.Annotations("StartLineAnnotation").Y = TangentCalculator(Coord.X) ' Attach Line Annotation to mouse movement
Chart.Annotations("ToolTip").X = Coord.X + 0.2
Chart.Annotations("ToolTip").Y = TangentCalculator(Coord.X)
ToolTip.AnchorX = Coord.X + 0.2
ToolTip.AnchorY = TangentCalculator(Coord.X) ' Attach ToolTip Annotation to mouse movement
ToolTipY = TangentCalculator(Coord.X)
ToolTipX = (XAxis0Date.AddSeconds((Coord.X / (DataPointList.Length - 1)) * TotalSecs)).ToString
ToolTip.Text = "" & ToolTipY & ", " & ToolTipX & ""
End If
End Sub
Private Sub Chart_MouseClick(sender As Object, e As MouseEventArgs) Handles Chart.MouseClick
If RangeSpecified = False Then
RangeSpecified = True
EndDotAnnotation.Visible = True
EndLineAnnotation.Visible = True
EndDotAnnotation.X = EndLineAnnotation.X
EndDotAnnotation.Y = EndLineAnnotation.Y
End If
End Sub
表单启动后the line annotation follows the cursor (在MouseMove事件中)。单击鼠标时,the second line annotation appears。用户应该能够单击并拖动线条,并且指定范围内的数据将显示在工具提示中。代码一直有效,直到我尝试选择图表中的任一行。然后它打破并抛出这个错误:
未处理的类型' System.OverflowException'发生在System.Windows.Forms.DataVisualization.dll中 附加信息:溢出错误。
在触发MouseClick事件之前出现错误。我认为它与MouseClick事件有关,因为如果我发表评论,一切正常。我该怎么办?
此外,即使我注释掉MouseClick事件,也要删除" + 0.1"在这一行的最后:
Chart.Annotations("StartLineAnnotation").X = Coord.X + 0.1
如果我点击图表上的任何地方,会给我带来同样的错误。我不确定为什么会这样做。这是一个次要问题,但如果有人能解释它会有所帮助。谢谢你的时间!