1

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.

What am I doing wrong ?

Code:

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.

enter image description here

I would like to see that striped line on the bar chart. The striped line should appear on the calculated average of the points.

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.

Here is my code:

Dim chart As New Chart chart.ID = DtDistinct.Rows(I)("CourseSisID")

Dim chartareas As New ChartArea chart.ChartAreas.Add(chartareas)

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: Wrong output

Here is what I need : Notice how the strip line is at a different value for each bar.

Right Output

dee
  • 75
  • 6
  • I just saw StackedBar charts do not support markers. Is there any other way I could highlight a particular point on the chart ? – dee Apr 04 '17 at 19:50
  • Take a look at [THIS ANSWER](http://stackoverflow.com/questions/41880866/c-how-to-crosshatch-a-bar-in-an-asp-net-bar-chart/41886353#41886353) I posted a while ago. If it doesn't help then a picture of what exactly you're trying to do would help. – jsanalytics Apr 05 '17 at 00:31
  • Thank you! I added an image of what I am looking for in the original question. I would like to show a striped line or a marker on the calculated average of a barchart. – dee Apr 05 '17 at 15:27
  • Take a look at the `BoxPlot` chart type. It is the closest thing that would natively support your goal. Other than that, when something is not supported, you need to get creative and hack it...:O) For instance, you could add an extra series to hold the average values and use the series' marker to display the average, like you mentioned before. – jsanalytics Apr 05 '17 at 15:50
  • So, I tried to use annotation because the series markers are not supported. I added the details in the original question - It would be great if someone could point out what I am doing wrong!! Thank you. – dee Apr 10 '17 at 14:27
  • I solved it using annotations and this answer : http://stackoverflow.com/questions/25022053/how-to-draw-verticle-line-on-mschart-that-fills-graph-but-isnt-infinite Here is my final code : – dee Apr 10 '17 at 19:34

1 Answers1

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)
dee
  • 75
  • 6