WinForms图表控件-在刻度之间显示标签 [英] WinForms Chart control - Show labels between ticks

查看:108
本文介绍了WinForms图表控件-在刻度之间显示标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,

解决方案

我认为唯一的方法是使用自定义标签。这意味着要接管添加控件中的标签并自己手动进行的工作,这可能适合您的情况,也可能不合适。



使用自定义标签标记轴部分对此进行了更详细的描述:


使用自定义标签为轴提供自定义文本。自定义标签是使用CustomLabels集合属性实现的
。如果使用
自定义标签,则图表区域不会显示来自数据
点的轴标签。



使用CustomLabel对象时,必须设置每个CustomLabel对象的ToPosition和
FromPosition属性,这些
属性必须指定标签文本区域的宽度。
不要为这两个属性分配相同的值,因为它们
代表一个范围。要将自定义标签直接放置在数据
点及其关联的刻度线之下,请将FromPosition属性设置为
该数据点的轴值减去刻度线
间隔的一半,然后设置将ToPosition属性设置为该数据
点的轴的值加上刻度线间隔的一半。例如,如果
轴的间隔为1(1、2、3,…),并且您想在X = 2上使用自定义
标签,则将ToPosition和FromPosition属性设置为
分别为1.5和2.5。



RowIndex属性指定自定义标签在哪一行显示
。如果在第一个标签行中使用了自定义标签,则图表
区域不会显示轴刻度中的任何标签。在第二行及以后的行中,唯一允许使用的标签
是自定义标签。


在您的情况下,您不希望标签位于刻度线的正下方,因此在上面带引号的文本中的示例中,您将位置ToPosition设置为2,将FromPosition设置为3。



您的特定图形,如果x轴值为DateTimes,则需要检查To / From位置是否适用于DateTimes。如果不是,则可能需要将值转换为序列号(可以使用例如 DateTime.ToOADate() 来执行此操作),然后再将它们用于x轴。这不会影响显示的内容,因为无论如何您都将用自己的标签替换这些标签,然后可以根据需要对其进行格式化。


By default the WinForms Chart control shows labels centrally below the ticks.

Is there any way to change that so that the labels are shown between the ticks instead, as per this image:

解决方案

I think the only way to do this would be to use custom labels. That means taking over the job of adding the labels from the control and doing it manually yourself, which may or may not be suitable in your case.

The "Labeling Axes Using Custom Labels" section of the MSDN documentation describes this in more detail:

Use custom labels to provide custom text for axes. Custom labels are implemented using the CustomLabels collection property. If you use custom labels, the chart area does not display axis labels from data points.

When using CustomLabel objects, you must set the ToPosition and FromPosition properties of each CustomLabel object, and these properties must specify the width of the label's text area. Do not assign the same value to both of these properties because they represent a range. To position a custom label directly beneath a data point and its associated tick mark, set the FromPosition property to the value of that data point's axis minus half of the tick mark interval, and set the ToPosition property to the value of that data point's axis plus half of the tick mark interval. For example, if an axis has an interval of 1 (1, 2, 3,…), and you want to use a custom label at X=2, then set the ToPosition and FromPosition properties to 1.5 and 2.5, respectively.

The RowIndex property specifies on which row the custom label is displayed. If a custom label is used in the first label row, the chart area does not display any labels from the axis scale. The only labels permitted in the second row and beyond are custom labels.

In your case you don't want the labels to be directly below the tick mark, so in the example in the quoted text above you'd set your position ToPosition to 2 and your FromPosition to 3.

Looking at your specific graph, if the x-axis values are DateTimes, you'll need to check if the To/From Positions will work with DateTimes. If not, you may need to convert the values to serial numbers (you could use e.g. DateTime.ToOADate() to do this) before using them for the x-axis. That won't effect what's displayed as you will be replacing those labels with your own ones anyway, which you can then format as you wish.

这篇关于WinForms图表控件-在刻度之间显示标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆