我正在尝试以编程方式在堆积条形图上添加标记,但它们似乎没有显示出来。实际上,我需要在数据点的平均值上显示一个标记但是现在,我甚至无法在图表上显示简单的标记。
我做错了什么?
代码:
Dim chart As New Chart chart.ID = DtDistinct.Rows(I)(" CourseSisID")
Dim chartareas As New ChartArea
chart.ChartAreas.Add(chartareas)
chart.Series.Clear()
chart.DataBindCrossTable(DtRecords.DefaultView, "Outcomescore", "ShortName", "RecordsPerGroup", "")
chart.ChartAreas(0).AxisY.Interval = 1
chart.ChartAreas(0).AxisY.Enabled = AxisEnabled.False
chart.Palette = ChartColorPalette.None
chart.PaletteCustomColors = New Color() {ColorTranslator.FromHtml("#DF5B59"), ColorTranslator.FromHtml("#E0D773 "), ColorTranslator.FromHtml("#8AAC53"), ColorTranslator.FromHtml("#6A843F")}
chart.ChartAreas(0).AxisX.MajorGrid.Enabled = False
chart.ChartAreas(0).AxisY.MajorGrid.Enabled = False
For Each cs As Series In chart.Series
cs.ChartType = SeriesChartType.StackedBar
cs.Points().FindMaxByValue.MarkerColor = Color.AliceBlue
cs.Points().FindMaxByValue.MarkerSize = 13
cs.Points().FindMaxByValue.MarkerStyle = MarkerStyle.Diamond
Next
pnlcharts.Controls.Add(chart)
这是我想要做的事情的图像。我知道上面的代码不会给我下面的图像,但我在尝试找到上面代码的解决方法,然后才意识到标记不能出现在条形图上。
我想在条形图上看到条纹线。条纹线应出现在计算的点平均值上。
因此,我能做的另一种方法是使用注释,因为stackbar图表不支持标记。我写了下面的代码来添加一个注释行,但它仍然没有给我输出我需要的只是因为我不知道如何为图表上的每个Y值做这个。
这是我的代码:
Dim chart As New Chart chart.ID = DtDistinct.Rows(I)(" CourseSisID")
Dim chartareas As New ChartArea chart.ChartAreas.Add(chartareas)
chart.DataBindCrossTable(DtRecords.DefaultView,&#34; OutcomeScore&#34;,&#34; ShortName&#34;,&#34; RecordsPerGroup&#34;,&#34; Label = RecordsPerGroup&#34;)< / p>
chart.Palette = ChartColorPalette.None
chart.PaletteCustomColors = New Color() {ColorTranslator.FromHtml("#DF5B59"), ColorTranslator.FromHtml("#E0D773 "), ColorTranslator.FromHtml("#8AAC53"), ColorTranslator.FromHtml("#6A843F")}
chart.ChartAreas(0).AxisX.MajorGrid.Enabled = False
chart.ChartAreas(0).AxisY.MajorGrid.Enabled = False
Dim charttitle As New Title
charttitle.Text = DtDistinct.Rows(I)("CourseSisID")
chart.Titles.Add(charttitle)
For Each cs As Series In chart.Series
cs.ChartType = SeriesChartType.StackedBar
Next
Dim ann1 As New VerticalLineAnnotation()
ann1.AxisX = chart.ChartAreas(0).AxisX
ann1.AxisY = chart.ChartAreas(0).AxisY
ann1.IsSizeAlwaysRelative = False
ann1.X = chart.DataManipulator.Statistics.Mean(chart.Series(0).Name)
ann1.IsInfinitive = True
ann1.ClipToChartArea = chart.ChartAreas(0).Name
ann1.LineColor = Color.Coral
ann1.LineWidth = 3
ann1.AnchorX = 1
ann1.AnchorY = 5
chart.Annotations.Add(ann1)
ann1.LineDashStyle = ChartDashStyle.Dash
pnlcharts.Controls.Add(chart)
这就是我需要的:注意每个条形的条带线的值是不同的。
答案 0 :(得分:1)
Dim annotation2 As New LineAnnotation()
annotation2.IsSizeAlwaysRelative = False
annotation2.AxisX = chart.ChartAreas(0).AxisX
annotation2.AxisY = chart.ChartAreas(0).AxisY
annotation2.Height = 1
annotation2.Width = 0
annotation2.LineWidth = 1.5
annotation2.StartCap = LineAnchorCapStyle.None
annotation2.EndCap = LineAnchorCapStyle.None
annotation2.AnchorX = AvgVar
annotation2.ToolTip = "Average=" & AvgVar
Select Case row
Case 1
annotation2.AnchorY = 4.5
Case 2
annotation2.AnchorY = 3.5
Case 3
annotation2.AnchorY = 2.5
Case 4
annotation2.AnchorY = 1.5
Case 5
annotation2.AnchorY = 0.5
End Select
' <- your point
annotation2.LineColor = Color.Gray
annotation2.LineDashStyle = ChartDashStyle.Dash
' <- your color
chart.Annotations.Add(annotation2)