Search code examples
c#asp.net-mvc-4pie-chart

Show default message if Pie Chart on MVC 4 not build


I am displaying a Pie chart on MVC 4. Its working fine when data is available.

But in case data is not found in a specific scenario, I want to show a default message like "Chart could not be generated".

Currently I am rendering Pie chart on a partial view.

Given below is my code.

Controller Action:

 public ActionResult PieChart(long dateValue, string regType)
        {
            List<PatchingFailurePieChart> lstRegType = BAL.GetFailureDetailsChart(CreatedOn, true, regType);

            if (lstRegType.Count > 0)
            {
                ArrayList xValue = new ArrayList();
                ArrayList yValue = new ArrayList();
                lstRegType.ToList().ForEach(rs => xValue.Add(rs.OSName));
                lstRegType.ToList().ForEach(rs => yValue.Add(rs.Count));

                new Chart(width: 750, height: 590, theme: ChartTheme.Blue)
                    .AddTitle("Pie chart as on date " + CreatedOn.ToString("MM/dd/yyyy"))
                    .AddLegend("OS Versions")
                    .AddSeries("Default", chartType: "Pie", xValue: xValue, yValues: yValue)
                    .Write("bmp");
            }
            ViewBag.dateValue = dateValue;
            ViewBag.regType = regType;
            return null;
        }

Partial View:

  <img  src="@Url.Action("PieChart", "PatchFailure", new { dateValue = @ViewBag.dateValue, regType = @ViewBag.regType })"/>

View: Which is calling Partial view

  @Html.Partial("PieChart");

Thanks in advance for any help.


Solution

  • I made it but it's not perfect. The data are read twice: when html page is opened and when chart is generated. Ideally would be to pass chart data to Url.Action call but probably it's not possible(?).

    Use urls for testing: /ChartTest?dataExists=false /ChartTest?dataExists=true

    Controller:

    public class ChartTestController : Controller
        {
            /// <summary>
            /// Action method for main page.
            /// GET: /ChartTest/
            /// </summary>
            /// <param name="dataExists">It sets if chard should be displayed or "No data found text is displayed</param>
            /// <returns></returns>
            //
            public ActionResult Index(bool dataExists)
            {
                ChartViewModel model = null;
    
                if (dataExists)
                {
                    model = new ChartViewModel
                    {
                        Chart = GetChart()
                    };
                }
                else
                {
                    model = null;
                }
    
                return View(model);
            }
    
            /// <summary>
            /// it prepares Chart object
            /// </summary>
            /// <returns></returns>
            private Chart GetChart()
            {
                return new Chart(600, 400, ChartTheme.Blue)
                    .AddTitle("Number of website readers")
                    .AddLegend()
                    .AddSeries(
                        name: "WebSite",
                        chartType: "Pie",
                        xValue: new[] { "Digg", "DZone", "DotNetKicks", "StumbleUpon" },
                        yValues: new[] { "150000", "180000", "120000", "250000" });
    
            }
    
            /* usage: 
            * http://localhost:54087/ChartTest/GetChart?dateValue=10&regType=aaa&dataExists=false
            * http://localhost:54087/ChartTest/GetChart?dateValue=10&regType=aaa&dataExists=true
            */
            public ActionResult GetChart(long dateValue, string regType)
            {            
                var result = GetChart();
                result.Write("jpg");
                return null;
            }
        }
    

    Class ChartViewModel:

     public class ChartViewModel
        {
            public Chart Chart { get; set; }
        }
    

    Index.cshtml:

    @model codeFirstSample.Models.ChartViewModel
    @{
    
        ViewBag.Title = "ChartTest";
    }
    
    <h2>ChartTest usage example</h2>
    
    
    Chart :
    
    @if (@Model == null)
    {
        <p>No data found.</p>
    }
    else
    {
        <img src="@Url.Action("GetChart", "ChartTest", new { dateValue = 1, regType = 2})" />
    
    }