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

查看:32
本文介绍了如何让用户在 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-flags 设置为 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.

这是第一种示例:

    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.然后我们可以计算anchor和size值:

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天全站免登陆