blumonde blumonde - 1 year ago 99
C# Question

MS Chart Data Value Labels

I am building a "Stacked Column" chart via MS-Chart using C# in MVC. My chart has 3 serieses. I am trying to get the data value labels of each series to display underneath the X-axis instead of on each column.

I would like my chart's data value labels to look the same as the image below:

enter image description here

I have been searching the net in hoping to find some pointer to this similar lay-out but have found none for 2 days.

Can someone please give me some pointer on how to accomplish this same position of data value labels arrangement? Thank you.

Answer Source

Here is the simplest solution. It isn't nicely styled but takes only a few lines:

var months = new[] { "Jan", "Feb", "Mar", "Apr", "May", 
                     "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

for (int i = 0; i < pointCount; i++)
    double sum = 0;
    string label = "";
    for (int j = seriesCount - 1; j >= 0;  j--)
        sum += chart.Series[j].Points[i].YValues[0];
        label += "\\n" + +chart.Series[j].Points[i].YValues[0] ;
    chart.Series[0].Points[i].AxisLabel = months[i] + "\\n"  + sum + label;

This adds a label string to each DataPoint of the 1st Series. Note that only one such labels can be shown per point; labels from later Series will be ignored.

Use suitable counters and whatever you want for the month part of the label.

enter image description here

For nicer looks, like bold sums or colored backgrounds you will need to do a lot more work.

Note that the numbers in the labels and the series are reversely stacked so the inner loop goes backward.

This will work for any number of series.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download