How to set Category Label for dynamic stacked column chart

950 Views Asked by At

I am building a stacked column chart. I don't know the number of series going into it so I am using a foreach to build each series. I want a category label for each of the series. Typically for something like this I would use the categoryexpression but can't figure out how to do it with the way I am building. Here is what it looks like w/o the labels, just for reference to code.

enter image description here

Any help would be appreciated.

@(Html.Kendo().Chart()

.Name("chart")

.Theme("flat")

.Title("Issues Waterfall")

.DataSource(ds => ds

    .ServerOperation(false)

)

.Series(series =>

{

    series.Column(new double[] { 100 }).Name("Total").Color("Blue").Stack("Total");



    foreach (var resp in Model.listResponsibleDowntime)

    {

        series.Column(new double?[] { resp.percent_pad }).Name(resp.resp_name).Color("White").Opacity(0).Labels(false).Tooltip(false).Stack(resp.resp_name);

        series.Column(new double?[] { resp.percent_downtime }).Name(resp.resp_name).Color(resp.resp_color).Labels(lables => lables.Format("{0:n2}%").Visible(true).Position(ChartBarLabelsPosition.OutsideEnd)).Stack(resp.resp_name);

    }



    series.Column(new double?[] { Model.oee }).Name("Actual").Color("Green").Stack("Actual").Labels(lables => lables.Format("{0:n2}%").Visible(true).Position(ChartBarLabelsPosition.OutsideEnd));



})

.CategoryAxis(axis => axis

    .MajorGridLines(lines => lines.Visible(false))

    .Labels(model => model

        .Rotation(0)

        .Visible(true)

    )

    //.Categories(Model.listCategories)

)

.Legend(legend => legend

    .Position(ChartLegendPosition.Top)

    .Margin(20, 50, 20, 50)

    .Visible(false)

)

.ValueAxis(axis => axis

    .Numeric()

    .Min(0)

    .Max(100)

    .Labels(labels => labels.Format("{0:n0}%"))

)

.Tooltip(tooltip => tooltip

    .Visible(true)

    .Template("#= series.name #: #= kendo.format('{0:n2}', value) #")

)

)

1

There are 1 best solutions below

0
On

I know it's old but this might help the next person because Telerik's documentation wont.

In your foreach you need to pass the the column builder an IEnumerable class and then tell it which field is the category and which is the value:

    public class KendoStackedColumnModel
    {
        public string StackName { get; set; }
        public string Colour { get; set; }

        public IEnumerable<KendoColumnModel> Columns { get; set; }

        public class KendoColumnModel
        {
            public decimal Value { get; set; }
            public string Category { get; set; }
        }

    }

And in the foreach:

    series.Column(stacked.Columns).CategoryField("Category").Field("Value").Name(stacked.StackName).Color(stacked.Colour);