vb.net 带有 MS Chart Control VB 的交互式图表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24955908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Interactive Chart with MS Chart Control VB
提问by Jakub Wisniewski
Hi I'am currently creating a Chart to and img and display it in view. But I would like to make it a little more interactive ... eg. when user put mose over point (in point chart) he can see the values of this point.
嗨,我目前正在创建图表和 img 并将其显示在视图中。但我想让它更具互动性......例如。当用户将鼠标放在点上(在点图表中)时,他可以看到该点的值。
Here I create and image of an chart
在这里我创建一个图表的图像
Function GenerateChart(id As Integer, width As Integer, height As Integer) As ActionResult
' Creating chart
Dim chart = New Chart()
Dim area = New ChartArea()
Dim series = New Series()
chart.Width = width
chart.Height = height
' Adding Series to the Chart
chart.Series.Add("ValueSeries")
chart.Series("ValueSeries").XValueMember = "Date"
chart.Series("ValueSeries").YValueMembers = "Value"
chart.Series("ValueSeries").ChartType = SeriesChartType.Point
'chart.Series("ValueSeries1").AxisLabel = "Label"
' Getting data for series
chart.DataSource = GetDataForChart(id)
chart.DataBind()
chart.ChartAreas.Add(New ChartArea())
' Saving chart as file
Dim returnVal = New MemoryStream()
chart.SaveImage(returnVal)
'Return adress for file
Return File(returnVal.GetBuffer(), "image/png")
End Function
are there any special properties that i may add to make it interactive ? And if I add them maybe I should return something diffrant than an image ?\
我可以添加任何特殊属性以使其具有交互性吗?如果我添加它们,也许我应该返回与图像不同的东西?\
EDIT 1
编辑 1
I have read about tool tips .... but you have to set each tooltip for each series value and still i am not sure if tooltips works when you save chart as img but 'll try it
我已经阅读了有关工具提示的信息......但是您必须为每个系列值设置每个工具提示,但我仍然不确定当您将图表另存为 img 时工具提示是否有效,但我会尝试
回答by Jens
You can use the Chart.HitTest method together with the MouseMove event to check for the mouse being over a datapoint. Here is an example
您可以将 Chart.HitTest 方法与 MouseMove 事件一起使用来检查鼠标是否位于数据点上。这是一个例子
Public Class Form1
Dim ToolTipProvider As New ToolTip
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i = 1 To 20
Chart1.Series(0).Points.AddXY(i, i ^ 2)
Next
End Sub
Private Sub Chart1_MouseMove(sender As Object, e As MouseEventArgs) Handles Chart1.MouseMove
Dim h As Windows.Forms.DataVisualization.Charting.HitTestResult = Chart1.HitTest(e.X, e.Y)
If h.ChartElementType = DataVisualization.Charting.ChartElementType.DataPoint Then
ToolTipProvider.SetToolTip(Chart1, h.Series.Points(h.PointIndex).XValue & " x " & h.Series.Points(h.PointIndex).YValues(0))
End If
End Sub
End Class
It first performs a HitTest and assigns the result to the variable h.
The property ChartElementType of the HitTestResult defines what elementtype is located at the given coordinates. If it is a datapoint a corresponding tooltip with the coordinates is assigned to the chart.
它首先执行 HitTest 并将结果分配给变量h。HitTestResult 的属性 ChartElementType 定义了位于给定坐标处的元素类型。如果它是一个数据点,则将带有坐标的相应工具提示分配给图表。


If you do it in a temporary image you need to perform the HitTest in your method and draw the tooltip on the image. I don't know why you do it this way, but making an image interactive is not easily done.
如果您在临时图像中执行此操作,则需要在您的方法中执行 HitTest 并在图像上绘制工具提示。我不知道您为什么要这样做,但是要使图像具有交互性并不容易。

