我看到很多D3代码都有这样的东西:
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
截至D3版本4.0 d3.scale.ordinal()
现在为d3.scaleOrdinal
,rangeRoundBands
似乎已消失。
> d3.scaleOrdinal()
{
[Function: scale]
domain: [Function],
range: [Function],
unknown: [Function],
copy: [Function]
}
D3 v4相当于这段代码(来自Mike Bostock' s bar chart example)会是什么?
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
答案 0 :(得分:67)
在D3 4.x rangeRoundBands
被移动到新的 Band 比例:
d3.scaleBand()
.range([range])
.round([round]);
相当于:
d3.scaleBand()
.rangeRound([range]);
答案 1 :(得分:50)
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
以上计算波段并设置波段之间的填充。在v4中,等价物是
var x = d3.scaleBand()
.rangeRound([0, width])
.padding(0.1);
答案 2 :(得分:-2)
var svg = d3.select("svg"),
margin = {top: 20, right: 20, bottom: 30, left: 60},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom;
var x = d3.scaleBand().rangeRound([0, width]).padding(0.1),
y = d3.scaleLinear().rangeRound([height, 0]);
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.tsv("data.txt", function(d) {
d.y = +d.y;
return d;
}, function(error, data) {
if (error) throw error;
x.domain(data.map(function(d) { return d.x; }));
y.domain([0, d3.max(data, function(d) { return d.y; })]);
使用scaleBand和scaleLinear的经典图表的可启动语法。