我已将注释与数据点对齐;我想垂直对齐注释。
我已经读过这篇文章,但无法弄清楚如何将图表宽度与数据点联系起来。 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
答案 0 :(得分:4)
在Chart
控件中定位相当复杂。
首先,图表有三个坐标系:
ChartArea
和InnerPlotPosition
Chart
控件的ClientArea
实现Annotations
对齐的最简单方法是首先将每个对齐到DataPoint
:
MyTextAnnotation.AnchorDataPoint = MyDataPoint
接下来,您将X-Position覆盖为您喜欢的值:
MyTextAnnotation.X = someValue;
一些注意事项:
Annotation.Position
会使用percentages
,在锚定之后 DataPoint ,它会使用值代替。所以使用50
不会放在中间的某个地方,而是放在右边...... 查看您的图表我建议使用值3
。
Btw:百分比与下一个外部容器有关: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-Position设置为轴上的固定值..:
MyTextAnnotation.X = 3;
这会把它们放在你画的黄线上。