如果未构建 MVC 4 上的饼图,则显示默认消息
Show default message if Pie Chart on MVC 4 not build
我在 MVC 4 上显示饼图。当数据可用时它工作正常。
但如果在特定情况下找不到数据,我想显示一条默认消息,如 "Chart could not be generated"。
目前我正在局部视图上渲染饼图。
下面是我的代码。
控制器操作:
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;
}
局部视图:
<img src="@Url.Action("PieChart", "PatchFailure", new { dateValue = @ViewBag.dateValue, regType = @ViewBag.regType })"/>
视图:调用局部视图
@Html.Partial("PieChart");
在此先感谢您的帮助。
我做到了,但并不完美。数据被读取两次:打开 html 页面时和生成图表时。理想情况下是将图表数据传递给 Url.Action 调用,但可能不可能(?)。
使用 url 进行测试:
/ChartTest?dataExists=false
/ChartTest?dataExists=true
控制器:
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®Type=aaa&dataExists=false
* http://localhost:54087/ChartTest/GetChart?dateValue=10®Type=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})" />
}
我在 MVC 4 上显示饼图。当数据可用时它工作正常。
但如果在特定情况下找不到数据,我想显示一条默认消息,如 "Chart could not be generated"。
目前我正在局部视图上渲染饼图。
下面是我的代码。
控制器操作:
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;
}
局部视图:
<img src="@Url.Action("PieChart", "PatchFailure", new { dateValue = @ViewBag.dateValue, regType = @ViewBag.regType })"/>
视图:调用局部视图
@Html.Partial("PieChart");
在此先感谢您的帮助。
我做到了,但并不完美。数据被读取两次:打开 html 页面时和生成图表时。理想情况下是将图表数据传递给 Url.Action 调用,但可能不可能(?)。
使用 url 进行测试: /ChartTest?dataExists=false /ChartTest?dataExists=true
控制器:
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®Type=aaa&dataExists=false
* http://localhost:54087/ChartTest/GetChart?dateValue=10®Type=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})" />
}