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
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:
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
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...):
Annotations are commonly used to comment or elaborate on chart elements such as data points. Annotations can also be used to draw custom shapes. By default, annotations are positioned using relative coordinates, with (0,0) representing the top-left corner, and (100,100) representing the bottom-right corner of the chart image. It is also possible to switch from this relative coordinate system to a system that uses axis values. With an axis coordinate system, X and Y, which represent the position of the top-left corner of an annotation, are set using X axis and Y axis values, instead of values that range from 0-100. There are two ways to use axis values when you specify the position and size of an annotation: Set the AxisX, AxisY or both of these annotation properties to the AxisX and AxisY property values of a ChartArea object. Use the AnchorDataPoint property to anchor the annotation to a data point. In this case, its positioning is automatically calculated. All annotations are derived from the Annotation class, which can be used to set attributes common to all Annotation objects, such as color, position, anchoring and so forth.
Anyway, hope this helps folk out.