javascript - C3.js - 改变折线图中圆圈的颜色

标签 javascript css c3

我正在使用 C3.js 构建折线图。在此图中,我必须根据颜色数组更改圆圈颜色。

c3的代码如下:

var chart = c3.generate({
        bindto:'#timeline',
        colors:['red', 'blue','green','yellow','green','red'],
        data: {
            x: 'x',
            columns: [
                ['x', '2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'],
                ['data1', 10, 15, 12, 5, 9, 11],
                ['data2', 15, 12, 10, 8, 4, 12],
            ]
        },
        axis: {
            x: {
                type: 'timeseries',
                tick: {
                    format: '%Y-%m-%d'
                },
                label: {
                    text: 'Dates',
                    position: 'outer-middle'
                }
            },
            y: {
                show:true,
                label: {
                    text: 'Count',
                    position: 'outer-middle'
                }
            }
        }
    });

对于每个数据点,我想用 colors 数组中的相应颜色填充它。我必须使用 CSS,但我不确定如何在 generate 函数中使用它。

最佳答案

经过几次尝试,我找到了方法。

数组 colors 不应在 generate 函数内。如下所示在 generate 函数之外声明它,然后使用 c3color 属性为每个点分配颜色,如下所示。

    var colors = ['red','green','blue','yellow','black','red'];
var chart = c3.generate({
    data: {
        columns: [
            ['data1', 30, 200, 100, 150, 150, 250],
        ],
        type: 'line',
        color:function(color,d){
                return colors[d.index];    
        }
    },
});

其工作原理显示在 this fiddle. 中。

关于javascript - C3.js - 改变折线图中圆圈的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31399174/

相关文章:

c3js - 显示在内容顶部的网格线

javascript - 使用reduce 添加数组中的数字对 (javascript)

html - 为什么在 Windows Phone 7.5 上的 Internet Explorer 中向 body 元素应用边距时,我的字体大小如此困惑?

javascript - 在其他页面上显示div

javascript - C3 图表 - 工具提示的内容可点击

javascript - c3.js 如何获取 DataGroups 的 onclick 事件

javascript - "Try...Catch" block 不使用 parseInt()

javascript - 如何使用 Javascript 加载 NSTableView 以实现自动化

javascript - Scrolltop 水平起点

html - CSS :after background in Safari for Windows