饼图在局部视图中不起作用
Piechart is not working in PartialView
通常应用程序使用 ViewBag。但是当我使用局部视图时,它不能使用viewbag。所以我把它作为一个字符串扔掉。数据正在运行,但饼图未打印。
Javascript 局部视图
@模型字符串
window.onload = function () {
var chart = new CanvasJS.Chart("chartContainer", {
theme: "light1", // "light1", "light2", "dark1", "dark2"
exportEnabled: true,
animationEnabled: true,
title: {
text: "Indian Smartphone Market Share"
},
subtitles: [{
text: "Q2, 2016"
}],
data: [{
type: "pie",
startAngle: 180,
toolTipContent: "<b>{label}</b>: {y}%",
showInLegend: "true",
legendText: "{label}",
indexLabel: "{label} - {y}%",
dataPoints: @Html.Raw(Model),
}]
});
chart.render();
}
</script>
控制器
[HttpPost]
public ActionResult Index(List<string> ulkeler)
{
NwContext db = new NwContext();
List<Ulke> ulkecs = new List<Ulke>();
foreach (var item in ulkeler)
{
ulkecs.Add( new Ulke { count = db.Customers.Where(x => x.Country == item).Count(),name =item });
}
List<DataPoint> dataPoints = new List<DataPoint>();
foreach (var item in ulkecs)
{
dataPoints.Add(new DataPoint(item.name.ToString(), Convert.ToInt32(item.count)));
}
string a= JsonConvert.SerializeObject(dataPoints);
return PartialView("PartialViewExample", a);
}
我是这样修复的:
我删除了这个window.onload = function () {
,写了function show() {
和
<button onclick="show()"></button>
通常应用程序使用 ViewBag。但是当我使用局部视图时,它不能使用viewbag。所以我把它作为一个字符串扔掉。数据正在运行,但饼图未打印。
Javascript 局部视图
@模型字符串
window.onload = function () {
var chart = new CanvasJS.Chart("chartContainer", {
theme: "light1", // "light1", "light2", "dark1", "dark2"
exportEnabled: true,
animationEnabled: true,
title: {
text: "Indian Smartphone Market Share"
},
subtitles: [{
text: "Q2, 2016"
}],
data: [{
type: "pie",
startAngle: 180,
toolTipContent: "<b>{label}</b>: {y}%",
showInLegend: "true",
legendText: "{label}",
indexLabel: "{label} - {y}%",
dataPoints: @Html.Raw(Model),
}]
});
chart.render();
}
</script>
控制器
[HttpPost]
public ActionResult Index(List<string> ulkeler)
{
NwContext db = new NwContext();
List<Ulke> ulkecs = new List<Ulke>();
foreach (var item in ulkeler)
{
ulkecs.Add( new Ulke { count = db.Customers.Where(x => x.Country == item).Count(),name =item });
}
List<DataPoint> dataPoints = new List<DataPoint>();
foreach (var item in ulkecs)
{
dataPoints.Add(new DataPoint(item.name.ToString(), Convert.ToInt32(item.count)));
}
string a= JsonConvert.SerializeObject(dataPoints);
return PartialView("PartialViewExample", a);
}
我是这样修复的:
我删除了这个window.onload = function () {
,写了function show() {
和
<button onclick="show()"></button>