我正在做简单的直方图。
<script>
var width=500,
height=500,
padding=50;
d3.csv('mydata.csv',function(data){
var map= data.map(function(i){ return parseInt(i.age)})
var histogram= d3.layout.histogram()
.bins(7)(map)
console.log(histogram)
var y = d3.scale.linear()
.domain([0,d3.max(histogram.map(function(i){return i.length;}))])
.range([0,height])
var x = d3.scale.linear()
.domain([0,d3.max(map)])
.range([0,width]);
var canvas= d3.select('body').append('svg')
.attr('width',width)
.attr('height',height+padding);
var bars = canvas.selectAll(".bar").data(histogram)
.enter().append('g').attr('class','bar')
bars.append('rect')
.attr('x',function(d) {return x(d.x);})
.attr('y',0)
.attr('width',function(d) {return x(d.dx);})
.attr('height',function(d) {return y(d.y)})
.attr('fill','purple')
})
</script>
我在YouTube上学习D3(https://www.youtube.com/watch?v=0CZ7-f9wXiM&list=PL6il2r9i3BqH9PmbOf5wA5E1wOG3FT22p&index=18)
我的问题在下面比例变量x和y即使不是函数,也不是fucntion()。但是在bars.append('rect')中,返回x(d.x)返回x(d.dx)返回y(d.y)这三件事就像具有参数的函数一样。这个API发生了什么?
我的英语很抱歉。如果您回答我,我会很高兴,我会尽力去认识!thx
“ range()”的返回值显然是一个数组。例如: