简体   繁体   中英

How do you display a data points value on top of the chart

I am using System.Web.UI.DataVisualization.Charting to display a single series of points. The functionality I'm trying to achieve is similar to IsValueShownAsLabel, except I would like the label to appear at the top of the chart, and not next to the data point.

I have tried smart labels, and setting the label style, however these are only changing the position of the label next to the data point, ie, whether it's to the left or the right of the point and not the actual position of the point.

I'm not sure where else to look or if what I'm trying to do is easily attained.

So I found the answer myself after some digging.

The Secondary X-Axis with custom labels is what I was looking for.

chart.ChartAreas[0].AxisX2.Enabled = AxisEnabled.True;

Then I looped through my data points and added custom labels for each value

for(var i = 0; i < chart.Series[0].Points.Count; i++)
        {
            var lowerValue = i + 1 - .5;
            var upperValue = i + 1 + .5;
            var roundedValue = Math.Round(chart.Series[0].Points[i].YValues.First(), 1);

            secondaryXAxis.CustomLabels.Add(lowerValue, upperValue, roundedValue.ToString(CultureInfo.InvariantCulture));
        }

The -.5 and +.5 was recommended to make sure the points fall onto the line that you want. Works like a charm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM