Morris.js 包含来自 ViewBag 的动态数据的甜甜圈

Morris.js Donut with dynamic data from ViewBag

有一些关于 Viewbag(MVC) & morris.js 的问题。我从这里找到了一些信息,但其中 none 有效。这就是为什么我决定发布它。

我有一个ViewBag.ServicesData变量存储数据如下(table方便阅读)

Service    Count
----------------
Color      1
Cut        6
Massage    2
Perm       2
Spa        2

但我想将此数据填充到 morris.js 甜甜圈 图表中。如果我对其进行硬编码,代码将类似于:

Morris.Donut({
  element: 'donut-example',
  data: [
    {label: "Color", value: 1},
    {label: "Cut", value: 6},
    {label: "Massage", value: 2},
    {label: "Perm", value: 2},
    {label: "Spa", value: 2}
  ]
});

我的问题来了,我不知道如何将 viewbag 数据存储到 data: [] 可以接受并成功显示圆环图的数组中?提前致谢。

已编辑: 最后,我在尝试不同的实施方式时找出了 答案

Morris.Donut({
  element: 'donut-example',
  data: [
    @foreach(var item in ViewBag.ServicesData)
    {
        @:{ label: "@item.service", value: "@item.count" },
    }
  ]
});

最后,我在尝试不同的实施方式时找到了答案

Morris.Donut({
  element: 'donut-example',
  data: [
    @foreach(var item in ViewBag.ServicesData)
    {
        @:{ label: "@item.service", value: "@item.count" },
    }
  ]
});