ASP.NET Stackedbar图表标记 [英] ASP.NET Stackedbar chart markers
问题描述
我正在尝试以编程方式在堆积的条形图上添加标记,但它们似乎没有显示。实际上,我需要在数据点的平均值上显示一个标记,但是现在,我什至无法在图表上显示一个简单的标记。
I am trying to add markers on my stacked bar chart programmatically and they do not seem to show up. In reality, I need to show a marker on the average of the datapoint but right now, I cannot even show a simple marker on the chart.
我在做什么错了?
代码:
将昏暗的图表作为新图表
chart.ID = DtDistinct.Rows(I)( CourseSisID)
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)
这是我正在尝试做的图像。我知道上面的代码不会给我下面的图片,但是在我意识到标记不能出现在条形图上之前,我试图用上面的代码找到一种解决方法。
Here is an image of what I am trying to do. I know that the above code will not give me the below image but I was trying to find a workaround with the above code, before I realized that markers cannot appear on bar charts.
我希望看到该条纹在条形图上。条纹应出现在计算出的点的平均值上。
I would like to see that striped line on the bar chart. The striped line should appear on the calculated average of the points.
因此,我可以做的另一种方法是使用注释,因为堆叠条形图不支持标记。我编写了以下代码以添加注释行,但是它仍然不能提供所需的输出,这仅仅是因为我不知道如何对图表上的每个Y值执行此操作。
So, an another way I could do was to use annotations since markers are not supported by stackedbar charts. I wrote the following code to add an annotation line, but it still does not give me the output I need only because I do not know how to do it for each Y value on the chart.
这是我的代码:
暗图作为新图
图。ID= DtDistinct。 Rows(I)( CourseSisID)
Dim chart As New Chart chart.ID = DtDistinct.Rows(I)("CourseSisID")
将昏暗的图表区域作为新的ChartArea
图表。ChartAreas.Add(chartareas)
Dim chartareas As New ChartArea chart.ChartAreas.Add(chartareas)
chart.DataBindCrossTable(DtRecords.DefaultView, OutcomeScore, ShortName, RecordsPerGroup, Label = RecordsPerGroup)
chart.DataBindCrossTable(DtRecords.DefaultView, "OutcomeScore", "ShortName", "RecordsPerGroup", "Label=RecordsPerGroup")
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)
上面的代码就是我得到的:
Here is what I get with the above code:
这就是我所需要的:请注意带状线对于每个条形如何具有不同的值。
Here is what I need : Notice how the strip line is at a different value for each bar.
推荐答案
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)
这篇关于ASP.NET Stackedbar图表标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!