Adding icons to bar charts made using c3js

感情迁移 提交于 2019-12-10 12:16:46

问题


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']]

    },
});

回答1:


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/



来源:https://stackoverflow.com/questions/52037460/adding-icons-to-bar-charts-made-using-c3js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!