Search code examples
c#mschart

get label of biggest column charts in c#


I've made a chart for the age range of people. I want to get the label of the largest and smallest column in the chart. And return zero if there were several large or small identical columns.

enter image description here

my code :

Series series = new Series();
series.ChartType = SeriesChartType.Column;
series.Points.AddXY("15-20", age_set1);
series.Points.AddXY("21-25", age_set2);
series.Points.AddXY("26-30", age_set3);
series.Points.AddXY("31-35", age_set4);
series.Points.AddXY("36-45", age_set5);
series.Points.AddXY("46-55", age_set6);
series.Points.AddXY("56-65", age_set7);
chart1.Series.Add(series);

Solution

  • If you mean the AxisLabel you need to find the DataPoint that has the largest value..:

    double max  = series.Points.Max(x => x.YValues[0]);
    DataPoint dp = series.Points.Where(x => x.YValues[0] == max)
                                .First();
    string label = dp.AxisLabel;
    

    Note that there may be several points with this value! If you omit the First() you can get a List<DataPoint> which you can then enumerate..

    if you want to return "0" for that case you could write:

    List<DataPoint> dps = series.Points.Where(x => x.YValues[0] == max)
                                       .ToList();
    string label = dps.Count == 1 ? dps.First().AxisLabel : "0";