chart1.Titles.Add("Line Chart");
chart1.ChartAreas[0].Axes[0].MajorGrid.Enabled = false;
chart1.ChartAreas[0].Axes[1].MajorGrid.Enabled = false;
this.dataGridView1.AllowUserToAddRows = false;
List<string> brand = new List<string>();
List<int> price = new List<int>();
List<int> yrsava = new List<int>();
// Traverse the DataGridView to get the corresponding value
for (int row = 0; row <= dataGridView1.Rows.Count - 1; row++)
{
brand.Add(dataGridView1.Rows[row].Cells[0].Value == null ? "" : dataGridView1.Rows[row].Cells[0].Value.ToString());
price.Add(dataGridView1.Rows[row].Cells[1].Value == null ? 0 : (int)dataGridView1.Rows[row].Cells[1].Value);
yrsava.Add(dataGridView1.Rows[row].Cells[2].Value == null ? 0 : (int)dataGridView1.Rows[row].Cells[2].Value);
}
// Clear the exist Series and add new series
chart1.Series.Clear();
chart1.Series.Add("Price");
chart1.Series.Add("Yrs ava");
// Add data to chart
chart1.Series["Price"].Points.DataBindXY(brand, price);
chart1.Series["Yrs ava"].Points.DataBindXY(brand, yrsava);
// Set chart type, here set it to "Bar"
chart1.Series[0].ChartType = SeriesChartType.Bar;
chart1.Series[1].ChartType = SeriesChartType.Bar;
// Draw StackedBar
// Set Xaxis
List<string> departments = new List<string>() { "A", "B", "C" };
// Add series
List<string> months = new List<string>() { "Jan", "Feb", "Mar", "Apr" };
for (int i = 0; i < months.Count; i++)
{
Series series = new Series();
series.Name = months[i];
series.ChartType = SeriesChartType.StackedBar;
this.chart1.Series.Add(series);
}
// Bind data
int[][] scores = new int[3][] { new int[] { 1, 2, 3, 4 }, new int[] { 3, 3, 3, 4 }, new int[] { 5, 2, 3, 4 } };
for (int i = 0; i < months.Count; i++)
{
for (int j = 0; j < scores.Length; j++)
this.chart1.Series[i].Points.AddXY(departments[j], scores[j][i]);
}
// Two AxisY
chart1.Series.Add("Y1");
chart1.Series.Add("Y2");
// Show the Axis title
chart1.ChartAreas[0].AxisX.Title = "X";
chart1.ChartAreas[0].AxisY.Title = "Y1";
chart1.ChartAreas[0].AxisY2.Title = "Y2";
chart1.Series["Y1"].Points.DataBindXY(X, Y1);
chart1.Series["Y2"].Points.DataBindXY(X, Y2)
chart1.Series[0].ChartType = SeriesChartType.StackedBar;
chart1.Series[1].ChartType = SeriesChartType.StackedBar;
chart1.Series[0].YAxisType = AxisType.Primary;
chart1.Series[1].YAxisType = AxisType.Secondary;
chart1.ChartAreas[0].AxisY2.LineColor = Color.Transparent;
chart1.ChartAreas[0].AxisY2.MajorGrid.Enabled = false;
chart1.ChartAreas[0].AxisY2.Enabled = AxisEnabled.True;
chart1.ChartAreas[0].AxisY2.IsStartedFromZero = chart1.ChartAreas[0].AxisY.IsStartedFromZero;