Question

I'm trying to deploy some charts on my web application, but am having trouble with the StackedColumn chart; the series are either overlapping each other or gapping apart from each other, the result being that the overall total is misrepresented. Here are two examples showing each problem:

http://sdrv.ms/17nzZ2k - gaps between series

http://sdrv.ms/1fg6LqW - overlapping series

This is my code to generate the chart:

Private Sub chartMonMStack_DataBinding(sender As Object, e As System.EventArgs) Handles chartMonMStack.DataBinding

    Dim chart As Chart = chartMonMStack
    Dim cArea As String = "area"

    chart.ChartAreas(cArea).Position.Width = 85
    chart.ChartAreas(cArea).Position.Height = 100
    chart.ChartAreas(cArea).AxisX.MajorGrid.LineColor = Drawing.ColorTranslator.FromHtml("#999999")
    chart.ChartAreas(cArea).AxisY.MajorGrid.LineColor = Drawing.ColorTranslator.FromHtml("#999999")
    chart.ChartAreas(cArea).AxisX.Title = "年"
    chart.ChartAreas(cArea).AxisY.Title = "NTD"
    chart.ChartAreas(cArea).AxisY.LabelStyle.Format = "#,##0"

    Dim conStr As String = ConfigurationManager.ConnectionStrings("GenshenPOS").ConnectionString
    Dim conn As New SqlConnection(conStr)

    conn.Open()

    Dim sql As String = "SELECT [y], [Store], SUM([amount]) AS [NTD] FROM vwMonthly " & _
                        "WHERE [m] = '" & dropMonth.SelectedValue & "月' GROUP BY [y], [Store] ORDER BY [y], [Store]"

    Dim sqlPoints As New SqlCommand(sql, conn)

    Dim reader As SqlDataReader = sqlPoints.ExecuteReader()

    While reader.Read()

        If chart.Series.IndexOf(reader.Item("Store")) = -1 Then

            chart.Series.Add(reader.Item("Store"))
            chart.Legends.Add(reader.Item("Store"))
            chart.Series(reader.Item("Store")).ChartType = SeriesChartType.StackedColumn
            chart.Series(reader.Item("Store")).ChartArea = cArea
            chart.Series(reader.Item("Store")).IsValueShownAsLabel = True
            chart.Series(reader.Item("Store")).LabelFormat = "#,##0"
            chart.Series(reader.Item("Store")).LabelForeColor = Drawing.Color.White

        End If

        chart.Series(reader.Item("Store")).Points.AddXY(reader.Item("y"), reader.Item("NTD"))

    End While

    conn.Close()

End Sub

I've read that the gaps can occur because of missing data if the series has a blank data point, however on the affected column, there are no missing data points. Additionally I don't think this can explain why in other instances, the bars overlap and do not position themselves on the chart correctly. Nevertheless I tried adding the following but to no avail.

For Each s As Series In chart.Series
    chart.DataManipulator.InsertEmptyPoints(1, IntervalType.Number, s)
Next

If anyone can help I'd appreciate it!

Was it helpful?

Solution

It appears that InsertEmptyPoints was the correct method for resolving the problem, however if anyone has issues with this not appearing to do anything as I did, then using additional parameters in the method could resolve your issue, as it did mine.

For Each s As Series In chart.Series
    chart.DataManipulator.InsertEmptyPoints(1, IntervalType.Number, 0, IntervalType.Number, MinXAxis, MaxXAxis, s)
Next

I used a 0 value in the offset section of the method (as I have no need to offset), and instead focused on the 5th (fromXValue) and 6th (toXValue) parameters to specify the range of values along my axis. The variables MinXAxis and MaxXAxis are assigned the values I need dynamically.

This resolves both the gapping and overlap for my series.

For more info on InsertEmptyPoints see msdn, however be advised it currently makes no mention of the fromXValue and toXValue parameters, I found out about them from within Visual Studio.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top