Search code examples
c#chartsmschart

Create chart series, add data and set visibility


I'm trying to create a group of chart series, add data to the series and then show a few of the series by making them visible. The data changes and a user can select which series to view. I think this method will be better than clearing all series with chart.Series.Clear(); and then recreating the series in the same method.

For example a list of cars in a carpool with random mileages and then select which cars to show.

The code below doesn't work (I've commented where). The series aren't public and I think they need to be added to a public collection like a SeriesCollection but I'm not sure how.

Thanks for any help.

// create new chart series and add to a chartarea
        ChartArea TestChartArea = new ChartArea();
        public void CreateChartSeries()
        {
            List<string> lstCars = new List<string> { "Mazda", "Tesla", "Honda", "Jaguar", "Ford", "Toyota" };

            foreach (string Car in lstCars)
            {
                // car series created correctly?
                var Srs = new Series(Car); 
                Srs.ChartArea = TestChart.Name;
                Srs.YAxisType = AxisType.Primary;
                Srs.Color = Color.Red;
                Srs.ChartType = SeriesChartType.Line;
                TestChart.Series.Add(Srs);
            }
        }

        // add data to chart series
        public void SeriesData()
        {
            List<string> lstCars = new List<string> { "Mazda", "Tesla", "Honda", "Jaguar", "Ford", "Toyota" };
            int[] Xseries = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            int[] Milage = new int[10];

            Random Random = new Random();

            foreach (string Car in lstCars)
            {
                for (int M = 0; M < 10; M++)
                    Milage[M] = Random.Next(150, 15000);

                // not sure how to call and add data to each series
                Srs.Points.DataBindXY(Xseries, Milage);  
            }
        }

        // plot series - some visible
        public void PlotCreatedSeries()
        {
            // not sure how to refer to each series
            Mazda.Enabled = true;  
            Tesla.Enabled = false;
            Honda.Enabled = true;
            Jaguar.Enabled = false;
            Ford.Enabled = true;
            Toyota.Enabled = false;
        }

Solution

  • The name 'Srs' you use to create the Series is only in scope i.e. usable within the loop. At the end of the loop you do add the newly created Series to your Chart:

     TestChart.Series.Add(Srs);
    

    The Series property is a public SeriesCollection. This is a bit confusing, as the singular type name and the plural property name are the same in this case, as oppose to, say, Legend(s) or ChartArea(s)..

    From now on you can access it either by its index..

     Series s = TestChart.Series[0]  // the series you have added first
    

    ..or, more readable and more stable, by its Name property:

     Series s = TestChart.Series["Mazda"]   // the same series
    
     TestChart.Series["Mazda"].Enabled = true;  
    

    Note that 'name' is also a tricky word:

    When you declare a variable you give it a 'name'. Series s = new Series();

    But many objects also have a property called Name: s.Name = "Volvo";

    The former must be unique but the latter is just a string; do keep it unique as well, but the system will not guard you.

    The former can never change, but, as you have seen, can go out of scope; the latter is just a string and you can change it.

    Note that the variable itself doesn't go out of scop as long as it is still referenced somewhere, here as an element of the SeriesiesCollection Series..

    Whether you want to add DataPoints bound or directly is up to you.

    For the former there are many binding options.

    For the latter you can use the Chart.Points methods..:

    • Add(DataPoint)
    • AddY(YValue)
    • AddXY(XValues, YValue(s))

    Note that sometimes, especially with live charts it makes sense to insert a DataPoint using one of the InsertXXX methods!

    Do look it up in MSDN! The middle version makes only sense if the x-values are either non-numeric or have to no real meaning, like, say, names.. - Note that adding meaningful x-values as numbers (or DateTimes) is crucial to use them for further goals, like tooltips, zoom or display ranges etc..

    Failing to do so is probably the most common mistake newbies make. The Chart looks ok, but the data inside are broken, read lost.