简体   繁体   English

如何在图表顶部显示数据点值

[英]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. 我正在使用System.Web.UI.DataVisualization.Charting显示单个系列的点。 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. 我试图实现的功能类似于IsValueShownAsLabel,除了我希望标签出现在图表的顶部,而不是在数据点旁边。

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. 我一直在寻找带有自定义标签的辅助X轴。

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. 建议使用-.5和+.5以确保这些点落在所需的线上。 Works like a charm. 奇迹般有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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