范围图中的rangeRoundBands outerPadding方式太大了

bgb*_*ink 11 javascript css d3.js

我是D3.js的新手,我的垂直条形图有问题.出于某种原因,当我使用rangeRoundBands进行缩放时,轴和条之间的距离太大.在API中,它解释如下:

在此输入图像描述

所以问题似乎是outerPadding.但是将outerPadding设置为零并没有帮助.但是,当我使用rangeBands时,问题就会消失,并且条形正确定位在轴的正下方.但后来我会得到这些讨厌的抗锯齿效果,所以这不是一个真正的选择.这是我的代码:

var margin = {top: 40, right: 40, bottom: 20, left: 20},
    width = 900 - margin.left - margin.right,
            height = x - margin.top - margin.bottom;

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

    var y = d3.scale.ordinal().rangeRoundBands([0, height], .15, 0);

    var xAxis = d3.svg.axis()
            .scale(x)
            .orient("top");

    var xAxis2 = d3.svg.axis()
            .scale(x)
            .orient("bottom");

    var xAxis3 = d3.svg.axis()
            .scale(x)
            .orient("bottom")
            .tickSize(-height, 0, 0)
            .tickFormat("");

    var svg = d3.select("#plotContainer").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 + ")");


    x.domain(d3.extent(data, function(d) {
        return d.size;
    })).nice();
    y.domain(data.map(function(d) {
        return d.name;
    }));

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

    svg.selectAll(".bar")
            .data(data)
            .enter().append("rect")
            .attr("class", function(d) {
                return d.size < 0 ? "bar negative" : "bar positive";
            })
            .attr("x", function(d) {
                return x(Math.min(0, d.size));
            })
            .attr("y", function(d) {
                return y(d.name);
            })
            .attr("width", function(d) {
                return Math.abs(x(d.size) - x(0));
            })
            .attr("height", y.rangeBand())
            .append("title")
            .text(function(d) {
                return "This value is " + d.name;
            });
    ;

    svg.selectAll(".bar.positive")
            .style("fill", "steelblue")
            .on("mouseover", function(d) {
                d3.select(this).style("fill", "yellow");
            })
            .on("mouseout", function(d) {
                d3.select(this).style("fill", "steelblue");
            });

    svg.selectAll(".bar.negative")
            .style("fill", "brown")
            .on("mouseover", function(d) {
                d3.select(this).style("fill", "yellow");
            })
            .on("mouseout", function(d) {
                d3.select(this).style("fill", "brown");
            });

    svg.selectAll(".axis")
            .style("fill", "none")
            .style("shape-rendering", "crispEdges")
            .style("stroke", "#000")
            .style("font", "10px sans-serif");

    svg.selectAll(".grid")
            .style("fill", "none")
            .style("stroke", "lightgrey")
            .style("opacity", "0.7");

    svg.selectAll(".grid.path")
            .style("stroke-width", "0");
Run Code Online (Sandbox Code Playgroud)

编辑:请看看这个小提琴:http: //jsfiddle.net/GUYZk/9/

我的问题在那里是可重复的.您不能使用rangeRoundBands更改outerPadding,而rangeBands表现正常.

Ben*_*her 9

TL; DR:这是数学的结果.要解决此问题,请使用rangeBands布局条形图,并使用shape-rendering: crispEdgesCSS将它们与像素边界对齐.


完整说明:

因为rangeRoundBands函数必须在所提供的像素范围内均匀分布条形,并且它还必须提供整数rangeBand,它用于Math.floor切断每个连续条形的小数位.

这个额外的外部填充复合具有更长数据集的原因是因为所有这些分数像素必须在某处结束.此函数的作者选择在范围的开头和结尾之间均匀分割它们.

因为每个圆角条的分数像素在间隔上(0, 1),所以在每一端上的额外像素将跨越数据条计数的大约1/4.使用10个条形图时,将永远不会注意到2-3个额外像素,但如果您有100个或更多像素,则额外的25个像素会变得更加明显.

一种可能的解决方案似乎适用于Chrome svg:rect:用于rangeBands布局,但随后shape-rendering: crispEdges作为CSS样式应用于条形路径/ rects.

然后,这使得SVG渲染器上的责任将每个条轻推到像素边界,但是它们在整体上间隔更均匀,间距的偶然变化以解决整个图表上的误差.

就个人而言,我使用shape-rendering: optimizeSpeed并让渲染代理做出必要的权衡,以快速渲染(可能是分数)条形位置.