gpt4 book ai didi

javascript - 如何使用带刻度将图表推至边缘

转载 作者:行者123 更新时间:2023-11-28 10:44:12 25 4
gpt4 key购买 nike

有没有办法让平行坐标一直到边缘,然后在其之间均匀地间隔“尺寸”?我以为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});
}

最佳答案

波段比例尺对于此数据可视化来说是错误的工具。

问题在于频带尺度具有关联的带宽。因此,当您使用频段比例时,它将返回相应带宽的左边距(或者,换句话说,“步长”的左边距):

enter image description here

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

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

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

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

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

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

解决方案:

改用点刻度。点尺度没有带宽:

enter image description here

这是演示,查看值:

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

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

关于填充,因为我说过“下面有更多内容”:你几乎猜对了!问题是 padding 是...

... a convenience method for setting the inner and outer padding to the same padding value.

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

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))
})
<script src="https://d3js.org/d3.v4.min.js"></script>

关于javascript - 如何使用带刻度将图表推至边缘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46730789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com