【问题标题】:Can I display a message if MS Chart Control has no data?如果 MS Chart Control 没有数据,我可以显示消息吗?
【发布时间】:2010-12-15 17:16:38
【问题描述】:

如果没有要绘制的数据,有没有办法在 MS 图表控件上显示“默认”消息?

我有一个图表,其中包含一些允许用户选择各种日期范围的控件。如果在该日期范围内没有要绘制的数据,则它目前什么都不显示(或者至少它显示了图例和背景,仅此而已。)

我希望有一条消息说“此期间没有数据”或其他内容。

谢谢,

【问题讨论】:

    标签: asp.net mschart


    【解决方案1】:

    根据 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);
        }
    }
    

    【讨论】:

      【解决方案2】:

      如果没有数据,您应该可以在图表中添加注释。

      TextAnnotation annotation = new TextAnnotation();
      annotation.X = 50;
      annotation.Y = 50;
      annotation.Text = "No Data";
      chart1.Annotations.Add(annotation);
      

      【讨论】:

        【解决方案3】:

        我猜您将检索到的数据转换为数组并将其用于图表绑定,如果是这样
        您可以使用标签,根据数组长度显示/隐藏它,因为如果图表没有数据,则没有属性可以显示特定文本。

            if (arr.Length > 0)
            {
                lblEmptyMSG.Visible = false;
            }
            else
            {
                lblEmptyMSG.Visible = true;
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-02-19
          • 2020-11-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-04-05
          • 2023-04-06
          • 2020-03-03
          相关资源
          最近更新 更多