[英]MsChart Select a portion of data and update Chart
I've created a program that reads in a text file and displays the data from that file into a DataGridView; 我创建了一个程序,该程序读取一个文本文件并将该文件中的数据显示到DataGridView中。 I then use the data from this DGV to update a 'chart' based on the results, the chart only consists of line graphs. 然后,我使用来自该DGV的数据根据结果更新“图表”,该图表仅由折线图组成。
What I'm trying to accomplish is allowing the user to select a portion of that data my dragging a beginning and end over it like you would to zoom in if only the x axis was enabled, and update the the graph based on that selection, calculating averages for this subset of data. 我要完成的工作是允许用户选择我要拖动的数据的一部分,就像在仅启用x轴的情况下进行放大一样,然后根据该选择来更新图形,计算此数据子集的平均值。
Using 运用
chart1.ChartAreas["ChartArea1"].CursorX.IsUserEnabled = Enabled;
chart1.ChartAreas["ChartArea1"].CursorX.IsUserSelectionEnabled = Enabled;
This allows me to select the area and zoom in but I'm unsure how to actually update the data based on the selection rather than just zoom. 这使我可以选择区域并放大,但是我不确定如何根据选择而不是仅根据缩放实际更新数据。
To allow nice zooming you should add this line to the two you show: 要进行良好的缩放,您应该将此行添加到显示的两行中:
chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Zoomable = true;
Here is a function to calculate an average of the visible points' 1st YValues: 这是一个计算可见点的第一个YValue平均值的函数:
private void chart1_AxisViewChanged(object sender, ViewEventArgs e)
{
// for a test the result is shown in the Form's title
Text = "AVG:" + GetAverage(chart1, chart1.Series[0], e);
}
double GetAverage(Chart chart, Series series, ViewEventArgs e)
{
ChartArea CA = e.ChartArea; // short..
Series S = series; // references
DataPoint pt0 = S.Points.Select(x => x)
.Where(x => x.XValue >= CA.AxisX.ScaleView.ViewMinimum)
.DefaultIfEmpty(S.Points.First()).First();
DataPoint pt1 = S.Points.Select(x => x)
.Where(x => x.XValue <= CA.AxisX.ScaleView.ViewMaximum)
.DefaultIfEmpty(S.Points.Last()).Last();
double sum = 0;
for (int i = S.Points.IndexOf(pt0); i < S.Points.IndexOf(pt1); i++)
sum += S.Points[i].YValues[0];
return sum / (S.Points.IndexOf(pt1) - S.Points.IndexOf(pt0) + 1);
}
Note that the ViewEventArgs
parameter is providing the values of the position and size of the View, but these are just the XValues
of the datapoints and not their indices; 请注意, ViewEventArgs
参数提供的是View的位置和大小的值,但这只是数据点的XValues
,而不是它们的索引; so we need to search the Points
from the left for the 1st and from right for the last point. 所以我们需要搜索Points
从左边的1号和正确的最后一个点。
Update Sometimes the zooming runs into a special problem: When the data are more finely grained than the default CursorX.IntervalType
it just won't let you zoom. 更新有时缩放会遇到一个特殊的问题:当数据的粒度比默认的CursorX.IntervalType
它就不允许您缩放。 In such a case you simply have to adapt to the scale of your data, eg like this: CA.CursorX.IntervalType = DateTimeIntervalType.Milliseconds;
在这种情况下,您只需要适应数据规模即可,例如: CA.CursorX.IntervalType = DateTimeIntervalType.Milliseconds;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.