9

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?

Jeff
  • 2,283
  • 9
  • 32
  • 50

3 Answers3

8

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";
zeFrenchy
  • 6,541
  • 1
  • 27
  • 36
  • 3
    Series are added to CHARTS, not ChartAreas. The third line should be {Chart Object}.Series.Add(Series0). http://msdn.microsoft.com/en-us/library/system.web.ui.datavisualization.charting.chart(v=vs.100).aspx – CigarDoug Mar 06 '14 at 12:38
5

A Chart can be divided into multiple Areas where one area can be a Bar chart other can be Pie chart.

System.Windows.Forms.DataVisualization.Charting.Chart chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart(); System.Windows.Forms.DataVisualization.Charting.ChartArea chartarea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); System.Windows.Forms.DataVisualization.Charting.ChartArea chartarea2 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); chart1.ChartAreas.Clear(); chart1.ChartAreas.Add(chartarea1); chart1.ChartAreas.Add(chartarea2);

Then you create some series; each series will be associated with a chart area. if you create 5 series and associate series1, series2 and series3 to chartarea1 then those series must be same or compatible chart type. otherwise Runtime error will occur. Multiple series In same Chart Area may have different x axis component in some cases. for example in the following code: series1 has 3 data points and series2 has 5, in this case chartarea will show first three x values from series1 and next two x values from series2.

chart1.Series.Clear(); chart1.Series.Add("Series1"); chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column; chart1.Series[0].ChartArea = chart1.ChartAreas[0].Name;

        chart1.Series[0].Points.AddXY("Point1", 20);
        chart1.Series[0].Points.AddXY("Point2", 50);
        chart1.Series[0].Points.AddXY("Point3",30);


        chart1.Series.Add("Series2");
        chart1.Series[1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
        chart1.Series[1].ChartArea = chart1.ChartAreas[0].Name;
        chart1.Series[1].Points.AddXY("newname1", 10);
        chart1.Series[1].Points.AddXY("newname2", 20);
        chart1.Series[1].Points.AddXY("newname3", 30);
        chart1.Series[1].Points.AddXY("newname4", 40);
        chart1.Series[1].Points.AddXY("newname5", 50);

        this.tabPage3.Controls.Add(chart1);
        chart1.Dock = System.Windows.Forms.DockStyle.Fill;

M. Pathan
  • 106
  • 1
  • 7
0

Previous answer breaks width of chart, this example uses elementposition objects, specifically set with to 100% (all nr's are %() This example: "Two Chart Areas, vertically divided 80/20":

ElementPosition ePos = new ElementPosition();
ePos.Width = 100; ePos.Y = 0; ePos.X = 2; ePos.Height = 80;
ElementPosition ePos2 = new ElementPosition();
ePos2.Width = 100; ePos2.Y = 80; ePos2.X = 2;  ePos2.Height = 20;
chartCandleStick.ChartAreas[0].Position = ePos;
chartCandleStick.ChartAreas[1].Position = ePos2;