Can I display a message if MS Chart Control has no data?

前端 未结 3 1822
情深已故
情深已故 2021-02-20 18:00

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 variou

3条回答
  •  盖世英雄少女心
    2021-02-20 18:38

    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.

    
    

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

提交回复
热议问题