D3 Y Scale, y vs height?

I'm learning D3, and I can see that I get the same visualization with these two things:

var w = 600;
var h = 100;

var dataset = [1, 6, 3, 4, 10, 4, 9]

var svg = d3.select("body").append("svg")
    .attr("height", h)
    .attr("width", w)

var xScale = d3.scale.linear()
    .domain([0, dataset.length])
    .range([0, w]);

Using the height attribute:

 
var yScale = d3.scale.linear()
    .domain([0, d3.max(dataset)])
    .range([h, 0]);

svg.selectAll("rect")
    .data(dataset)
    .enter()
    .append("rect")
    .attr({
        x: function(d, i) { return xScale(i); },
        y: function(d) { return yScale(d); },
        width: w / dataset.length,
        height: function(d) { return h - yScale(d); },
        fill: "teal"
    });

在这里输入图像描述

Or setting y:

var yScale = d3.scale.linear()
    .domain([0, d3.max(dataset)])
    .range([0, h]);

svg.selectAll("rect")
    .data(dataset)
    .enter()
    .append("rect")
    .attr({
        x: function(d, i) { return xScale(i); },
        y: function(d) { return h - yScale(d); },
        width: w / dataset.length,
        height: function(d) { return yScale(d); },
        fill: "teal"
    });

在这里输入图像描述

Is either one more correct, if so, why?


Now that I got to a later chapter, it seems like former option, which uses range([h, 0]) is better when creating a yAxis. If I use the latter option range([0, h]) , the graph comes out as follows (with no transforms of the yAxis call:

在这里输入图像描述

Which generally is not what one would want.

链接地址: http://www.djcxy.com/p/90976.html

上一篇: D3js format.parse()返回null比Date

下一篇: D3 Y Scale,y vs高度?