views:

793

answers:

3

I have a StackedBar which shows 5 values per bar, with the data value displayed in the middle of each block. So far, so good. However, when the value is zero, the value is still being displayed, which is messy when there are a lot of zeroes.

I would like to be able to hide the label for a zero. How can I do that?

(I presume I could do it the long way by reading the data row-by-row and building the graph step-by-step, but I would prefer to be able to just throw the query results at the control).

A: 

You can hide labels in the Customize event:

protected void SummaryChart_Customize(object sender, EventArgs e)
{
    //hide label value if zero
    foreach (System.Web.UI.DataVisualization.Charting.Series series in SummaryChart.Series)
    {
        foreach (System.Web.UI.DataVisualization.Charting.DataPoint point in series.Points)
        {
            if (point.YValues.Length > 0 && (double)point.YValues.GetValue(0) == 0)
            {
                point.IsValueShownAsLabel = false;
            }
        }
    }
}
+1  A: 

Use a custom number format that suppresses zeros, something like

General;;;

0.0%;;;

$#,##0.00;;;

Jon Peltier
A: 

I had the same problem and I solved it using the following number format

[=0]"";0.0%

The first part:

[=0]""

means that: if the value is equal to zero it should display an empty string

The second part:

0.0%

In this specific case means that all other values should be displayed as percent with one decimal. Any number format can be used as the second part.

[=0];General (Standard in some localized versions of Excel)

can be used to use default format.

Using VBA it would be:

Dim area as range
'The data area for the chart'
set area = Sheet1.range("A1:B3")
area.NumberFormat = "[=0];General"
Zen