7

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

Little JB
  • 153
  • 2
  • 2
  • 8

7 Answers7

6

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;
            }
        }
    }
}
Abu Muhammad
  • 421
  • 2
  • 13
4

This work perfectly for me

  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.LegendText = point.AxisLabel;//In case you have legend
                point.AxisLabel = string.Empty;
                point.Label = string.Empty;
        }
    }
  }
Matelin
  • 41
  • 3
4

Jim's solution didn't work for me, but here's how I did it, making use of some of his code - thanks Jim!

  1. In the Designer, set up an EmptyPointStyle element under the relevant Series element. This should set the value to not be shown as a label, and not in the legend.
  2. In the code-behind, use the DataBound or Customize event to hide the zero points by setting their IsEmpty property to True.

Code:

  1. In the ASPX:

      <Series>
            <asp:Series ChartType="Pie" Name="Series1" ..etc....>
                <EmptyPointStyle IsValueShownAsLabel="false" IsVisibleInLegend="false" />              
            </asp:Series>
      </Series>
    
  2. In the Code behind (yep using VB here!):

(Note: I have to explode all points on this particular Pie chart as well, which is not relevant to this question, but I left it in, in case it helps someone.)

Protected Sub Chart1_DataBound(sender As Object, e As EventArgs) Handles Chart1.DataBound
    Dim chart As Chart = TryCast(sender, Chart)

    If chart IsNot Nothing Then
        ' Explode all points
        For Each p As DataPoint In chart.Series(0).Points
            p.CustomProperties = "Exploded=true"

            ' Remove zero points
            If p.YValues.Length > 0 AndAlso p.YValues.GetValue(0) = 0 Then
                p.IsEmpty = True
            End If
        Next
    End If
End Sub
Joe Niland
  • 885
  • 1
  • 14
  • 31
  • I have marked this up because converted to C# point.IsEmpty = true is the key to success, this is required when you need point.IsValueShownAsLabel = true and then setting Series[chartSeries].LabelFormat = "mm:ss"; – 27k1 Nov 08 '13 at 12:18
2

Use a custom number format that suppresses zeros, something like

General;;;

0.0%;;;

$#,##0.00;;;

Jon Peltier
  • 146
  • 1
  • 1
2

This worked correctly(I tested it for only one series)

foreach (System.Windows.Forms.DataVisualization.Charting.DataPoint point in chartShow.Series["S3"].Points)
{
     if (point.YValues.Length > 0 && (double)point.YValues.GetValue(0) == 0)
     {
          point.IsEmpty = true;
     }
     else
     {
          point.IsEmpty = false;
     }
}
Mohsen
  • 323
  • 1
  • 6
  • 15
1

this is working for me

For Each s As Series In Chart1.Series
    For Each dp As DataPoint In s.Points
        If dp.YValues(0) = 0 Then
            dp.IsEmpty = True
        End If
    Next
Next
JSK NS
  • 3,346
  • 2
  • 25
  • 42
Om Prakash
  • 21
  • 1
0

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
  • 1,928
  • 3
  • 12
  • 17