0

我有一个在运行时由用户操作的动态图表控件。

用户可以将多个系列添加到图表中。

我正在尝试为用户实现一种按日期过滤每个系列的方法。

MSDN 提供:

// Filters all points where the X value is less than, or equal to, a specific date.    
// The resulting data is stored in an output series, preserving the original data.    
myDataManip.Filter(CompareMethod.LessOrEqual, DateTime.Parse("1/1/2001").ToOADate(), "MySeries", "ResultSeries", "X");

如果我有一个系列并且我知道输入系列名称,那就没问题了。

问题是我将如何为具有多个系列的图表实现此功能

这是我做的一个快速解决方法:

private void button2_Click(object sender, EventArgs e)
{
    var dt = dateTimePicker.Value;
    var tempSeries = new Series[chart1.Series.Count];
    try
    {
        for (var i = 0; i < chart1.Series.Count; i++)
        {
            tempSeries[i] = new Series
            {
                Name = chart1.Series[i].Name,
                IsVisibleInLegend = true,
                IsXValueIndexed = true
            };

            for (var j = 0; j < chart1.Series[i].Points.Count; j++)
            {
                if (DateTime.Parse(chart1.Series[i].Points[j].AxisLabel) <= dt)
                {
                    tempSeries[i].Points.Add(chart1.Series[i].Points[j]);
                }
            }
        }
        chart1.Series.Clear();
        for (var i = 0; i < tempSeries.Count(); i++)
        {
            chart1.Series.Add(tempSeries[i]);
        }
    }
    catch (Exception error)
    {
        MessageBox.Show(error.ToString());
    }
}
4

0 回答 0