如果MS Chart Control没有数据,我可以显示一条消息吗?

Ben*_*Ben 10 asp.net mschart

如果没有图表数据,有没有办法在MS Chart Control上显示"默认"消息?

我有一个图表,有一些控件允许用户选择不同的日期范围.如果在该日期范围内没有要绘制的数据,它目前只显示任何内容(或者至少显示图例和背景,但就是这样.)

我希望有一条消息说"这段时间没有数据"或者其他东西.

谢谢,

hum*_*ads 10

基于克里斯的回应,这是一个更完整的例子:

在ASPX代码中,将OnDataBound处理程序添加到图表标记中.这假设您正在为数据源使用SqlDataSource.

<asp:Chart ID="ChartExample" runat="server" 
    DataSourceID="SqlDataSourceExample" 
    OnDataBound="ChartExample_DataBound">
Run Code Online (Sandbox Code Playgroud)

在代码隐藏中,处理程序检查第一个系列是否有任何数据,如果没有,则将注释插入红色.

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);
    }
}
Run Code Online (Sandbox Code Playgroud)


Chr*_*ken 5

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

TextAnnotation annotation = new TextAnnotation();
annotation.X = 50;
annotation.Y = 50;
annotation.Text = "No Data";
chart1.Annotations.Add(annotation);
Run Code Online (Sandbox Code Playgroud)