gpt4 book ai didi

c# - 图表注释 : How can I align the annotation along an axis of a datapoint

转载 作者:太空狗 更新时间:2023-10-29 23:21:04 29 4
gpt4 key购买 nike

我已将注释与数据点对齐;我想垂直对齐注释。

我已阅读此内容,但无法弄清楚如何将图表宽度与数据点相关联。 https://msdn.microsoft.com/en-us/library/dd456731.aspx

还有这个:MS Charting Annotations refuse to align to mouse position

我添加了一张图表图片作为示例。

'create new chart
chart1 = New DataVisualization.Charting.Chart

'add chart areas
chart1.ChartAreas.Add("NewChartArea")
chart1.ChartAreas("NewChartArea").Area3DStyle.Enable3D = False
chart1.ChartAreas("NewChartArea").AxisX.MajorGrid.Enabled = False 'turn chart background grid on and off
chart1.ChartAreas("NewChartArea").AxisY.MajorGrid.Enabled = False 'turn chart background grid on and off
chart1.ChartAreas("NewChartArea").AxisX.Title = "Activities of daily living" '----> title on the bottom for the x axis
'[template] chart1.ChartAreas("NewChartArea").AxisX.LabelStyle.Angle = 45
'[template] Chart1.Series(SeriesZeroChartName).Label = " my own label" ' -----> adds a label at the top of each bar

For c As Integer = 2 To Me.DataGridViewResultsAdls.ColumnCount - 1

Dim NextSeriesChartName As String
NextSeriesChartName = DataGridViewResultsAdls.Columns(c).Name

chart1.Series.Add(NextSeriesChartName)
chart1.Series(NextSeriesChartName).ChartType = DataVisualization.Charting.SeriesChartType.Bar 'CHART CHANGER ****** change this value to change chart type
chart1.Series(NextSeriesChartName).Points.Clear()
chart1.Series(NextSeriesChartName).IsValueShownAsLabel = True '----> puts little labels ontop of each bar
chart1.Series(NextSeriesChartName).SmartLabelStyle.Enabled = True


' fill each subsequent series with points
For Count As Integer = 0 To DataGridViewResultsAdls.Rows.Count - 2
Dim NextColumnName As String
NextColumnName = DataGridViewResultsAdls.Columns(c).Name

' define X values
Dim XLabelMyCustom As String
XLabelMyCustom = DataGridViewResultsAdls.Item(0, Count).Value
'define Y values
Dim YLabelMyCustom As String
YLabelMyCustom = DataGridViewResultsAdls.Item(NextColumnName, Count).Value

' add the point to the chart
chart1.Series(NextSeriesChartName).Points.AddXY(XLabelMyCustom, YLabelMyCustom)

' create custom labels for the x axis
chart1.ChartAreas("NewChartArea").AxisX.CustomLabels.Add(Count + 0.5, Count + 0.4 + 0.5, "Q2", 0, DataVisualization.Charting.LabelMarkStyle.None)
chart1.ChartAreas("NewChartArea").AxisX.CustomLabels.Add(Count + 0.5 + 0.5, Count + 0.9 + 0.5, "Q1", 0, DataVisualization.Charting.LabelMarkStyle.None)
chart1.ChartAreas("NewChartArea").AxisX.CustomLabels.Add(Count + 0.5, Count + 1, "Q3", 2, DataVisualization.Charting.LabelMarkStyle.LineSideMark)



'Create a variable MyDataPoint to hold the current datapoint
Dim MyDataPoint As DataPoint
MyDataPoint = chart1.Series(NextSeriesChartName).Points(Count)
'Create a new text annotation
Dim MyTextAnnotation As TextAnnotation
MyTextAnnotation = New TextAnnotation
MyTextAnnotation.Text = "some notation"
'[template] MyTextAnnotation.X = <---- sets coordinates on screen for x
'[template ]MyTextAnnotation.Y = <---- sets coordinates on screen for y
'[template] MyTextAnnotation.AnchorDataPoint = MyDataPoint 'sets the point where the notation will be
'[template] chart1.Annotations.Add(MyTextAnnotation) ' adds the notation to the chart

' only add annotations to the chart once per series
If c = 2 Then
MyTextAnnotation.AxisY = chart1.ChartAreas("NewChartArea").AxisY

' [template] chart1.Series(NextSeriesChartName).Points.Item(Count).ToString <--- output points to a string {x,y}


MyTextAnnotation.AnchorDataPoint = MyDataPoint 'sets the point where the notation will be
chart1.Annotations.Add(MyTextAnnotation) ' adds the notation to the chart
MyTextAnnotation.AnchorOffsetX = -10


End If
Next

Next

'Add chart to control and set dock to fill
Me.PanelChartAdls.Controls.Add(chart1)
chart1.Dock = DockStyle.Fill

End If

最佳答案

Chart 控件中定位相当复杂。

首先,图表具有三个坐标系:

  • 数据值
  • 某些区域的百分比,最显着的是 ChartAreaInnerPlotPosition
  • Chart 控件的 ClientArea
  • 的像素

实现注释对齐的最简单方法是首先将每个注释锚定到它的DataPoint:

MyTextAnnotation.AnchorDataPoint = MyDataPoint 

接下来您将 X 位置覆盖为您喜欢的:

MyTextAnnotation.X = someValue;

一些注意事项:

  • 虽然默认 Annotation.Position 会使用百分比,但在锚定之后DataPoint 它使用 代替。所以使用 50 不会放在中间的某个地方,而是放在右边......

查看您的图表,我建议使用值 3

  • 如果您的值(value)观不同,则此定位也会有所不同。当您调整图表大小时,坐标轴的大小也会调整,注释也会移动。

顺便说一句:百分比与下一个外部容器有关:ChartAreasChart.ClientRectangleInnerPlotPostionChartArea 和它停靠到的容器的每个元素..

  • Positions 默认设置为自动,因此它们的值为 NaN。要访问(当前)值,您可以调用 ElementPosition.ToRectangleF()

  • 另请注意,Chart's Axes 上有几个函数可以转换值、像素和百分比位置。

但是您需要找到一个有效的时刻来调用这些函数;如果 Chart 未完成其布局,它们将返回 null

您可以安全地在三个 Paint 事件之一或响应用户交互(如鼠标事件)中调用它们。

下面是如何使用它们将所有 Annotations 定位到(有点)固定的像素位置:

private void chart1_PostPaint(object sender, ChartPaintEventArgs e)
{
Axis AY = chart1.ChartAreas[0].AxisY;

double pypx = AY.ValueToPosition(AY.PixelPositionToValue(30));

foreach (TextAnnotation ta in chart1.Annotations)
{
ta.X = pypx;
}
}

现在,Annotations 不会在调整图表大小时或数据值增大或缩小时移动。至少不是很多;可以看到一些跳跃。要么是因为我遗漏了什么,要么是因为舍入问题。

但我建议使用更简单的方法将它们的 X 位置设置为轴上的固定值..:

MyTextAnnotation.X = 3;

这会将它们放在您画的黄线上。

关于c# - 图表注释 : How can I align the annotation along an axis of a datapoint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37024717/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com