I have a Windows Forms Application that displays a graph from data stored in a database. I am able to get the data to display in a bar graph or pie graph. But the legend in the bar graph only displays "Series1", the name of the series. The legend of the pie chart displays a correct legend with the series data. I've searched MSDN and found several articles on adding a legend but they all have the same results.
Here is my bar chart code:
string[] xvals = new string[dt.Rows.Count];
int[] yvals = new int[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
xvals[i] = dt.Rows[i]["XValues"].ToString();
yvals[i] = Convert.ToInt32(dt.Rows[i]["YValues"].ToString());
}
Chart barChart = new Chart();
ChartArea chartArea = new ChartArea();
barChart.ChartAreas.Add(chartArea);
barChart.Dock = DockStyle.Fill;
barChart.BackColor = Color.Transparent;
barChart.Palette = ChartColorPalette.Fire;
barChart.ChartAreas[0].BackColor = Color.Transparent;
barChart.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
barChart.ChartAreas[0].AxisY.MajorGrid.Enabled = false;
Series series1 = new Series
{ Name = "Series1", IsVisibleInLegend = true, ChartType = SeriesChartType.Bar };
series1.ChartType = SeriesChartType.Column;
barChart.Series.Add(series1);
for (int i = 0; i < dt.Rows.Count; i++)
{
series1.Points.AddXY(dt.Rows[i]["XValues"].ToString(),
Convert.ToInt32(dt.Rows[i]["YValues"].ToString()));
var p1 = series1.Points[i];
p1.Color = Color.FromArgb((byte)r.Next(90, 255), (byte)r.Next(90, 255), 160);
}
barChart.Legends.Add(new Legend("Legend1"));
barChart.Legends["Legend1"].BackColor = Color.Transparent;
barChart.Series["Series1"].Legend = "Legend1";
series1.IsVisibleInLegend = true;
gbo1.Controls.Add(barChart);
Here is my pie chart code:
string[] xvals = new string[dt.Rows.Count];
int[] yvals = new int[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
xvals[i] = dt.Rows[i]["XValues"].ToString();
yvals[i] = Convert.ToInt32(dt.Rows[i]["YValues"].ToString());
}
Chart pieChart = new Chart();
ChartArea chartArea = new ChartArea();
chartArea.Name = "PieChartArea";
pieChart.ChartAreas.Add(chartArea);
pieChart.Dock = DockStyle.Fill;
pieChart.Location = new Point(0, 50);
pieChart.Palette = ChartColorPalette.Fire;
pieChart.BackColor = Color.Transparent;
pieChart.ChartAreas[0].BackColor = Color.Transparent;
Series series2 = new Series
{ Name = "Series2", IsVisibleInLegend = true, ChartType = SeriesChartType.Pie };
pieChart.Series.Add(series2);
for (int i = 0; i < dt.Rows.Count; i++)
{
series2.Points.Add((int)dt.Rows[i]["YValues"]);
var p2 = series2.Points[i];
p2.Color = Color.FromArgb((byte)r.Next(90, 255), (byte)r.Next(90, 255), 160);
p2.LegendText = dt.Rows[i]["XValues"].ToString();
}
pieChart.Legends.Add(new Legend("Legend2"));
pieChart.Legends["Legend2"].BackColor = Color.Transparent;
pieChart.Series["Series2"].Legend = "Legend2";
series2.IsVisibleInLegend = true;
gboReport1.Controls.Add(pieChart);
What am I missing? Please help.
Here is the output of the bar chart: Bar Chart with bad Legend
Here is the output of the pie chart: Pie Chart with good Legend