How to show "No Data" when chart does not have anything to display

338 Views Asked by At

I want to display a "no data" label on the View when the chart returns nothing. I'm using ChartDirector, and this is a MVC project.

      [HttpGet]
    public ActionResult Sales()
    {            
        SALESChart1(ViewBag.Viewer = new RazorChartViewer(HttpContext, "chart1"));
        return View();
    }

The chart code:

    private void SALESChart1(RazorChartViewer viewer)
    {           
        
        string sQuery =   query goes here

        // The data for the bar chart
        double[] data = myDBTabletoUse.getCol(0);
        double[] data2 = myDBTabletoUse.getCol(1);

        string[] labels = myDBTabletoUse.getColAsString(2);

        chart stuff goes here

        // Output the chart
        viewer.Image = c.makeWebImage(Chart.PNG);

        // Include tool tip for the chart
        viewer.ImageMap = c.getHTMLImageMap("", "", "title='{xLabel}: {value} HL'");
   }

And view to display chart

                        <th colspan="2">
                            @Html.Raw(ViewBag.Viewer.RenderHTML())
                        </th>
                        
                
1

There are 1 best solutions below

0
On

May be you can create an empty chart with the text "No Data" at the center. It is like:

XYChart c = null;

// Assume data.Length == 0 means no data
if (data.Length == 0)
{
   c = new XYChart(600, 400);
   c.addTitle2(Chart.Center, "No Data", "Arial Bold", 15);       
}
else
{
   c = new XYChart(......);
   ... draw chart as usual ...
}

// Output the chart
viewer.Image = c.makeWebImage(Chart.PNG);