d3.js: tickformat - adding a % sign without multiplying by 100

后端 未结 1 1774
醉梦人生
醉梦人生 2021-02-05 02:24

my data has percents as, for example, [10.1, 3.2, 5.4]

d3.format(\"0f\") will give me [10, 3, 5]

d3.format(\"0%\")

1条回答
  •  悲&欢浪女
    2021-02-05 03:03

    Update for D3 v4 (using ES6):

    // Can also be axisTop, axisRight, or axisBottom
    d3.axisLeft()
        .tickFormat(d => d + "%")
    

    Original answer for D3 v3:

    You can create your own format:

    d3.svg.axis()
        .tickFormat(function(d) { return d + "%"; });
    

    If you want to get rid of the decimal places:

    d3.svg.axis()
        .tickFormat(function(d) { return parseInt(d, 10) + "%"; });
    

    0 讨论(0)
提交回复
热议问题