MSChart的选择数据和更新图表的部分, [英] MsChart Select a portion of data and update Chart
问题描述
我创建了一个程序,读取文本文件并从该文件到一个DataGridView显示数据;然后,我使用的数据从这个DGV更新基于结果的图,该图只包括线图。
I've created a program that reads in a text file and displays the data from that file into a DataGridView; I then use the data from this DGV to update a 'chart' based on the results, the chart only consists of line graphs.
我试图做到的是允许用户选择该数据我的拖动开始的一部分,并结束在它就像您放大如果启用仅x轴,并更新基于该选择的图形,计算平均值用于数据的子集。
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.
使用
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.
推荐答案
要允许很好的放大你应该加入这一行你展示两个:< 。/ p>
To allow nice zooming you should add this line to the two you show:
chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Zoomable = true;
下面是计算可见点1 YValues平均功能:
Here is a function to calculate an average of the visible points' 1st YValues:
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);
}
请注意, ViewEventArgs
参数提供查看的位置和大小的值,但这些只是 XValues
数据点的指数,而不是;所以我们需要搜索分数
从左边的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; so we need to search the Points
from the left for the 1st and from right for the last point.
< STRONG>更新有时变焦运行到一个特殊的问题:当数据比默认的更细粒度 CursorX.IntervalType
它只是不会让您放大。在这种情况下,你只需去适应你的数据的规模,例如像这样: CA.CursorX.IntervalType = DateTimeIntervalType.Milliseconds;
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. In such a case you simply have to adapt to the scale of your data, e.g. like this: CA.CursorX.IntervalType = DateTimeIntervalType.Milliseconds;
这篇关于MSChart的选择数据和更新图表的部分,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!