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

javascript - Creating custom donut or pie chart using c3.js - Stack Overflow

programmeradmin3浏览0评论

Trying to create the below using c3.js.

We are using the same charting library across the application so would like to keep it consistent. Didn't find a way in c3.js to either customize donut or pie chart to get this. Any help or pointers are greatly appreciated.

Trying to create the below using c3.js.

We are using the same charting library across the application so would like to keep it consistent. Didn't find a way in c3.js to either customize donut or pie chart to get this. Any help or pointers are greatly appreciated.

Share Improve this question edited Jul 2, 2015 at 22:03 potatopeelings 41.1k8 gold badges96 silver badges121 bronze badges asked Jun 25, 2015 at 0:36 confusedfreakconfusedfreak 1011 silver badge4 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 19

HTML

<div id="chart"></div>

CSS

#chart {
    width: 150px;
    height: 150px;    
}

/* don't do anything fancy when hovering */
#chart .c3-defocused.c3-target {
    opacity: 1 !important;
}

#chart text {
    fill: #ccc;
}

#chart .c3-chart-arcs-title {
    fill: white;
    font-size: 3em;
}

JS

var percentage = 0.79;

var chart = c3.generate({
    data: {
        columns: [
            ['show', percentage],
            ['dontshow', 1 - percentage],
        ],
        type: 'donut',
        order: null
    },
    color: {
        pattern: ['#13BDD1', '#FFF']
    },
    legend: {
        show: false
    },
    donut: {
        label: {
            show: false
        },
        title: Math.round(percentage * 100),
        width: 15,
        expand: false
    },
    tooltip: {
        show: false
    }
});

// baseline text properly
d3
    .select(".c3-chart-arcs-title")
    .attr("dy", "0.3em")

// add percentage symbol
d3.select(".c3-chart-arcs")
    .append("text")
    .text("%")
    .attr("dy", "-0.5em")
    .attr("dx", "2em")

// black background for center text
d3.select(".c3-chart")
    .insert("circle", ":first-child")
    .attr("cx", chart.internal.width / 2)
    .attr("cy", chart.internal.height / 2 - chart.internal.margin.top)
    .attr("r", chart.internal.innerRadius)

Fiddle - http://jsfiddle.net/xpvhow5p/


发布评论

评论列表(0)

  1. 暂无评论