locked
Chart Controls Setting Legend Text Color to Series Color RRS feed

  • Question

  • I am trying to set the color of the legend items color to the color of their corresponding series color. I am trying to this within the designer as I will be using the chart several times.

    Thanks

    Tuesday, July 5, 2016 5:34 PM

Answers

  • Hi sm56472,

    >>"I am trying to set the color of the legend items color to the color of their corresponding series color"

    We don't need to set the color of legend. When we add a series to chart, we will create a legend which associate it. The legend will display the same color as the corresponding series. Code below is for your reference.

    var series1 = new System.Windows.Forms.DataVisualization.Charting.Series();
    series1.Color = Color.Blue;
    series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Bar;
    series1.YValueMembers = "English";
    series1.IsVisibleInLegend = true;
    series1.Legend = "legend1";
    series1.LegendText = "English";
    series1.XValueMember = "Name";
    
    var series2 = new System.Windows.Forms.DataVisualization.Charting.Series();
    series2.Color = Color.Green;
    series2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Bar;
    series2.YValueMembers = "Math";
    series2.IsVisibleInLegend = true;
    series2.Legend = "legend2";
    series2.LegendText = "Math";
    
    chart1.Series.Add(series1);
    chart1.Series.Add(series2);
    
    chart1.Legends.Add("legend1");
    chart1.Legends.Add("legend2");

    Bind source.

    public class Score
    {
        public int English { get; set; }
    
        public int Math { get; set; }
    
        public string Name { get; set; }
    }
    List<Score> scores = new List<Score>();
    scores.Add(new Score { English = 85, Math = 83, Name = "John" });
    scores.Add(new Score { English = 45, Math = 22, Name = "Jerry" });
    scores.Add(new Score { English = 43, Math = 45, Name = "Lily" });
    scores.Add(new Score { English = 75, Math = 12, Name = "Andy" });
    chart1.DataSource = scores;
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, July 6, 2016 5:41 AM

All replies

  • Hi sm56472,

    >>"I am trying to set the color of the legend items color to the color of their corresponding series color"

    We don't need to set the color of legend. When we add a series to chart, we will create a legend which associate it. The legend will display the same color as the corresponding series. Code below is for your reference.

    var series1 = new System.Windows.Forms.DataVisualization.Charting.Series();
    series1.Color = Color.Blue;
    series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Bar;
    series1.YValueMembers = "English";
    series1.IsVisibleInLegend = true;
    series1.Legend = "legend1";
    series1.LegendText = "English";
    series1.XValueMember = "Name";
    
    var series2 = new System.Windows.Forms.DataVisualization.Charting.Series();
    series2.Color = Color.Green;
    series2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Bar;
    series2.YValueMembers = "Math";
    series2.IsVisibleInLegend = true;
    series2.Legend = "legend2";
    series2.LegendText = "Math";
    
    chart1.Series.Add(series1);
    chart1.Series.Add(series2);
    
    chart1.Legends.Add("legend1");
    chart1.Legends.Add("legend2");

    Bind source.

    public class Score
    {
        public int English { get; set; }
    
        public int Math { get; set; }
    
        public string Name { get; set; }
    }
    List<Score> scores = new List<Score>();
    scores.Add(new Score { English = 85, Math = 83, Name = "John" });
    scores.Add(new Score { English = 45, Math = 22, Name = "Jerry" });
    scores.Add(new Score { English = 43, Math = 45, Name = "Lily" });
    scores.Add(new Score { English = 75, Math = 12, Name = "Andy" });
    chart1.DataSource = scores;
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, July 6, 2016 5:41 AM
  • Li, thanks for your response but I wanted to know if all of this can be set through the designer and not by code. Also, to clarify my question, lets say I have 2 series, one orange and one green, and in my legend I have the series color symbol and the name of the series. Instead of having both, I just want the name text to be the color of the series. Thanks
    Wednesday, July 6, 2016 2:05 PM
  • Hi am56472,

    >>"and in my legend I have the series color symbol and the name of the series."

    If you want to set through the designer, you only need to add two legends according property window. We don't need to set value for any properties of legends.

    Then we could choose legend for series on Series page and set the value for LegendText.

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, July 8, 2016 8:00 AM