4

I'm having trouble configuring the bar colors for a Kendo UI column chart. Here's the code:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <div id="chart"></div>
    <script src="js/thirdParty/jquery.js"></script>
    <script src="js/thirdParty/kendo.all.min.js"></script>
    <script>
        $(function () {
            var data,
                dataSource;
            data = [{
                type: "C1",
                amount: 100,
                year: 2008,
                color: "red"
            }, {
                type: "C2",
                amount: 120,
                year: 2008,
                color: "blue"
            }, {
                type: "C2",
                amount: 50,
                year: 2009,
                color: "blue"
            }, {
                type: "C1",
                amount: 10,
                year: 2010,
                color: "red"
            }, {
                type: "C1",
                amount: 120,
                year: 2011,
                color: "red"
            }, {
                type: "C2",
                amount: 50,
                year: 2011,
                color: "blue"
            }];
            dataSource = new kendo.data.DataSource({
                data: data,
                group: {
                    field: "type"
                },
                sort: { field: "year" }
            });
            $("#chart").kendoChart({
                dataSource: dataSource,
                series: [{
                    type: "column",
                    field: "amount",
                    categoryField: "year",
                    name: "#= group.value #",
                    colorField: "color"
                }],
            })
        });
    </script>
</body>
</html>

I'm trying to get "C1" to be red and "C2" to be blue but the chart renders like in the following screen shot:

enter image description here

Any pointers in the right direction would be appreciated.

4

2 回答 2

16

在进一步研究之后,我发现colorField用于将颜色设置为系列中的单个点(而不是整个系列)。我发现seriesColors是我正在寻找的:

http://docs.kendoui.c​​om/api/dataviz/chart#configuration-seriesColors

这是我重构的示例:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <div id="chart"></div>
    <script src="js/thirdParty/jquery.js"></script>
    <script src="js/thirdParty/kendo.all.min.js"></script>
    <script>
        $(function () {
            var data,
                dataSource;
            data = [{
                type: "C1",
                amount: 100,
                year: 2008
            }, {
                type: "C2",
                amount: 120,
                year: 2008
            }, {
                type: "C2",
                amount: 50,
                year: 2009
            }, {
                type: "C1",
                amount: 10,
                year: 2010
            }, {
                type: "C1",
                amount: 120,
                year: 2011
            }, {
                type: "C2",
                amount: 50,
                year: 2011
            }];
            dataSource = new kendo.data.DataSource({
                data: data,
                group: {
                    field: "type"
                },
                sort: { field: "year" }
            });
            $("#chart").kendoChart({
                dataSource: dataSource,
                seriesColors: ["red", "blue"],
                series: [{
                    type: "column",
                    field: "amount",
                    categoryField: "year",
                    name: "#= group.value #"
                }],
            })
        });
    </script>
</body>
</html>
于 2013-09-03T04:48:44.887 回答
3

检查 JS Fiddle

http://jsfiddle.net/9VZdS/45/

$(function() {
    var dataset = new Array(1,2,3,null,5,6);
    var highlighted = new Array(null,null,null,4,null,null);

    $('#chart').kendoChart({
        theme: 'metro',
        seriesDefaults: {
            type: 'column',
            stack: true
        },
        series: [
            {
                name: 'Not Highlighted',
                data: dataset,
                color: 'red'
            },
            {
                name: 'Highlighted',
                data: highlighted,
                color: 'blue'
            }
        ]
    })
});
于 2013-08-27T05:12:22.133 回答