バーごとに 5 つの値を表示する StackedBar があり、各ブロックの中央にデータ値が表示されます。ここまでは順調ですね。ただし、値がゼロの場合、値は表示されたままです。これは、ゼロが多数ある場合に厄介です。
ゼロのラベルを非表示にしたいと思います。どうやってやるの?
(データを行ごとに読み取り、グラフを段階的に構築することで、長い道のりを歩むことができると思いますが、クエリ結果をコントロールにスローできるようにしたいと思います)。
Customizeイベントでラベルを非表示にできます。
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;
}
}
}
}
ジムの解決策は私にはうまくいきませんでしたが、彼のコードの一部を利用して、これが私がそれを行った方法です-ジムに感謝します!
コード:
ASPXの場合:
<Series>
<asp:Series ChartType="Pie" Name="Series1" ..etc....>
<EmptyPointStyle IsValueShownAsLabel="false" IsVisibleInLegend="false" />
</asp:Series>
</Series>
背後にあるコード(ここではVBを使用しています!):
(注:この特定の円グラフのすべてのポイントも分解する必要があります。これはこの質問には関係ありませんが、誰かに役立つ場合に備えて、そのままにしておきます。)
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
これは私にとって完璧に機能します
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;
}
}
}
次のようなゼロを抑制するカスタム数値形式を使用します
全般的;;;
0.0%;;;
$#,##0.00;;;
同じ問題があり、次の数値形式を使用して解決しました
[=0]"";0.0%
最初の部分:
[=0]""
つまり、値がゼロに等しい場合、空の文字列を表示する必要があります
2 番目の部分:
0.0%
この特定のケースでは、他のすべての値が小数点以下 1 桁のパーセントとして表示される必要があることを意味します。2 番目の部分として任意の数値形式を使用できます。
[=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"