Question

I'm creating a stacked bar chart using MS Charts and VB. Occasionally, we run into issues where we have some of the stacks showing up negative.

We're currently using a pretty gross formula to set our Y axis maximum...

    If dblMaxTotal >= 15 Then
        dblMaxTotal = System.Math.Round(((dblMaxTotal * 12) / 10) / 10, 0) * 10
    Else
        dblMaxTotal = 15
    End If

    chAutoTotalBindsBreak.ChartAreas("ChartArea1").AxisY.Maximum = dblMaxTotal
    chAutoTotalBindsBreak.ChartAreas("ChartArea1").AxisY2.Maximum = dblMaxTotal

    chAutoTotalBindsBreak.ChartAreas("ChartArea1").AxisY.Interval = dblMaxTotal * 0.2
    chAutoTotalBindsBreak.ChartAreas("ChartArea1").AxisY2.Interval = dblMaxTotal * 0.2

This works fine for what we do, including with the negative numbers.

HOWEVER, I need to ALWAYS display a line for where Y = 0. Whether it's forced in, or set as one of the interval points is irrelevant, just as long as it is there.

Était-ce utile?

La solution

For those curious, here's what we went with.

If dblMaxTotal >= 15 Then
    dblMaxTotal = System.Math.Round(((dblMaxTotal * 12) / 10) / 10, 0) * 10
ElseIf dblMaxTotal > 0 Then
    dblMaxTotal = 15
Else
    dblMaxTotal = 0
End If

chAutoTotalBindsBreak.ChartAreas("ChartArea1").AxisY.Maximum = dblMaxTotal
chAutoTotalBindsBreak.ChartAreas("ChartArea1").AxisY2.Maximum = dblMaxTotal

If dblMaxTotal = 0 And dblMinAmount < 0 Then
    dblMaxTotal = (dblMinTotal) * -1
End If

Dim MinValue As Double = dblMaxTotal * 0.2
If dblMinAmount < 0 Then
    While MinValue < dblMinTotal * -1
        If MinValue < dblMinAmount * -1 Then
            MinValue = MinValue + (dblMaxTotal * 0.2)
        End If
    End While
    chAutoTotalBindsBreak.ChartAreas("ChartArea1").AxisY.Minimum = MinValue * -1
    chAutoTotalBindsBreak.ChartAreas("ChartArea1").AxisY2.Minimum = MinValue * -1
End If

chAutoTotalBindsBreak.ChartAreas("ChartArea1").AxisY.Interval = dblMaxTotal * 0.2
chAutoTotalBindsBreak.ChartAreas("ChartArea1").AxisY2.Interval = dblMaxTotal * 0.2
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top