Shubham Jindal
Shubham Jindal

Reputation: 21

Adding icons to bar charts made using c3js

I have made a stacked bar chart using c3 libraries and would like to add icons to each column (using c3 or d3). I went through their documentation but there doesnt seem to be any relevant functionality!

var chart = c3.generate({
    data: {
        columns: [
            ['data1', 30, 200, 100],
            ['data2', 130, 100, 140]
        ],
        type: 'bar',
        groups: [['data1', 'data2']]

    },
});

Upvotes: 1

Views: 900

Answers (1)

mgraham
mgraham

Reputation: 6207

You can import font-awesome and then (mis)use c3's label format configuration to set an icon for each series

var chart = c3.generate({
    data: {
        columns: [
            ['data1', 30, -200, -100, 400, 150, 250],
            ['data2', -50, 150, -150, 150, -50, -150],
            ['data3', -100, 100, -40, 100, -150, -50]
        ],
        groups: [
            ['data1', 'data2']
        ],
        type: 'bar',
        labels: {
//            format: function (v, id, i, j) { return "Default Format"; },
            format: {
                data1: function (v, id, i, j) { return "\uf1ec"; }, // a calculator
                data2: function (v, id, i, j) { return "\uf212"; }, // a book
                data3: function (v, id, i, j) { return "\uf1ae"; },  // a human
            }
        }
    },
    grid: {
        y: {
            lines: [{value: 0}]
        }
    }
});

You'll need this css rule -->

.c3-chart-text .c3-text {
  font-family: 'FontAwesome';
}

And to import the FontAwesome css to get access to the glyphs (this may be an old version) -->

https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css

The glyph codes can be found here -->

https://fontawesome.com/cheatsheet

Example: https://jsfiddle.net/h0g1fwpa/19/

Upvotes: 3

Related Questions