How to hide datapoint label when value is zero in a StackedBar

Little JB picture Little JB · Mar 20, 2009 · Viewed 23k times · Source

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).

Answer

Jim picture Jim · Jul 8, 2009

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;
            }
            else
            {
                point.IsValueShownAsLabel = true;
            }
        }
    }
}