如何使用 C# 在 WinForms MS Chart 中的范围栏两侧打印标签
如何为范围条形图系列中的每个 y 值添加标签?
大家都知道,为了绘制 rangebartype 系列,我们需要两个 yvalues 作为 yvalue[0] 和 yvalue[1] 。这里我需要为每个 yvalues 添加数据标签(这意味着同时在 yvalue[0] 和 yvalue[1] )。我该如何实现?有人可以建议我吗?拜托!!
范围栏的标签应如下所示(显示在范围栏的两侧)。
Label1 ███████████████ Label2
Label███████████████████ Label
How can i add labels for each and every yvalue in series of a rangebarchart ?
You all know that for plotting rangebartype series ,we need two yvalues as yvalue[0] and yvalue[1] .Here I need to add data labels to each of those yvalues( which means both at yvalue[0] and yvalue[1]).how can i implement that?can anybody suggest me?please!!
The label should look like as below for a rangebar(to be displayed on both sides of a rangebar).
Label1 ███████████████ Label2
Label███████████████████ Label
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MS Chart 中没有内置方法可以执行此操作。然而,有一个丑陋的小解决方法可以给你你想要的显示。
对于要显示的每个数据点(范围栏),您需要创建 2 个彼此重叠的数据点。举个例子:
DataPoint0:X=1 Y=5,10
DataPoint1: X=1 Y=10,5
这两个数据点彼此重叠,只是一个从左到右显示,另一个从右到左显示。
对于每个 DataPoint,在 CustomProperties 下都有 BarLabelStyle 属性。对于两个数据点,将其设置为“外部”。通常,这会将标签显示在范围栏的右侧,但对于 DataPoint1,由于 Y 值相反,标签现在放置在范围栏的左侧。因此,将 DataPoint0 的标签设置为 10(最大值),将 DataPoint1 的标签设置为 5(最小值)。
这看起来就像一个范围条,最小值在左侧,最大值在右侧。
注意:如果范围栏的任一端距离图表边缘太近,MS Chart 以其无限的智慧,将强制标签显示在范围栏内。为了克服这个问题,您实际上可以将第三个数字添加到 DataPoint 对象的 Y 值中。第三个值不会显示,但如果它大于数据系列中的最大 Y 值,它将强制图表重新缩放以适应这个较大的值,因此您的标签不会被强制位于范围条内。您也可以通过设置 ChartArea 的属性来处理此问题。
There is no built-in way to do this in MS Chart. However, there is an ugly little workaround that will give you the display that you want.
For each datapoint (rangebar) that you want to display, you will need to create 2 datapoints that lie on top of one another. As an example:
DataPoint0: X=1 Y=5,10
DataPoint1: X=1 Y=10,5
These two datapoints lie right on top of one another, except one is displayed left-to-right and the other is displayed right-to-left.
For each DataPoint, under CustomProperties, there is the BarLabelStyle property. Set this to 'Outside' for both of the datapoints. Normally, this will display the label to the right of the range bar, but for DataPoint1, with the reversed Y values, the label is now placed to the left of the range bar. So, set your label for DataPoint0 to 10 (max value), and for DataPoint1 set the label to 5 (min value).
This will then look just like one range bar with the min value on the left and the max value on the right.
Caution: If either end of the range bar is too close to the edge of your graph, MS Chart, in its infinite wisdom, will force the label to be displayed inside the range bar. To overcome this, you can actually add a third number to the Y values of the DataPoint object. This third value is not displayed, but if it is larger than the largest Y value in your data series, it will force the chart to rescale to accommodate this larger value, so your labels are not forced inside the range bars. You could probably handle this another way with setting properties of the ChartArea also.