チャート化するデータがない場合、MS チャート コントロールに「デフォルト」メッセージを表示する方法はありますか?
ユーザーがさまざまな日付範囲を選択できるようにするいくつかのコントロールを備えたグラフがあります。その日付範囲にグラフ化するデータがない場合、現在は何も表示されません (または、少なくとも凡例と背景が表示されますが、それだけです)。
代わりに「この期間のデータがありません」などのメッセージが欲しいです。
ありがとう、
ベン
Chris の応答に基づいて、より完全な例を次に示します。
ASPX コードで、OnDataBound ハンドラーをチャート タグに追加します。これは、データ ソースに SqlDataSource を使用していることを前提としています。
<asp:Chart ID="ChartExample" runat="server"
DataSourceID="SqlDataSourceExample"
OnDataBound="ChartExample_DataBound">
コード ビハインドでは、ハンドラーは最初のシリーズにデータがあるかどうかを確認し、ない場合は注釈を赤で挿入します。
protected void ChartExample_DataBound(object sender, EventArgs e)
{
// If there is no data in the series, show a text annotation
if(ChartExample.Series[0].Points.Count == 0)
{
System.Web.UI.DataVisualization.Charting.TextAnnotation annotation =
new System.Web.UI.DataVisualization.Charting.TextAnnotation();
annotation.Text = "No data for this period";
annotation.X = 5;
annotation.Y = 5;
annotation.Font = new System.Drawing.Font("Arial", 12);
annotation.ForeColor = System.Drawing.Color.Red;
ChartExample.Annotations.Add(annotation);
}
}
データがない場合は、チャートに注釈を追加できるはずです。
TextAnnotation annotation = new TextAnnotation();
annotation.X = 50;
annotation.Y = 50;
annotation.Text = "No Data";
chart1.Annotations.Add(annotation);
取得したデータを配列にキャストし、それをチャートバインディングに使用すると思います。その場合
、ラベルを使用して、配列の長さに応じて表示/非表示にできます。チャートにデータがない場合、特定のテキストを表示するプロパティがないためです。
if (arr.Length > 0)
{
lblEmptyMSG.Visible = false;
}
else
{
lblEmptyMSG.Visible = true;
}