- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我已将注释与数据点对齐;我想垂直对齐注释。
我已阅读此内容,但无法弄清楚如何将图表宽度与数据点相关联。 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
控件中定位相当复杂。
首先,图表具有三个坐标系:
ChartArea
和 InnerPlotPosition
Chart
控件的 ClientArea
实现注释
对齐的最简单方法是首先将每个注释锚定到它的DataPoint
:
MyTextAnnotation.AnchorDataPoint = MyDataPoint
接下来您将 X 位置覆盖为您喜欢的值:
MyTextAnnotation.X = someValue;
一些注意事项:
Annotation.Position
会使用百分比
,但在锚定到之后DataPoint 它使用值 代替。所以使用 50
不会放在中间的某个地方,而是放在右边......查看您的图表,我建议使用值 3
。
顺便说一句:百分比与下一个外部容器有关:ChartAreas
到 Chart.ClientRectangle
,InnerPlotPostion
到 ChartArea
和它停靠到的容器的每个元素..
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/
我是一名优秀的程序员,十分优秀!