Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 761
Ben Avatar asked Dec 15 '10 17:12

Ben


People also ask

How chart control is used in asp net?

The ASP.NET Chart Control provides flexible data binding options that allow you to bind to a table from a database or a collection created in code. The control also includes a built-in chart wizard that is invoked at design time, to assist in chart configuration.

How can we add series to MVC charts syntax?

You can add multiple series to the chart by using Series property. The series are rendered in the order as it is added to the series array.

What is net chart?

NET CHARTS. SciChart is a Realtime, High Performance . NET Charting Library, compatible with WPF and Windows Forms, made with Financial, Medical & Scientific Apps in mind. Perfect for Projects that Need Extreme Performance and Slick Interactive .

Which namespace is used to create a chart using ASP NET MVC?

Chart class, which combines the namespace ( System. Web. Helpers ) with the class name ( Chart ).


2 Answers

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);
    }
}
like image 68
humbads Avatar answered Oct 26 '22 21:10

humbads


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);
like image 29
Chris Kooken Avatar answered Oct 26 '22 22:10

Chris Kooken