在C#中选择图表上的多个部分 [英] Selecting multiple sections on a chart in c#

查看:41
本文介绍了在C#中选择图表上的多个部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用类型列在图表中显示的数据制作一个简单的WindowsFormAplication.

I am trying to make a simple WindowsFormAplication with data displayed in charts with type column.

现在,这个想法是让用户选择图表的一部分,并让程序从同一数据集中找到相同的数字,然后在SAME图表上选择所有这些数字.我的程序就是这样做的,直到需要在该图表上显示多个选择为止.

Now, the idea is for the user to select a part of the chart, and for the program to find the same numbers from the same dataset and select all of them on the SAME chart. My program does just that, up to the point where it needs to display multiple selections on that chart.

我只找到一种选择数据的方法,那就是通过Cursor.SetSelectionPosition(double,double);但似乎没有选择的选项.

I only found one way of selecting data, and that is via Cursor.SetSelectionPosition(double, double); but there doesn't seem to be an option for multiple selection.

在标准图表上甚至可以做到吗?有关在C#中完成此操作的任何建议将不胜感激.

Is this even possible on a standard chart? Any suggestions on how to accomplish this in C# will be much appreciated.

推荐答案

任何时候都只能选择一个范围.

There can only be one range selected at any time.

所以你需要..

  • ..收集范围和
  • ..可能还会收集所选的 DataPoints .
  • 最后,您还需要确定一个UI来清除选择.

一种显示多个选择的简单方法,与光标选择非常相似,就是添加 Striplines ..:

An simple way to display several selections, very similar to the cursor selection is adding Striplines..:

以下是上述结果的代码;请注意,它假设您的值将适合 float ,并滥用了 SizeF 结构来存储选择的开始和结束值.如果您想更加精确,可以将其替换为 Tuple< double,double> ..:

Here is the code for the above result; note that it assumes that your values will fit in a float and abuses the SizeF structure to store the start and end values of the selections. If you want to be more precise you can replace it with a Tuple<double, double>..:

前三个用于保存数据的类级别变量,正在进行的选择,范围列表和 DataPoint 索引列表:

First three class level variables to hold the data, the ongoing selection, the list of ranges and a list of DataPoint indices:

SizeF curRange = SizeF.Empty;
List<SizeF> ranges = new List<SizeF>();
List<int> selectedIndices = new List<int>();

此事件将新选择保存在参数 e 中,因此我们可以将它们存储:

This event holds the new selections in the param e, so we can store them:

private void chart1_SelectionRangeChanging(object sender, CursorEventArgs e)
{
    curRange = new SizeF((float)e.NewSelectionStart, (float)e.NewSelectionEnd);
}

现在选择过程已经完成;选择数据现在已经丢失,但是我们已经存储了它们.因此,我们可以添加新范围,收集新选择的 DataPoint 索引,最后创建并显示新的 StripLine :

Now the selection process is done; the selection data is lost by now, but we have stored them. So we can add the new range, collect the newly selected DataPoint indices and finally create and display a new StripLine:

private void chart1_SelectionRangeChanged(object sender, CursorEventArgs e)
{
    ranges.Add(curRange);
    selectedIndices.Union(collectDataPoints(chart1.Series[0], 
                          curRange.Width, curRange.Height))
                   .Distinct();

    StripLine sl = new StripLine();
    sl.BackColor = Color.FromArgb(255, Color.LightSeaGreen);
    sl.IntervalOffset = Math.Min(curRange.Width, curRange.Height);
    sl.StripWidth = Math.Abs(curRange.Height - curRange.Width);
    chart1.ChartAreas[0].AxisX.StripLines.Add(sl);
}

这个小例程应该收集一个范围内的所有 DataPoint 索引:

This little routine should collect all DataPoint indices in a range:

List<int> collectDataPoints(Series s, double min, double max)
{
    List<int> hits = new List<int>();
    for (int i = 0; i < s.Points.Count ; i++)
        if (s.Points[i].XValue >= min && s.Points[i].XValue <= max) hits.Add(i);
    return hits;
}

要清除选择,请清除两个列表,即 StripLines 集合和 curRange 结构.

To clear the selection you clear the two lists, the StripLines collection, and the curRange structure.

这篇关于在C#中选择图表上的多个部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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