chart control filter multiple series by date

929 Views Asked by At

I have a dynamic chart control that's manipulated by the user during run time.

The user has the ability to add multiple series' to the chart.

I'm trying to implement a way for the user to filter each series by date.

MSDN provides:

// 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");

This would be fine if I had a single series and I knew the input series name.

The question is how would i implement this for a chart that has multiple series'

Here is a quick work around I did:

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());
    }
}
0

There are 0 best solutions below