如果MS Chart Control没有数据,我能显示消息吗?

10
有没有办法在MS图表控件中显示“默认”消息,如果没有数据要绘制?
我有一个图表,带有一些控件,允许用户选择各种日期范围。如果在该日期范围内没有要绘制的数据,则当前什么都不显示(或者至少显示图例和背景,但仅此而已。)
我想要有一个消息,例如“此时段无数据”之类的东西。
谢谢, Ben
3个回答

10

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

5

如果没有数据,您应该能够向图表添加注释。

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

0
我猜你将检索到的数据转换为数组并用于图表绑定,如果是这样的话,你可以使用一个标签,根据数组长度来显示/隐藏它,因为没有属性可以在图表没有数据时显示特定的文本。
    if (arr.Length > 0)
    {
        lblEmptyMSG.Visible = false;
    }
    else
    {
        lblEmptyMSG.Visible = true;
    }

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