我们如何计算图表区域中的多个选择?

时间:2017-05-31 11:57:47

标签: c# selection mschart

我想计算图表区域中的多个选项。例如,用户可以将多个选择标记为此图片Multiple selected chart。所以我如何计算此图表中有多少个选择。这是windowsform中的MSmap使用C#。

我的多选代码如下;

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

      private void chart1_SelectionRangeChanged(object sender, CursorEventArgs e)
            {

                ranges.Add(rangeOfCurve);
                selectedIndices.Union(collectDataPoints(chart1.Series[0],rangeOfCurve.Width, rangeOfCurve.Height)).Distinct();

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

            }
            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;
            }

      private void chart1_SelectionRangeChanging(object sender, CursorEventArgs e)
            {

                rangeOfCurve = new SizeF((float)e.NewSelectionStart, (float)e.NewSelectionEnd);
            }

这是我将所选数据导出到新的.csv文件的代码。在这里我添加了按钮点击事件,然后选择区域数据导出另一个.csv文件。但我想说我可以在图表区域添加多个选择但是数据仅导出最后选择的部分。我可以获得所有多个选择数据。这是用于将一个选定区域数据获取到另一个.csv文件的代码。

private void btnExport_Click(object sender, EventArgs e)
 {
     List<Graph> ObservingData = new List<Graph>(); // List to store all available Graph objects from the CSV
     int index = 0;
     using (StreamWriter sw = new StreamWriter(@"D:\CSVFile\NEWFile\Export\NewFile.csv"))
     {
         // Loops through each lines in the CSV
         foreach (string line in System.IO.File.ReadAllLines(pathToCsv))
         {
             // here line stands for each line in the csv file
             string[] CsvLine = line.Split(',');

             // creating an object of type Graph based on the each csv line
             // and adding them to the  List<Graph>
             Graph Instance1 = new Graph();

             if (index == 0)
             {
                 sw.WriteLine(line);
             }    
             else
             {
                 //Add the code here..**
                  if (((chart1.ChartAreas[0].CursorX.SelectionStart))<=index  && ( index<= (chart1.ChartAreas[0].CursorX.SelectionEnd)))
                 {
                     sw.WriteLine(line);
                 }                       
             }
             index++; 
         }
         sw.Close();
     }
     MessageBox.Show("Data are copied to the new .CSV file");
 }

如果你能提供任何帮助来解决这个问题。我非常感谢你。

1 个答案:

答案 0 :(得分:0)

假设您已添加StripLines标记选择,以下是如何从这些DataPoints收集StripLines的示例:

让我们创建一个List<>点列表:

selectionPoints = new List<List<DataPoint>>();

现在我们可以像这样收集DataPoints

List<List<DataPoint>> GetSelectedPoints(ChartArea ca, Series S)
{
    selectionPoints = new List<List<DataPoint>>();

    foreach (var sl in ca.AxisX.StripLines)
    {
        List<DataPoint> points = new List<DataPoint>();
        points = S.Points.Select(x => x)
                  .Where(x => x.XValue >= sl.IntervalOffset
                           && x.XValue <= (sl.IntervalOffset + sl.StripWidth)).ToList();

        selectionPoints.Add(points);
    }
    return selectionPoints;
}

现在我们可以用DataPoints颜色来做事了......:

foreach (var pointList in selectionPoints)
{
    foreach (var dp in pointList) dp.Color = Color.Red;
}

..或导出它们:

string filePath = "D:\\demo.csv";
StringBuilder sb = new StringBuilder();
foreach (var pointList in selectionPoints)
{
    foreach (var dp in pointList)
        sb.Append(dp.XValue + "," + dp.YValues[0] + ";");  // pick your format!
}
File.WriteAllText(filePath, sb.ToString());