将y轴上的数字转换为字符串,K为千d3.js [英] converting numbers on y axis to string with K for thousand d3.js

查看:267
本文介绍了将y轴上的数字转换为字符串,K为千d3.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用d3.js图表​​绘制y轴和x轴..它的工作精细...但在y轴的值,你可以说范围是说0到10000,我想如果数字大于千它会带来K说
如果数字是1000它将显示1K,对于15000它将显示在y轴上的滴答15K

i am using d3.js charts to plot y axis and x axis .. its working fine ... but the values in y axis you can say range is say 0 to 10000 and i want if the number is greater than thousand it will come with K say if the number is 1000 it will show 1K, for 15000 it will show on y axis ticks 15K

如何做到。 。我不能为字符串值管理y.domain和range函数...

how to do that .. i am not able to manupulate y.domain and range functions for the string values ...

var y = d3.scale.linear().range([ height, 0 ]);
y.domain([0,
                                  //d3.min(cities, function(c) { return d3.min(c.values, function(v) { return v.count; }); }),
                                  d3.max(cities, function(c) { return d3.max(c.values, function(v) { return v.count; }); })
                                ]);

如何做到这一点有什么办法...请帮助

how to do that is there any way to do ... please help

推荐答案

API参考< a>,我们看到 d3.formatPrefix

var prefix = d3.formatPrefix(1.21e9);
console.log(prefix.symbol); // "G"
console.log(prefix.scale(1.21e9)); // 1.21

我们可以这样使用

var yAxis = d3.svg.axis()
    .scale(y)
    .ticks(5)
    .tickFormat(function (d) {
        var prefix = d3.formatPrefix(d);
        return prefix.scale(d) + prefix.symbol;
    })
    .orient("left");

但是,如果这正是你的意思,我们可以简化 d3.format(s)

However, in case this is exactly what you mean, we can simplify using d3.format("s")

var yAxis = d3.svg.axis()
    .scale(y)
    .ticks(5)
    .tickFormat(d3.format("s"))
    .orient("left");

这篇关于将y轴上的数字转换为字符串,K为千d3.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆