locked
Question on stacked column charts - I get white space in my bars! RRS feed

  • Question

  • I use the chart control to make a "stacked" bat chart, but I'm finding white space in the middle of some of my columns.   Maybe I don't understand how it works.  Suppose I want a bar which is 1/3 red and 2/3 green.   I make 2 series, one of which has the number 1 as its y coordinate, and the other has the number 2.   I would expect that would produce the mixed bar, with no white space between the red and green.   Instead, I sometimes get a white chunk that is at least as large as the green.  (I'm oversimplifying, I also have zeros for some series.

    Am I doing something wrong?

       

    ' this is code for just one bar

    For j = 0 To scv.Length - 1 theSeries = .Series("Column " & scv(j).colindex) theSeries.Points.AddXY(startIndex + i, scv(j).count) Next



    • Edited by Gidmaestro Wednesday, July 26, 2017 10:39 AM
    Wednesday, July 26, 2017 10:38 AM

Answers

  • It appears the gaps occur when there is no X axis data point for one series but there is x for the others.

    In this example you can see at x = 2, series 1 has no x,y value point. But the other two have an x = 2 and y value.

    Imports System.Windows.Forms.DataVisualization.Charting
    
    Public Class Form2
        Private Xvalues0 As List(Of Integer) = New List(Of Integer)({1, 2, 3, 4, 5, 6})
        Private Xvalues1 As List(Of Integer) = New List(Of Integer)({1, 3, 4, 5, 6, 8})
        Private Xvalues2 As List(Of Integer) = New List(Of Integer)({1, 2, 3, 4, 5, 7})
    
        Private Yvalues0 As List(Of Integer) = New List(Of Integer)({1, 3, 2, 3, 1, 3})
        Private Yvalues1 As List(Of Integer) = New List(Of Integer)({2, 0, 5, 1, 1, 2})
        Private Yvalues2 As List(Of Integer) = New List(Of Integer)({1, 2, 1, 2, 1, 2})
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            InitializeChart()
            DrawChart()
    
        End Sub
    
        Private Sub InitializeChart()
            With Chart1
                .Dock = DockStyle.Fill
                .Series.Clear()
                .Series.Add("0")
                .Series.Add("1")
                .Series.Add("2")
                Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.StackedColumn
                Chart1.Series(1).ChartType = DataVisualization.Charting.SeriesChartType.StackedColumn
                Chart1.Series(2).ChartType = DataVisualization.Charting.SeriesChartType.StackedColumn
    
                .Legends.Add("Default")
                .Legends(0).LegendStyle = LegendStyle.Row
                .Legends(0).Docking = Docking.Top
                .Legends(0).Alignment = StringAlignment.Center
            End With
    
            With Chart1.ChartAreas(0)
                .AxisX.Title = "Item"
                .AxisX.MajorGrid.LineColor = Color.Silver
                .AxisX.Maximum = 16
                .AxisX.Minimum = 0.0
    
                .AxisY.Title = "Y"
                .AxisY.MajorGrid.LineColor = Color.Silver
                .AxisY.Minimum = 0.0
                .AxisY.Maximum = 10
            End With
        End Sub
    
        Private Sub DrawChart()
    
            For i As Integer = 0 To 5
                Chart1.Series(0).Points.AddXY(Xvalues0(i), Yvalues0(i))
            Next
    
            For i As Integer = 0 To 5
                Chart1.Series(1).Points.AddXY(Xvalues1(i), Yvalues1(i))
            Next
    
            For i As Integer = 0 To 5
                Chart1.Series(2).Points.AddXY(Xvalues2(i), Yvalues2(i))
            Next
    
        End Sub
    End Class

    If I change the values so now there is a data point for all 3 series at x = 2 there is no gap, even though the y value of one is zero.

    Private Xvalues1 As List(Of Integer) = New List(Of Integer)({1, 2, 3, 4, 5, 8})


    • Edited by tommytwotrain Wednesday, July 26, 2017 11:36 AM
    • Marked as answer by Gidmaestro Wednesday, July 26, 2017 12:23 PM
    Wednesday, July 26, 2017 11:29 AM