Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果MVC4上的饼图未生成,则显示默认消息_C#_Asp.net Mvc 4_Pie Chart - Fatal编程技术网

C# 如果MVC4上的饼图未生成,则显示默认消息

C# 如果MVC4上的饼图未生成,则显示默认消息,c#,asp.net-mvc-4,pie-chart,C#,Asp.net Mvc 4,Pie Chart,我正在MVC4上显示一个饼图。当数据可用时,其工作正常 但如果在特定场景中找不到数据,我想显示一条默认消息,如“无法生成图表” 目前我正在局部视图上绘制饼图 下面是我的代码 控制器操作: public ActionResult PieChart(long dateValue, string regType) { List<PatchingFailurePieChart> lstRegType = BAL.GetFailureDetailsCh

我正在MVC4上显示一个饼图。当数据可用时,其工作正常

但如果在特定场景中找不到数据,我想显示一条默认消息,如“无法生成图表”

目前我正在局部视图上绘制饼图

下面是我的代码

控制器操作:

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

提前感谢您的帮助。

我做了,但并不完美。数据被读取两次:打开html页面时和生成图表时。理想的做法是将图表数据传递给Url.Action调用,但可能不可能(?)

使用URL进行测试: /ChartTest?dataExists=false /ChartTest?dataExists=真

控制器:

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

}
@model codeFirstSample.Models.ChartViewModel
@{
ViewBag.Title=“ChartTest”;
}
图表测试使用示例
图表:
@如果(@Model==null)
{
没有找到任何数据

} 其他的 { }
我做了,但并不完美。数据被读取两次:打开html页面时和生成图表时。理想的做法是将图表数据传递给Url.Action调用,但可能不可能(?)

使用URL进行测试: /ChartTest?dataExists=false /ChartTest?dataExists=真

控制器:

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

}
@model codeFirstSample.Models.ChartViewModel
@{
ViewBag.Title=“ChartTest”;
}
图表测试使用示例
图表:
@如果(@Model==null)
{
没有找到任何数据

} 其他的 { }
Hi,您使用哪个库来显示图表?我使用的是Chart Helper(),我将重新创建您的项目,那么什么是PatchingFailurePieChart?PatchingFailurePieChart类有两个属性作为OSName字符串,Count int返回列表。在ArrayList的x,y坐标中进一步使用了哪个。您好,您使用哪个库来显示图表?我正在使用图表助手()我将重新创建您的项目,那么什么是PatchingFailurePieChart?PatchingFailurePieChart类有两个属性作为OSName字符串,Count int返回列表。这将在ArrayList中进一步用于x,y坐标。谢谢。。。如果图表数据不可用,可以显示“无数据”信息。我的声誉<15。所以投票不会公开反映。所以你们不能“点击向上箭头将我的答案从0改为1”?:)谢谢你们选择它作为答案;)但你可以再加1分;)非常感谢。如果图表数据不可用,可以显示“无数据”信息。我的声誉<15。所以投票不会公开反映。所以你们不能“点击向上箭头将我的答案从0改为1”?:)谢谢你们选择它作为答案;)但你可以再加1分;)
@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})" />

}