在mschart中添加数据点时使用双标签

用户名

我正在C#.Net WinForms中编写代码,以在单击图表上的某些数据点时添加标签。在单击图表之前,图形如下所示。

点击前的图表

现在,我单击330-340之间的数据点。该图表显示了带有双标签(338.61)的选定数据点。如下所示:点击后的图表

以下是调试输出: 调试输出

这是chart_mouseclick的以下代码

private void chart1_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {               
        double mouse_Xvalue = chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.X);
        double mouse_Yvalue = chart1.ChartAreas[0].AxisY.PixelPositionToValue(e.Y);

        DataPoint Prev_DataPoint = chart1.Series[0].Points.Select(x => x)
            .Where(x => x.XValue >= mouse_Xvalue)
            .DefaultIfEmpty(chart1.Series[0].Points.First()).First();

        DataPoint Next_DataPoint = chart1.Series[0].Points.Select(x => x)
            .Where(x => x.XValue <= mouse_Xvalue)
            .DefaultIfEmpty(chart1.Series[0].Points.Last()).Last();

        DataPoint Add_DataPoint = Math.Abs(Prev_DataPoint.XValue - mouse_Xvalue) < Math.Abs(Next_DataPoint.XValue - mouse_Xvalue) ? Prev_DataPoint : Next_DataPoint;

        int add_data_point_index = chart1.Series[0].Points.IndexOf(Add_DataPoint);

        DataPoint max = Add_DataPoint;
        for (int i = add_data_point_index - 10; i <= add_data_point_index + 10; i++)
        {
            DataPoint dp = chart1.Series[0].Points[i];

            if (dp.YValues[0] > max.YValues[0])
            {
                add_data_point_index = i;
                max = dp;
            }
        }

        chart1.Series[1].Points.Add(max);
        chart1.Series[1].Sort(PointSortOrder.Ascending, "X");

        for (int i = 0; i < chart1.Series[1].Points.Count; i++)
        {
            chart1.Series[1].Points[i].Label = 
            Math.Round(chart1.Series[1].Points[i].XValue, 2).ToString();
        }                 
    }
}

代码有什么问题?

塔瓦

这实际上是一个非常有趣的观察。

让我们来看第二个版本,它对您来说很好用:

chart1.Series[1].Points.AddXY(max.XValue, max.YValue[0])

或简而言之:

series1.Points.AddXY(x,y);

这是添加的常规方法DataPoints使用两个(或多个)值以及所有其他来自默认值的属性创建一个新点Series

The 'other' properties include colors, markers and label data, including IsValueShownAsLabel .

No surprises here.

Now for the original version:

chart1.Series[1].Points.Add(max);

or in short

series1.Points.Add(dp);

where max (or dp) are DataPoints in the first series (series0).

This behaves surprisingly different..:

One could imagine that the DataPoint gets moved (like Controls would be) but it isn't. Or cloned like strings would be.

Instead only a new reference is created and added to series1.Points. This has several surprising consequences..:

  • Both references point to the same datapoint object; so suddenly there is a point in series1 with properties taken from series0, including IsValueShownAsLabel!

  • 由于两个系列现在都包含一个具有相同值/坐标的数据点,因此图表非常聪明,可以稍微分开地显示其标签,以避免重叠。这就是您看到的效果。

  • 如果现在将数据点从其原始的series0中移除,则series1仍将包含对它的引用,该引用带有颜色,标签等,就像在series0中一样。

因此,实际上没有办法使原始版本起作用,因为对同一点的两次引用将始终使图表显示标签两次或根本不显示标签。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在matlab中向数据结构添加数据点

如何在ggplot曲线中添加数据点?

使用 VBA 添加数据标签

C# 如何向动态添加的 MSChart 添加数据?

如何通过事件侦听器在Matlab gui中的动画行中添加数据点?

使用python在MySQL中添加数据

无法在PrestaShop 1.6的脚本标签中添加数据属性

使用 Flask 在 sqlite3 数据库中添加数据时出错

在添加数据时从数据框中删除列表

与Softmax相比,添加数据点对SVM丢失的影响

添加数据点期间散点图中的静态色图

数据名称添加数据html标签

使用模型类在 firebase 中添加数据时设置自定义键

在Pandas中添加数据框的列时出错

如何为highchart添加数据标签?

使用ggraph时,如果标签文本在图中重叠,如何审查数据点标签(注释)

在localStorage中添加数据

使用路由助手时如何向参数添加数据,需要将数据存储在变量中

图形标签中的多个数据点

添加数据时,RangeBar 图表 AxisX 上出现不需要的标签

使用变量在R中添加数据框列

如何使用PHP在JSON中添加数据

使用express js在文件中写入/添加数据

如何使用scala在csv文件中添加数据?

如何使用JavaScript在Excel中添加数据验证列表

使用多个条件在 MS Access 中添加数据行

如何使用 HTML Agility Pack 在 href 中添加数据

在我的treeview(python tkinter)中,当我添加数据时,我希望基于父名称(tkinter)添加数据

在Windows窗体的标签中添加数字