如何在堆积条形图中当数值为零时隐藏数据点标签

7
我有一个StackedBar,每个条形图显示5个值,并在每个块的中间显示数据值。到目前为止,一切正常。然而,当值为零时,仍然显示该值,当零很多时会显得混乱。
我想隐藏零的标签。我该如何做?
(我认为我可以通过逐行阅读数据并逐步构建图表来完成这项工作,但我更愿意能够将查询结果直接传递给控件)。
7个回答

6

您可以在“自定义事件”中隐藏标签:

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;
            }
        }
    }
}

4
这对我来说完美无缺。
  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;
        }
    }
  }

4

Jim的解决方案对我没用,但是在他的代码基础上,我找到了解决方法 - 谢谢Jim!

  1. 在设计器中,在相关系列元素下设置一个EmptyPointStyle元素。这应该将值设置为不显示标签,也不在图例中显示。
  2. 在代码后台中,使用DataBound或Customize事件通过将它们的IsEmpty属性设置为True来隐藏零点。

代码:

  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!):

(注:我还需要展开此特定饼图上的所有点,这与本问题无关,但我保留了它,以防对某人有所帮助。)
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

我将这段代码标记了一下,因为在转换为C#时,point.IsEmpty = true是成功的关键。当您需要point.IsValueShownAsLabel = true并且设置Series [chartSeries] .LabelFormat =“mm:ss”时,这是必需的。 - 27k1

2

这个功能正常工作(我只测试了一个系列)

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;
     }
}

2

使用自定义数字格式来抑制零,例如

常规;;;

0.0%;;;

$#,##0.00;;;


1

这对我来说有效

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

0

我曾经遇到过同样的问题,我使用以下数字格式解决了它

[=0]"";0.0%

第一部分:

[=0]""

意思是:如果值等于零,则应显示空字符串。

第二部分:

0.0%

在这种特定情况下,意味着所有其他值应以百分比形式显示,并保留一位小数。第二部分可以使用任何数字格式。
[=0];General (Standard in some localized versions of Excel)

可以用于使用默认格式。

使用VBA,代码如下:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接