在一张图表中添加多个注释MS图表

时间:2019-07-10 09:16:05

标签: c# vb.net mschart

我想为两个具有相同x值和y值的不同折线图添加两个注释。

带有linechart1(目标折线图)的第一个注释应该始终可见,带有linechart2(任务完成)的第二个注释只能在数据点上可见。

我必须始终显示“ Target”注释,并且一旦鼠标位于其数据点上,“ Taskdone”注释应出现。但是,如果mousemove事件触发的第一个注释(目标)消失,第二个注释出现。

我的样本数据在这里

Month | Target |Task Done | Total Tasks
------|--------|----------|------------
Apr   |  10    |  24.92   |  30
May   |  10    |  14.01   |  35
Jun   |  10    |  18.97   |  40

使用的代码

private sub CreateAnnoation()
    Dim pd_annotate As CalloutAnnotation = New CalloutAnnotation()
    pd_annotate.CalloutStyle = CalloutStyle.RoundedRectangle
    pd_annotate.CalloutAnchorCap = LineAnchorCapStyle.Arrow
    pd_annotate.Height = 6
    pd_annotate.AxisX = chart1.ChartAreas(0).AxisX
    pd_annotate.AxisY = chart1.ChartAreas(0).AxisY
    pd_annotate.SetAnchor(chart1.Series("Target").Points(1))

    pd_annotate.AnchorX = chart1.Series("Target").Points(1).XValue
    pd_annotate.AnchorY = chart1.Series("Target").Points(1).YValues(0)
    pd_annotate.Visible = True
    pd_annotate.Text = "Target : " & chart1.Series("Target").Points(1).YValues(0)
    pd_annotate.BackColor = Color.LightYellow
    pd_annotate.ForeColor = Color.Red
    pd_annotate.Font = New System.Drawing.Font("Calibri", 12, System.Drawing.FontStyle.Bold)
    pd_annotate.LineWidth = 1
    pd_annotate.LineColor = Color.Black
    pd_annotate.LineDashStyle = ChartDashStyle.Solid
    chart1.Annotations.Add(pd_annotate)

    Dim ta As CalloutAnnotation = New CalloutAnnotation
    With ta
        chart1.Annotations.Add(ta)
        AddHandler chart1.MouseMove, AddressOf chart1_MouseMove
    End With
End Sub

Private Sub chart1_MouseMove(sender As Object, e As MouseEventArgs) Handles chart1.MouseMove
        Dim result As HitTestResult = chart1.HitTest(e.X, e.Y)
        Dim ta As New CalloutAnnotation

        If result.ChartElementType = ChartElementType.DataPoint Then
            chart1.Series("TaskDone").Points(result.PointIndex).XValue.ToString()

            Dim thisPt As New PointF(CSng(chart1.Series("TaskDone").Points(result.PointIndex).XValue),
                                CSng(chart1.Series("TaskDone").Points(result.PointIndex).YValues(1)))

            With ta
                .AnchorDataPoint = chart1.Series("TaskDone").Points(result.PointIndex)
                '.LineWidth = 1
                '.X = thisPt.X + 1
                '.Y = thisPt.Y + 1
                '.Text = thisPt.ToString
                .CalloutStyle = CalloutStyle.Rectangle
                .CalloutAnchorCap = LineAnchorCapStyle.Arrow
                .Height = 5
                .BackColor = Color.LemonChiffon
                .ForeColor = Color.DarkBlue
                .Text = "Total Task=" & chart1.Series("TaskDone").Points(result.PointIndex).YValues(1)
                .Font = New Font("Calibri", 10, FontStyle.Bold)
                .Visible = True

                chart1.Annotations(0) = ta
                chart1.Invalidate()
            End With
        End If

    End Sub

enter image description here

0 个答案:

没有答案