如何让用户在MSChart上创建注释? [英] How to let user create Annotations on MSChart?

查看:54
本文介绍了如何让用户在MSChart上创建注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在运行时创建注释,以及如何使用Annotation.BeginPlacement()启用最终用户放置?我尝试了多种方法来执行此操作,但是无法使其正常工作.在调用BeginPlacement()之后,它应该实时呈现自己.

How do you create an Annotation on-the-run and how do you enable end-user placement with Annotation.BeginPlacement()? I've tried to do this in multiple ways, but cannot get it working. It should render itself in real-time after the BeginPlacement() has been called.

有关此主题的文档很少-几乎没有-因此我无法找到有关此问题的任何帮助.

Documentations on this subject is little to none - and mostly none - so I'm not able to find any help for this problem.

到目前为止,我已经尝试创建一个注释并将其与AnchorX/Y放置在一起,将所有Allow-标志设置为true,并在鼠标移动时将其命名为BeginPlacement(),但在放置该注释时看不到该注释,或者它会相应地放进去吗?例如,LineAnnotation从正确的位置开始,但是没有在我离开它的地方结束.当我移动它使其从我的ChartAreas {0,0}开始时,它将到达终点.

What I've tried so far, is to create an annotation and place it with AnchorX/Y, set all Allow- flags to true and called BeginPlacement() while mouse is moving, but cannot see the annotation while placing it nor will it go in it's place accordingly. For example, LineAnnotation starts in right position, but doesn't end where I left it. When I move it so it starts from my ChartAreas {0,0}, it will hit the end-point.

我想知道什么时候以及如何使用这些工具?我想做的是让用户在图表上绘制注释,并在分析图表时用作工具.

What I want to know, is when and how to use these tools available? What I am trying to do, is to let the user draw annotations on a chart and use as tools when analyzing the charts.

推荐答案

您需要计算正确的位置.请记住,MouseMove不会为您提供位置(百分比)或值(数据),而是像素.您可以使用各种轴功能对其进行变换.正式地,它们仅在xxxPaint事件中起作用,但是在鼠标事件期间它们也可以正常工作.

You need to calculate the right positions. Remember that the MouseMove will not give you positions (percentages) or values(data) but pixels. You can transform them using the various axis functions. Officially they only work in a xxxPaint event, but during mouse events they also work fine.

更新:有两种固定方法:

  • 使用"位置"(即百分比)或值"(即数据值).
  • Either by using the 'Positions', i.e. the percentages or the 'Values', i.e. the data values.

以下是第一种类型的示例:

Here is an example of the 1st kind:

    LineAnnotation laNew = null;

    private void chart1_MouseDown(object sender, MouseEventArgs e)
    {
        if (cbx_drawAnnotation.Checked)
        {
            Axis ax = chart1.ChartAreas[0].AxisX;
            Axis ay = chart1.ChartAreas[0].AxisY;
            laNew = new LineAnnotation();
            chart1.Annotations.Add(laNew);
            double vx = ax.ValueToPosition(ax.PixelPositionToValue(e.X));
            double vy = ay.ValueToPosition(ay.PixelPositionToValue(e.Y));
            laNew.X = vx;
            laNew.Y = vy;
        }
    }


    private void chart1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button.HasFlag(MouseButtons.Left) && cbx_drawAnnotation.Checked)
        {
            Axis ax = chart1.ChartAreas[0].AxisX;
            Axis ay = chart1.ChartAreas[0].AxisY;
            double vx = ax.ValueToPosition(ax.PixelPositionToValue(e.X))- laNew.X;
            double vy = ay.ValueToPosition(ay.PixelPositionToValue(e.Y)) - laNew.Y;
            laNew.Width =  Math.Min(100, vx);
            laNew.Height =  Math.Min(100, vy);
            laNew.LineColor = rb_green.Checked ? Color.Green : Color.Red;
            laNew.AllowMoving = true;  // optional
        }
    }

这很好用,除非您需要以某种方式重新缩放轴,例如更改轴的最小值和/或最大值.

This works fine unles you need to rescale the axis in some way, like changing the axis minimum and/or maximum values.

  • 在这种情况下,您需要锚定数据值.

首先,我们需要将AnnotationAxes关联,并将IsSizeAlwaysRelative设置为false.然后我们可以计算锚点和尺寸值:

First we need to relate the Annotation to the Axes and also set IsSizeAlwaysRelative to false. Then we can calculate the anchor and size values:

private void chart1_MouseDown(object sender, MouseEventArgs e)
{
    if (cbx_drawAnnotation.Checked)
    {
        Axis ax = chart1.ChartAreas[0].AxisX;
        Axis ay = chart1.ChartAreas[0].AxisY;
        laNew = new LineAnnotation();
        chart1.Annotations.Add(laNew);

        laNew.IsSizeAlwaysRelative = false;

        laNew.AxisX = ax;
        laNew.AxisY = ay;

        laNew.AnchorX = ax.PixelPositionToValue(e.X);
        laNew.AnchorY = ay.PixelPositionToValue(e.Y);

        laNew.LineColor = rb_green.Checked ? Color.Green : Color.Red;
        laNew.AllowMoving = true;
    }
}


private void chart1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button.HasFlag(MouseButtons.Left) && cbx_drawAnnotation.Checked)
    {
        Axis ax = chart1.ChartAreas[0].AxisX;
        Axis ay = chart1.ChartAreas[0].AxisY;

        laNew.Width = ax.PixelPositionToValue(e.X) - laNew.AnchorX;   // values
        laNew.Height = ay.PixelPositionToValue(e.Y) - laNew.AnchorY;  
    }
}

请注意,我现在如何缩放最大值,同时仍然调整图表的大小,并且注释与数据点一起保留..

Note how I now can scale the maximum and also still resize the the chart and the annotations stay with the data points..:

更新:要将行限制为ChartArea,请将其添加到MouseDown事件的定义中:

Update: To restrict the line to the ChartArea add this to the definition in the MouseDown event:

 laNew.ClipToChartArea = chart1.ChartAreas[0].Name;

要防止异常离开图表,请将其添加到MouseMove ..:

To prevent an exception from leaving the Chart, add this to the condition in the MouseMove..:

.. && chart1.ClientRectangle.Contains(e.Location)

这篇关于如何让用户在MSChart上创建注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆