最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - C3js row data, custom colors - Stack Overflow

programmeradmin2浏览0评论

I'm using c3js to make a chart with row data and want to use specific colours.

var chart = c3.generate({
    data: {
        x:'x',
        rows: [
             ['x','Winter 2007-2008','Winter 2008-2009','Winter 2009-2010'],
             ['2013-01-01',12,30,3],
             ['2013-01-02',123,200,22],
             ['2013-01-03',21,100,54],
             ['2013-01-04',32,400,23],
             ['2013-01-05',12,150,12],
             ['2013-01-06',34,250,15]
        ],
    },
    axis: {
        x: {
            type: 'timeseries',
            tick: {
                format: '%Y-%m-%d'
            }
        }
    }
});

This is all good, except I wish to set each line as a custom colour.

With column data, this is simple:

data: {
    x:'x',
    columns:[[]]
},
color: {
        pattern: ['#1f77b4', '#aec7e8', '#ff7f0e']
}

Can this be achieved with row-oriented data?

I'm using c3js to make a chart with row data and want to use specific colours.

var chart = c3.generate({
    data: {
        x:'x',
        rows: [
             ['x','Winter 2007-2008','Winter 2008-2009','Winter 2009-2010'],
             ['2013-01-01',12,30,3],
             ['2013-01-02',123,200,22],
             ['2013-01-03',21,100,54],
             ['2013-01-04',32,400,23],
             ['2013-01-05',12,150,12],
             ['2013-01-06',34,250,15]
        ],
    },
    axis: {
        x: {
            type: 'timeseries',
            tick: {
                format: '%Y-%m-%d'
            }
        }
    }
});

This is all good, except I wish to set each line as a custom colour.

With column data, this is simple:

data: {
    x:'x',
    columns:[[]]
},
color: {
        pattern: ['#1f77b4', '#aec7e8', '#ff7f0e']
}

Can this be achieved with row-oriented data?

Share Improve this question edited Dec 30, 2014 at 9:09 JasTonAChair asked Dec 30, 2014 at 9:02 JasTonAChairJasTonAChair 1,9781 gold badge19 silver badges31 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Try setting color function to the chart as shown below:-

    color: function (color, d) {
        // d will be 'id' when called for legends
        var colors = {           // Mapping for dataseries to color code.
            'Winter 2007-2008': '#ff0000',
            'Winter 2008-2009':'#00ff00',
            'Winter 2009-2010':'#0000ff'
        };
        if(typeof d === 'object') {
            return colors[d.id];
        }else {
            return colors[d];
        }
        //return d.id && d.id === 'data3' ? d3.rgb(color).darker(d.value / 150) : color;
    }

Working example:- jsFiddle

Color pattern should work:

color: {pattern: ['#ff0000','#00ff00','#0000ff']},

JsFiddle.

发布评论

评论列表(0)

  1. 暂无评论