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

发布于 2024-10-07 23:11:01 字数 190 浏览 7 评论 0原文

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

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

我希望有一条消息说“此期间没有数据”或其他东西代替。

谢谢,

Is there a way to display a "default" message on a MS Chart Control if there is no data to chart?

I have a chart, with some controls that allow the user to pick various date ranges. If there is no data to be charted in that date range, it currently just displays nothing (or at least it shows the legend, and background, but that's it.)

I want there to be a message saying "no data for this period" or something instead.

Thanks,

Ben

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

友欢 2024-10-14 23:11:01

基于 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);
    }
}

Building on Chris's response, here's a more complete example:

In the ASPX code, add the OnDataBound handler to the chart tag. This assumes you are using a SqlDataSource for the data source.

<asp:Chart ID="ChartExample" runat="server" 
    DataSourceID="SqlDataSourceExample" 
    OnDataBound="ChartExample_DataBound">

In the code-behind, the handler checks if the first series has any data, and if it doesn't, inserts the annotation in red.

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);
    }
}
无风消散 2024-10-14 23:11:01

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

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

You should be able to add an annotation to the chart if there is no data.

TextAnnotation annotation = new TextAnnotation();
annotation.X = 50;
annotation.Y = 50;
annotation.Text = "No Data";
chart1.Annotations.Add(annotation);
三生池水覆流年 2024-10-14 23:11:01

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

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

I guess that you cast retrieved data to an Array and use it for chart binding, if so
you can use a label, show/hide it according array length, as there is no property to be displayed a certain text if chart has no data.

    if (arr.Length > 0)
    {
        lblEmptyMSG.Visible = false;
    }
    else
    {
        lblEmptyMSG.Visible = true;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文