1
votes

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

1
Which chart api are you using?Daniel A. Thompson
Is this what you mean: using System.Windows.Forms.DataVisualization.Charting;Darth Vader's Son

1 Answers

1
votes

That is how Bar and Pie charts are designed to work.

All ChartTypes except Pie charts display the Series.Names or the SeriesTexts in their Legend.

Only Pie charts, which only have one Series anyway will display the DataPoint.YValues[0].

If you really want to display datapoint data in your Legend you can do that, but of course it will look crowded if you add more than a few data points..

This is an example of how you can add hide the regular Legend and add a new one which displays the data values:

chart1.ApplyPaletteColors();
chart1.Legends[0].Enabled = false;

Legend L2 = new Legend();
chart1.Legends.Add(L2);
L2.Docking = Docking.Right;
foreach (DataPoint dp in yourSeries.Points)
{
    LegendItem LI = new LegendItem(dp.YValues[0].ToString("0.##"), dp.Color, "");
    LI.BorderWidth = 0;
    L2.CustomItems.Add(LI);
}

enter image description here

If you want to you can also add those items to the regular Legend; simply create a reference to it and use the code above:

Legend L1 = chart1.Legends[0];

Note that you can't delete the original items from the original Legend, though!