简体   繁体   English

d3.js在time.scale上设置rect的x位置

[英]d3.js setting x position of rect on time.scale

I am working on a d3 chart that uses a time.scale and rects and I am stuck on a very small issue. 我正在制作一个使用time.scale和rects的d3图表,我遇到了一个非常小的问题。 Either the x axis is not extending the full length of its svg container (so the x axis is not under that last bar) or the x position of the rects is somehow not properly taking the range into account. x轴没有延伸其svg容器的整个长度(因此x轴不在最后一个条形下)或者rect的x位置在某种程度上不能正确地考虑该范围。 I have tried looking through several examples and many stackoverflow posts but so far I can't figure out where I've screwed up. 我已经尝试了几个例子和许多stackoverflow帖子,但到目前为止我无法弄清楚我搞砸了。 Thank you! 谢谢!

Here is my code: 这是我的代码:

var margin = {top:30, right:30, bottom:30, left:30},
    width = 600 - margin.right - margin.left,
    height = 300 - margin.top - margin.bottom;  


var parseDate =  d3.time.format("%d-%b-%y").parse;

var formatTime = d3.time.format("%e %B");                 


var x = d3.time.scale().range([0, width]);

var y = d3.scale.linear().range([height, 0]);



var xAxis = d3.svg.axis()
            .scale(x)
            .orient("bottom")
            .tickFormat(d3.time.format("%b %y"));                       


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

var svg = d3.select("body").append("svg")
            .attr("width", width + margin.left + margin.right)
            .attr("height",height + margin.top + margin.bottom)
            .append("g")
            .attr("transform" , "translate(" + margin.left + "," + margin.top + ")");





d3.tsv("data/months.tsv", function(error, data) {
data.forEach(function(d) {
        d.month =  parseDate(d.month);
        d.value = +d.value;  //ensures variable is treated like a number

    }); 


   x.domain(d3.extent(data, function(d) { return d.month; }));
   y.domain([0,d3.max(data,function(d) { return d.value; })]);

   var barPadding = 4;

   var barWidth = width/data.length - barPadding;

   var rects = svg.selectAll("rect")
                .data(data)
                .enter().append("rect")
                .attr("class" , "bar")
                .attr("x", function(d) { return x(d.month); })  // I think the problem is here
                .attr("y", function(d) { return y(d.value); })  
                .attr("width", barWidth )   
                .attr("height", function(d) { return height - y(d.value); });           



    svg.append("g")
            .attr("class", "axis")
            .attr("transform","translate(0, " + height + ")")
            .call(xAxis);

            svg.append("g")
            .attr("class" , "axis") 
            .call(yAxis);                   

});

Thank you in advance! 先感谢您!

The problem is that you're starting the bars at the position of the ticks. 问题是你在蜱虫的位置开始了。 That is, the left hand side of the bar is aligned with the respective tick, creating the impression that the bar isn't aligned because it is off center. 也就是说,条形图的左侧与相应的刻度线对齐,从而产生条形图未对齐的印象,因为它偏离中心。

To fix, simply subtract barWidth/2 as an offset to the x position of the bars. 要修复,只需将barWidth/2减去作为条形x位置的偏移量。 You'll also want to adjust the output range of your x scale such that the y axis and the first bar don't overlap. 您还需要调整x刻度的输出范围,使y轴和第一个条不重叠。

x.range([barWidth/2, width]);
// etc.
rects.attr("x", function(d) { return x(d.month) - barWidth/2; })

Complete example here . 在这里完成示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM