我是使用Entity Framework的MVC4新手。现在我得到列的计数和键值,并将它们存储到varible中。我想将该变量传递给JavaScript,以获取这些值并将它们分配到图表中。如何根据程序数量获得动态图表?
现在我暂时使用ViewBag。
这是我的控制器代码:
var qry = db.Tbl_Mst_Programs.Where(r => r.Program != null)
.GroupBy(r => r.Program)
.Select(gr => new { key = gr.Key, Count = gr.Count() }).ToList();
var internship = qry[0];
var ltv = qry[1];
var mtv = qry[2];
var workCampus = qry[3];
ViewBag.internship = internship;
ViewBag.ltv = ltv;
ViewBag.mtv = mtv;
ViewBag.workCampus = workCampus;
return View();
这是我对图表的查看代码:
<script type="text/javascript">
var internship = @ViewBag.internship;
var ltv=@ViewBag.ltv;
var mtv=@ViewBag.mtv;
var worcmps=@ViewBag.workCampus;
var testdata = [
{
key: "Internship",
y: internship
},
{
key: "LTV",
y: ltv
},
{
key: "MTV",
y: mtv
},
{
key: "Work Campus",
y: worcmps
}
];
function thirtySeries() {
var data = [];
for (var i = 0; i < 30; i++) {
data.push({
key: "Series-" + i,
y: Math.floor(Math.random() * 100)
});
}
return data;
}
function defaultChart(containerId, data, labelType) {
nv.addGraph(function () {
var width = 300,
height = 300;
var chart = nv.models.pieChart()
.x(function (d) { return d.key })
.y(function (d) { return d.y })
.color(d3.scale.category10().range())
.width(width)
.height(height)
.labelType(labelType)
;
d3.select("#" + containerId + " svg")
.datum(data)
.transition().duration(1200)
.attr('width', width)
.attr('height', height)
.call(chart);
nv.utils.windowResize(chart.update);
return chart;
});
}
答案 0 :(得分:0)
如果您在视图对象中有数据,则只需按如下方式访问它:
var ltv = '@Model.ltv';