Asp.net

如果 MS Chart Control 沒有數據,我可以顯示消息嗎?

  • February 3, 2012

如果沒有要繪製的數據,有沒有辦法在 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);
   }
}

引用自:https://stackoverflow.com/questions/4452841