如何通过在图表区域上移动鼠标指针来显示折线图的信息

时间:2017-04-20 15:43:30

标签: vb.net

我在visual studio 2015中有一个桌面应用程序,它有一个折线图。我想在图表区域内移动鼠标时得到(x,y)点的值,所以我不必放置每个标签,因为它会被标签淹没,这将是一团糟。想法是使点(或某种垂直和水平相交线)跟随鼠标指针的X位置,但根据Y轴值改变Y位置。

下面的图片以更好的方式(我希望)展示了我正在寻找的东西。您可以看到鼠标指针位于图表区域中,并且有一个蓝点(我用红色圈出),它位于鼠标指针的X位置和图形Y轴的Y位置。请注意,如果它位于线的上方或下方并不重要,则蓝点位于该线中。

Click here to see the image. I can't post embedded images yet.

现在,X和Y的值如上所示(在红色矩形内)。将这些值放在蓝点附近会很好,比如注释或类似的东西。

编辑: 我设法取得了一些进展,但出现了其他问题。我可以在ChartArea2(这是预期的)中放置一个注释,但发生了两件事:

1)注释出现我单击ChartArea1或ChartArea2。 2)它创建两个注释而不是一个。

我使用的代码如下。

使用图表区域和系列创建图表:



Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim cargo As String = ComboBox1.Text
        Dim fi As Date = DateTimePicker1.Text
        Dim ff As Date = DateTimePicker2.Text
        Dim tabla As New DataTable
        Dim fechas As New DataTable
        Dim k As Integer
        Dim green As Integer
        Dim posX, posY, width, height As Single
        Dim numberOfAreas As Integer = 2


        green = RGB(118, 183, 3)

        cn.ConnectionString = ""
        Try
            tabla.Clear()
            fechas.Clear()

            Dim sql = ("select p.nombre, s.servicio_nombre, dp.etapa, dp.carga, fecha_inicial, fecha_final, extract (epoch from fecha_final - fecha_inicial)/86400 as duracion 
                        from desarrolloproyecto dp natural join cargo c natural join proyecto p natural join servicio s 
                        where nombre_cargo = '" & cargo & "' AND fecha_inicial BETWEEN '" & fi & "' AND '" & ff & "'
                        OR
                        nombre_cargo = '" & cargo & "' AND fecha_final BETWEEN '" & fi & "' AND '" & ff & "'
                        order by p.nombre desc")
            Dim sqlf = ("select t1.fechas, t1.carga from 
                        (select (generate_series(fecha_inicial, fecha_final, '1 day'::interval))::date AS fechas, sum(dp.carga) as carga
                        FROM desarrolloproyecto dp NATURAL JOIN cargo c NATURAL JOIN proyecto p NATURAL JOIN servicio s 
                        where  nombre_cargo = '" & cargo & "'
                        group by fechas
                        order by fechas)t1
                        where t1.fechas BETWEEN '" & fi & "' AND '" & ff & "'
                        order by t1.fechas")

            Dim da As New Npgsql.NpgsqlDataAdapter(sql, cn)
            Dim daf As New Npgsql.NpgsqlDataAdapter(sqlf, cn)

            cn.Open()
            da.Fill(tabla)
            daf.Fill(fechas)

            With Chart1
                .Series.Clear()
                .Series.Add("fechas")
                .Series.Add(cargo)
                .Series.Add("fech")
                .Series(cargo).ChartArea = "ChartArea1"
                .Series("fechas").ChartArea = "ChartArea1"
                .Series("fech").ChartArea = "ChartArea2"




                .Legends("Legend1").Alignment = StringAlignment.Center
                .Legends("Legend1").Docking = Docking.Bottom
                .Series("fechas").IsVisibleInLegend = False
                .Series("fech").IsVisibleInLegend = False
                .Series("fechas").Color = Color.Transparent
                .Series(cargo).Color = Color.FromArgb(118, 183, 3)
                .Series(cargo).CustomProperties = "DrawingStyle = Cylinder"
                .Series("fech").Color = Color.FromArgb(118, 183, 3)

                .Series("fech").ChartType = SeriesChartType.Line
                .Series("fechas").ChartType = SeriesChartType.StackedBar
                .Series(cargo).ChartType = SeriesChartType.StackedBar

                .ChartAreas("ChartArea1").Position = New ElementPosition(0, 0, 95, 70)
                .ChartAreas("ChartArea1").CursorX.IsUserEnabled = True
                .ChartAreas("ChartArea1").CursorX.IsUserSelectionEnabled = True
                .ChartAreas("ChartArea1").AxisX.ScaleView.Zoomable = True
                .ChartAreas("ChartArea1").AxisX.ScrollBar.IsPositionedInside = False
                .ChartAreas("ChartArea1").CursorY.IsUserEnabled = True
                .ChartAreas("ChartArea1").CursorY.IsUserSelectionEnabled = True
                .ChartAreas("ChartArea1").AxisY.ScaleView.Zoomable = True
                .ChartAreas("ChartArea1").AxisY.ScrollBar.IsPositionedInside = False
                .ChartAreas("ChartArea1").AxisY.Minimum = fi.ToOADate 'valores minimos y maximos de ejes x e y
                .ChartAreas("ChartArea1").AxisY.Maximum = ff.ToOADate
                .ChartAreas("ChartArea1").AxisY.IntervalType = DateTimeIntervalType.Auto
                .ChartAreas("ChartArea1").AxisX.IntervalOffset = 0
                .ChartAreas("ChartArea1").AxisX.Interval = 1

                
                .ChartAreas("ChartArea2").Position = New ElementPosition(0, 70, 95, 25)
                .ChartAreas("ChartArea2").CursorX.IsUserEnabled = True
                .ChartAreas("ChartArea2").CursorY.IsUserEnabled = True
                .ChartAreas("ChartArea2").AxisX.Minimum = fi.ToOADate
                .ChartAreas("ChartArea2").AxisX.Maximum = ff.ToOADate
                .ChartAreas("ChartArea2").AxisX.IntervalType = DateTimeIntervalType.Auto



                .Series("fech").SmartLabelStyle.Enabled = True


                For k = 0 To tabla.Rows.Count - 1
                    .Series("fechas").Points.AddXY(tabla.Rows(k).Item(0) & "-" & tabla.Rows(k).Item(1) & "-E" & tabla.Rows(k).Item(2), tabla.Rows(k).Item(4))
                    .Series(cargo).Points.AddY(tabla.Rows(k).Item(6))
                Next

                For k = 0 To fechas.Rows.Count - 1
                    .Series("fech").Points.AddXY(fechas.Rows(k).Item(0), fechas.Rows(k).Item(1))
                    If k = 0 Then
                        posY = fechas.Rows(0).Item(1)
                        posX = fechas.Rows(0).Item(1)
                    Else
                        posY = Min(posY, fechas.Rows(k).Item(1))
                        posX = Max(posX, fechas.Rows(k).Item(1))
                    End If

                Next
                .ChartAreas("ChartArea2").AxisY.Minimum = posY - 25
                .ChartAreas("ChartArea2").AxisY.Maximum = posX + 25
                


            End With
            Label1.Focus()

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        cn.Close()
    End Sub




现在,对于注释部分,它如下:



Private Sub Chart1_CursorPositionChanging(sender As Object, e As CursorEventArgs) Handles Chart1.CursorPositionChanging

        
        Dim a As CalloutAnnotation = New CalloutAnnotation()
        a.AnchorDataPoint = Chart1.Series("fech").Points(0)
        a.Text = "Select this Annotation Object\nand move the Anchor point"
        Chart1.Annotations.Add(a)

    End Sub




所以我想我现在有3个问题。

1)只有当我将移模器移到ChartArea2上时,如何才能显示注释? (我们的想法不是点击ChartArea2。

2)我怎样只能出现一个注释?

3)当我沿X轴移动鼠标指针时,如何移动注释?我希望注释共同点是x =鼠标指针值和y = y轴值。

0 个答案:

没有答案