5

我在这个问题上花了几个小时,但似乎无法弄清楚如何解决它。

我正在尝试创建一个像这样的堆积条形图:

http://demos.kendoui.c​​om/dataviz/bar-charts/stacked-bar.html

我正在使用 ASP.NET MVC (Razor)。

这是一些示例代码:

@(Html.Kendo().Chart()
    .Name("chart")
    .Title("chart")
    .Legend(legend => legend
        .Position(ChartLegendPosition.Bottom)
    )
    .SeriesDefaults(seriesDefaults =>
        seriesDefaults.Bar().Stack(true)
    )
    .Series(series =>
    {

        series.Bar(new double[] { 4 });
        series.Bar(new double[] { 2 });
        series.Bar(new double[] { 7 });

    })
    .CategoryAxis(axis => axis
        .Categories("Machine")
        .MajorGridLines(lines => lines.Visible(false))
    )
    .ValueAxis(axis => axis
        .Numeric()
        .Labels(labels => labels.Format("{0}"))
        .Max(24)
        .Line(line => line.Visible(false))
        .MajorGridLines(lines => lines.Visible(true))
    )
    .Tooltip(tooltip => tooltip
        .Visible(true)
        .Template("#= series.name #: #= value #")
    )
)

现在这是我面临的问题。这行代码:

series.Bar(new double[] { 4 });

将值 4 添加到我的图表中。问题是,在我写代码的那一刻,我不知道会有多少系列。这将由数据库中的数据决定。例如,如果有 5 条记录,我需要添加 5 个系列,以此类推。

你知道如何添加这些系列吗?

干杯!

4

1 回答 1

5

For others having simmilar problem I think I found a workaround .

@{
List<List<int>> myList = new List<List<int>>();
myList.Add(new List<int> { 7 });
myList.Add(new List<int> { 2 });
myList.Add(new List<int> { 3 });
myList.Add(new List<int> { 4 });
}        

...

.Series(series =>
        {
            for (int i = 0; i < 4;i++ )
            {
                series.Bar(myList[i]);
            }

        })
...

Since Kendo Ui chart wants to get List I had to create List of Lists. Than use for loop to iterate over desired data. This way data can be stacked in only one category.

It allows the number of series to be a variable (passed to the view through model)

于 2013-10-18T14:41:27.337 回答