javascript - C3js row data, custom colors - Stack Overflow

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

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744928293a4601578.html

相关推荐

  • javascript - C3js row data, custom colors - Stack Overflow

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

    1天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信