0

I have a lot of charts i am graphing and for some i need to give each X value a custom color and have that item in the legend as well. The following code achieves this but on the legend there is always the first default value which is now pointless. How would i remove/hide/clear that original default legend item? Just for reference the chartColors array is just an array of colors to cycle through.

mainChart.Legends.Add(new Legend("legend"));
mainChart.Legends["legend"].Docking = Docking.Bottom;

foreach (Series ser in mainChart.Series)
{
    int i = 0;
    foreach (DataPoint point in ser.Points)
    {
        point.Color = chartColors[i % chartColors.Count()];
        mainChart.Legends["legend"].CustomItems.Add(point.Color,point.AxisLabel);
        i++;
    }
}

According to http://msdn.microsoft.com/en-us/library/vstudio/dd456659(v=vs.100).aspx

"Legend items in this collection are always attached to the end of other legend items in the legend. "

so to maybe clarify my question, how do i remove the "other legend items"

edit: I found this answer but it is for winforms and im not sure how to call the function they add

WinForms.Charting suppress autogenerating legend

Community
  • 1
  • 1
RustyH
  • 473
  • 7
  • 22

3 Answers3

0

Okay figured it out posting in case someone else needs to do it, i used the awnser from the posted question as a bases but added the points before the method was called and then only removed the non custom ones

    protected void chartarea1_CustomizeLegend(object sender,System.Web.UI.DataVisualization.Charting.CustomizeLegendEventArgs e)
    {
        int customItems = ((Chart)sender).Legends[0].CustomItems.Count();
        if (customItems>0)
        {
            int numberOfAutoItems = e.LegendItems.Count()-customItems;
            for (int i = 0; i < numberOfAutoItems; i++)
            {
                e.LegendItems.RemoveAt(0);
            }
        }

    }
RustyH
  • 473
  • 7
  • 22
0

for all series at your Chart do next:

series .IsVisibleInLegend = false;
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
0
mainChart.Series[0].IsVisibleInLegend = false;
AbdusSalam
  • 420
  • 6
  • 10
  • While you're identifying the series differently, this is essentially the same answer as @Vasyl-Golubenko submitted three years ago. – Jeremy Caney Jun 04 '20 at 00:24
  • Code only answers are allowed, but it's encouraged to explain the answer as well. Consider adding some explanation. – zonksoft Jun 04 '20 at 16:16