问题
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 ?\
EDIT 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
回答1:
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
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.
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.
来源:https://stackoverflow.com/questions/24955908/interactive-chart-with-ms-chart-control-vb