本文介绍了MS 图表注释拒绝与鼠标位置对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我为什么会发生以下情况?红线表示注释应该放置的位置(沿 X 轴),但它总是呈现在左边缘......我在谷歌和 SO 中进行了一些搜索,并找到了一个答案暗示 PixelPositionToValue(Mouse.X) 会对它进行排序,但即使使用它,它最终也会完全相同.

Can anyone tell me why the below is happening please? The red line signifies where the annotation should be positioned (along X axis), but it's always rendered right on the left edge... I did a few searches in Google and SO, and found an answer which implies that PixelPositionToValue(Mouse.X) would sort it, but even using this it ends up exactly the same.

Private Sub AssignNewDownTime()
    Dim sStr As String = InputBox("Please enter downtime reason")
    Dim annot As New Charting.RectangleAnnotation()

    annot.ClipToChartArea = "Chart1"
    annot.BackColor = Color.DarkRed
    annot.ForeColor = Color.White
    annot.AllowMoving = True
    annot.AllowAnchorMoving = False
    annot.AllowSelecting = False
    annot.IsMultiline = False
    annot.AllowTextEditing = False
    annot.IsSizeAlwaysRelative = False
    annot.X = Chart1.ChartAreas(0).AxisX.PixelPositionToValue(StartMousePoint.X)
    annot.Y = 10
    annot.Width = 25

    annot.Text = sStr & " /X: " & annot.X & "Y:" & annot.Y

    Chart1.Annotations.Add(annot)
    Chart1.Invalidate()
End Sub

推荐答案

呸!这总是发生......我把头发拉了几个小时,在SO上发帖,5分钟内我就修好了.无论如何,对于可能也会脱发的后代,这是解决方案:

Gah! This always happens... I pull my hair out for hours, post on SO and within 5 minutes I've fixed it. Anyway, for future generations that may also pull their hair out, here's the solution:

注释 X 和 Y 未设置为图表的 X 和 Y,因此图表的范围从 0.0 到 1.0,而注释的默认范围是 0 到 100.噩梦!有几种方法可以解决这个问题,我选择了:

Annotations X and Y aren't set to that of the chart, so whereas the chart will range from 0.0 to 1.0, annotations default range is 0 to 100. Nightmare! There are a couple of ways around this, I chose:

annot.AxisX = Chart1.ChartAreas(0).AxisX

设置注释的 X 轴以模仿图表的 X 轴.因此,值和限制将是正确的.一旦我这样做,它立即起作用.您当然也可以设置注释的 AxisY,但请注意,在绘制图表时 Y 是从下到上的.可以在此处找到更多信息:http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.annotation(v=vs.110).aspx - 特别是(我希望我先阅读...):

Which sets the X axis of the annotation to mimic that of your chart. Thus the values and limits will be correct. As soon as I did this it worked instantly. You can of course set the AxisY of the annotation as well, but beware that in charting the Y is bottom to top. More information can be found here: http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.annotation(v=vs.110).aspx - specifically (which I wish I'd read first...):

注释通常用于评论或详细说明图表元素,例如数据点.注释也可用于绘制自定义形状.默认情况下,注释使用相对坐标定位,(0,0) 表示左上角,(100,100) 表示图表图像的右下角.也可以从这个相对坐标系切换到使用轴值的系统.对于轴坐标系,表示注释左上角位置的 X 和 Y 是使用 X 轴和 Y 轴值设置的,而不是使用 0-100 范围内的值.指定注释的位置和大小时,有两种使用轴值的方法:将 AxisX、AxisY 或这两个注释属性设置为 ChartArea 对象的 AxisX 和 AxisY 属性值.使用 AnchorDataPoint 属性将注释锚定到数据点.在这种情况下,它的定位是自动计算的.所有的注解都派生自 Annotation 类,该类可用于设置所有 Annotation 对象共有的属性,例如颜色、位置、锚点等.

无论如何,希望这对大家有所帮助.

Anyway, hope this helps folk out.

这篇关于MS 图表注释拒绝与鼠标位置对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 08:17