javascript 剑道 UI 图表颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18456990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:00:19  来源:igfitidea点击:

Kendo UI Chart Colors

javascriptjquerytelerikkendo-ui

提问by Carl Rippon

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

我在为 Kendo UI 柱状图配置条形颜色时遇到问题。这是代码:

<!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 redand "C2" to be bluebut the chart renders like in the following screen shot:

我试图让“C1”为红色,“C2”为蓝色,但图表呈现如下屏幕截图:

enter image description here

在此处输入图片说明

Any pointers in the right direction would be appreciated.

任何指向正确方向的指针将不胜感激。

回答by Carl Rippon

After looking into this further, I found that colorFieldis for setting color to a single point in the series (not to the entire series). I found seriesColorswas what I was looking for:

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

http://docs.kendoui.com/api/dataviz/chart#configuration-seriesColors

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

Here is my refactored example:

这是我重构的示例:

<!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>

回答by Arun Bertil

check the JS Fiddle

检查JS小提琴

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

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'
            }
        ]
    })
});