【发布时间】:2017-06-09 19:31:06
【问题描述】:
我想统计图表区域的多选。作为一个例子,用户可以将多选标记为这张图片,Multiple selected chart。那么我如何计算这个图表中有多少多选。这是使用C#在windowsform中的MSchart .
我的多选代码如下;
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");
}
如果你能提供任何帮助来解决这个问题。我非常感谢你。
【问题讨论】:
-
您可以枚举 StripLines 集合并测试每个集合的属性。你想要关于带状线的信息(顺便说一句,这不是真正的“选择”)还是关于数据点的信息?
-
@TaW。我是这门语言的初学者。所以请帮我解决这个问题。我想在图表上显示多项选择,我想在 .csv 中获得多项选择的数据点我有的文件。所以我想计算每个多选区域的边界。我可以通过使用'chart1.ChartAreas[0].CursorX.SelectionStart'和'chart1.ChartAreas[0].CursorX.SelectionEnd来获得一个选定的数据区域' 但我无法一一获得多选区域。所以请给我任何帮助。
-
轴只能有一个光标,因此您只能通过这种方式创建一个选择。带状线也不是创建选择的最简单方法。 - 有关仅使用鼠标事件创建多个选择的示例,请参见链接!如果您有任何问题,请随时提问!
-
@TaW 感谢您的帮助。但我的问题是如何获取此图表中带状线之间的所有选定数据。请提出任何我想做的建议。
-
我可以重新打开这个问题,但我不能让你从决定你想要的 UI 中解脱出来。你是否编写了一个循环遍历 Stiplines 集合的方法?您是否设法用鼠标创建多个带状线?