图表可以分为多个区域,一个区域可以是柱状图,另一个可以是饼图。
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);
然后创建一些系列,每个系列将与一个图表区域相关联。如果您创建了5个系列并将series1、series2和series3关联到chartarea1,则这些系列必须是相同或兼容的图表类型,否则会出现运行时错误。在同一图表区域中,多个系列在某些情况下可能具有不同的x轴分量。例如,在下面的代码中,series1有3个数据点,series2有5个,此时chartarea将显示来自series1的前三个x值和来自series2的下两个x值。
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;