Reputation: 153
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).
Upvotes: 7
Views: 24789
Reputation: 343
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;
}
}
Upvotes: 2
Reputation:
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;
}
}
}
}
Upvotes: 6
Reputation: 21
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
Upvotes: 1
Reputation: 41
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;
}
}
}
Upvotes: 4
Reputation: 919
Jim's solution didn't work for me, but here's how I did it, making use of some of his code - thanks Jim!
Code:
In the ASPX:
<Series>
<asp:Series ChartType="Pie" Name="Series1" ..etc....>
<EmptyPointStyle IsValueShownAsLabel="false" IsVisibleInLegend="false" />
</asp:Series>
</Series>
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
Upvotes: 4
Reputation: 1978
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"
Upvotes: 0
Reputation: 146
Use a custom number format that suppresses zeros, something like
General;;;
0.0%;;;
$#,##0.00;;;
Upvotes: 2