如何使用带刻度将图表推到边缘

时间:2017-10-13 13:11:26

标签: javascript d3.js

有没有办法让平行坐标一直到边缘,然后在它之间平均间隔“尺寸”?我认为x = d3.scaleBand().rangeRound([0, width]).padding(1)是罪魁祸首,但似乎还有其他东西在起作用。如果有人帮助我,我真的很感激。

enter image description here

var margin = {top: 20, right: 10, bottom: 10, left: 20},
                    width = d3.select(ele[0])._groups[0][0].offsetWidth - margin.left - margin.right,
                    height = 400 - margin.top - margin.bottom;

                // set the height based on the calculations above
                svg.attr('height', height + margin.top + margin.bottom);

                var x = d3.scaleBand().rangeRound([0, width]).padding(1),
                    y = {},
                    dragging = {},
                    selected;

                var line = d3.line(),
                    background,
                    foreground,
                    extents;

                x.domain(dimensions = d3.keys(data[0]).filter(function(d) {
                    if(d === "name") {
                        return false;
                    }
                    return y[d] = d3.scaleLinear()
                        .domain(d3.extent(data, function(p) {
                            return +p[d]; }))
                        .range([height, 0]);
                }));

                extents = dimensions.map(function(p) { return [0,0]; });

                // Add grey background lines for context.
                background = svg.append("g")
                    .attr("class", "background")
                    .attr("transform", "translate(0," + margin.top + ")")
                    .selectAll("path")
                    .data(data)
                    .enter().append("path")
                    .attr("d", path);

                // Add blue foreground lines for focus.
                foreground = svg.append("g")
                    .attr("class", "foreground")
                    .attr("transform", "translate(0," + margin.top + ")")
                    .selectAll("path")
                    .data(data)
                    .enter().append("path")
                    .attr("d", path);

                // Add a group element for each dimension.
                var g = svg.selectAll(".dimension")
                    .data(dimensions)
                    .enter().append("g")
                    .attr("class", "dimension")
                    .attr("transform", function(d) {  return "translate(" + x(d) + ")"; })
                    .call(d3.drag()
                        .subject(function(d) { return {x: x(d)}; })
                        .on("start", function(d) {
                            dragging[d] = x(d);
                            background.attr("visibility", "hidden");
                        })
                        .on("drag", function(d) {
                            dragging[d] = Math.min(width, Math.max(0, d3.event.x));
                            foreground.attr("d", path);
                            dimensions.sort(function(a, b) { return position(a) - position(b); });
                            x.domain(dimensions);
                            g.attr("transform", function(d) { return "translate(" + position(d) + ")"; })
                        })
                        .on("end", function(d) {
                            delete dragging[d];
                            transition(d3.select(this)).attr("transform", "translate(" + x(d) + ")");
                            transition(foreground).attr("d", path);
                            background
                                .attr("d", path)
                                .transition()
                                .delay(500)
                                .duration(0)
                                .attr("visibility", null);
                        }));

                // Add an axis and title.
                g.append("g")
                    .attr("class", "y axis")
                    .each(function(d) {  d3.select(this).call(d3.axisLeft(y[d]));})
                    .attr("transform", "translate(0," + margin.top + ")")
                    .append("text")
                    .style("text-anchor", "middle")
                    .attr("y", -9)
                    .text(function(d) { return d; });

                // Add and store a brush for each axis.
                g.append("g")
                    .attr("class", "brush")
                    .attr("transform", "translate(0," + margin.top + ")")
                    .each(function(d) {
                        d3.select(this)
                            .call(y[d].brush = d3.brushY().extent([[-8, 0], [8,height]])
                                .on("brush start", brushstart)
                                .on("brush", brush_parallel_chart));
                    })
                    .selectAll("rect")
                    .attr("x", -8)
                    .attr("width", 16);

                function position(d) {
                    var v = dragging[d];
                    return v === null || v === undefined ? x(d) : v;
                }

                function transition(g) {
                    return g.transition().duration(500);
                }

                // Returns the path for a given data point.
                function path(d) {
                    return line(dimensions.map(function(p) {
                        return [position(p), y[p](d[p])];
                    }));
                }

                function brushstart() {
                    d3.event.sourceEvent.stopPropagation();
                }

                function within(d, extent){
                    return extent[1] <= d && d <= extent[0];
                }

                // Handles a brush event, toggling the display of foreground lines.
                function brush_parallel_chart() {
                    for(var i = 0; i < dimensions.length; ++i){
                        if(d3.event.target === y[dimensions[i]].brush) {
                            extents[i] = d3.event.selection.map(y[dimensions[i]].invert,y[dimensions[i]]);
                        }
                    }

                    foreground.style("display", function(d) {
                        return dimensions.every(function(p, i) {
                            if(extents[i][0]===0 && extents[i][1]===0) {
                                return true;
                            }
                            return within(d[p], extents[i])
                        }) ? null : "none";
                    });

                    selected = data.filter(function(item){
                        if(dimensions.every(function(dim, index){
                                if(extents[index][0]===0 && extents[index][1]===0) {
                                    return true;
                                }
                                return within(item[dim], extents[index]);
                            })){
                            return true;
                        }
                    });

                    scope.onBrush({item: selected});
                }

2 个答案:

答案 0 :(得分:1)

乐队规模是此数据的错误工具。

问题是频段规模具有相关的带宽。因此,当您使用波段比例时,它将返回相应带宽的左边距(或者换句话说,&#34;步骤&#34;的左边距):

enter image description here

使用5个值以及从0100的范围来查看带范围返回的值:

&#13;
&#13;
var data = "abcde".split("")
var s = d3.scaleBand()
  .domain(data)
  .range([0, 100]);

data.forEach(function(d) {
  console.log(s(d))
})
&#13;
<script src="https://d3js.org/d3.v4.min.js"></script>
&#13;
&#13;
&#13;

如果将填充设置为1,将有助于减少带宽,但不会那么多(以下更多内容):

&#13;
&#13;
var data = "abcde".split("")
var s = d3.scaleBand()
  .domain(data)
  .range([0, 100])
  .padding(1);

data.forEach(function(d) {
  console.log(s(d))
})
&#13;
<script src="https://d3js.org/d3.v4.min.js"></script>
&#13;
&#13;
&#13;

<强>解决方案

使用点刻度。点比例没有带宽:

enter image description here

以下是演示,请查看值:

&#13;
&#13;
var data = "abcde".split("")
var s = d3.scalePoint()
  .domain(data)
  .range([0, 100]);

data.forEach(function(d) {
  console.log(s(d))
})
&#13;
<script src="https://d3js.org/d3.v4.min.js"></script>
&#13;
&#13;
&#13;

关于填充,因为我说&#34;更多关于下面&#34; :你几乎把它弄好了!问题是padding是......

  

...一种方便的方法,用于将内部和外部填充设置为相同的填充值。

因此,它设置内部和外部填充。如果您使用paddingInner(1)paddingOuter(0),您将得到相同的点数结果:

&#13;
&#13;
var data = "abcde".split("")
var s = d3.scaleBand()
  .domain(data)
  .range([0, 100])
  .paddingInner(1)
  .paddingOuter(0);

data.forEach(function(d) {
  console.log(s(d))
})
&#13;
<script src="https://d3js.org/d3.v4.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:-2)

尝试将margin更改为:

var margin = {top: 20, right: 0, bottom: 10, left: 0},

在第一行。边距是图表周围的空间。这也应该改变width的值,它使用声明中的边距。由于宽度将增加,x比例应该改变,使“尺寸”等间隔但彼此进一步分开。