Creating multiple charts and the relation between Chart, Series, ChartArea

Jeff picture Jeff · Jun 25, 2013 · Viewed 21.7k times · Source

I have been generating charts using MSChart for some time now, but I have never created multiple charts within one chart object. Thinking about this task has revealed a gap in my knowledge.

How I think about creating a chart

  1. Create Chart object
  2. Add ChartArea object to Chart object
  3. Create Series and add data
  4. Add Series to Chart

The object structure ends up looking like this

                 Chart
               /       \
          ChartArea   Series

As far as I have been concerned in the past, ChartArea is simply the area I set up the labels and that sort of thing. To add another, I will be wanting to add another ChartArea and one or more series.

           ___________________ Chart ___________________
          /                  /       \                  \
       ChartArea0      ChartArea1   Series0            Series1

How do I associate Series0 to ChartArea0? It would make sense to add a Series to a ChartArea, but that is not possible. For what reason is it beneficial to associate a Series with a Chart, rather than a ChartArea?

Answer

zeFrenchy picture zeFrenchy · Jun 27, 2013

Series are associated with chart areas like so

Chart Chart0 = new Chart();
ChartArea ChartArea0 = new ChartArea("name");
Chart0.ChartAreas.Add(ChartArea0);
Series Series0 = new Series();
Chart0.Series.Add(Series0);
// link series to area here
Series0.ChartArea = "name";