带有MS Chart Control VB的交互式图表 [英] Interactive Chart with MS Chart Control VB
问题描述
您好,我目前正在创建图表和img并在视图中显示。但是我想使其更具交互性……例如。当用户将mose放在点上(在点图中)时,他可以看到该点的值。
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.
在这里,我创建了图表并显示了图像
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 ?\
编辑1
I已经阅读了有关工具提示的信息....,但是您必须为每个系列值设置每个工具提示,但我不确定将图表另存为img时是否可以使用工具提示,但会尝试使用
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
推荐答案
您可以将Chart.HitTest方法与MouseMove事件一起使用,以检查鼠标是否位于数据点上。这是一个示例
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
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
它首先执行HitTest并将结果分配给变量 h
。
HitTestResult的ChartElementType属性定义在给定坐标处的元素类型。如果它是一个数据点,则将带有坐标的相应工具提示分配给图表。
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并在图片。我不知道为什么要用这种方法,但是要使图像具有交互性并不容易。
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.
这篇关于带有MS Chart Control VB的交互式图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!