gpt4 book ai didi

javascript - 直方图的 X 轴值

转载 作者:行者123 更新时间:2023-12-03 02:18:16 26 4
gpt4 key购买 nike

如何定义 X 轴的值范围?

我举了一个使用 0 到 1 范围内的十进制值的示例,这显然不适用于更大的数字。

<!DOCTYPE html>
<meta charset="utf-8">
<style>

.bar rect {
fill: steelblue;
}

.bar text {
fill: #fff;
font: 10px sans-serif;
}

</style>
<svg width="960" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>

// var data = d3.range(1000).map(d3.randomBates(10));
var data = [1321017167, 1421017167, 1421017167, 1421017167, 1521017167, 1521017167];

var formatCount = d3.format(",.0f");

var svg = d3.select("svg"),
margin = {top: 10, right: 30, bottom: 30, left: 30},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom,
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var x = d3.scaleLinear()
.rangeRound([0, width]);

var bins = d3.histogram()
.domain(x.domain())
.thresholds(x.ticks(20))
(data);

var y = d3.scaleLinear()
.domain([0, d3.max(bins, function(d) { return d.length; })])
.range([height, 0]);

var bar = g.selectAll(".bar")
.data(bins)
.enter().append("g")
.attr("class", "bar")
.attr("transform", function(d) { return "translate(" + x(d.x0) + "," + y(d.length) + ")"; });

bar.append("rect")
.attr("x", 1)
.attr("width", x(bins[0].x1) - x(bins[0].x0) - 1)
.attr("height", function(d) { return height - y(d.length); });

bar.append("text")
.attr("dy", ".75em")
.attr("y", 6)
.attr("x", (x(bins[0].x1) - x(bins[0].x0)) / 2)
.attr("text-anchor", "middle")
.text(function(d) { return formatCount(d.length); });

g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));

</script>

最佳答案

默认情况下,线性比例尺的域为[0, 1]。您只是从 Mike Bostock 复制了该代码,而没有更改域。巧合的是,在他的原始代码中,该域是默认域,但在您的代码中,您必须定义它:

var x = d3.scaleLinear()
.rangeRound([0, width])
.domain(d3.extent(data))//domain here

这里我使用d3.extent,但您可以使用您想要的任何其他数组。

这是您的代码,仅进行了更改:

<!DOCTYPE html>
<meta charset="utf-8">
<style>

.bar rect {
fill: steelblue;
}

.bar text {
fill: #fff;
font: 10px sans-serif;
}

</style>
<svg width="960" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>

// var data = d3.range(1000).map(d3.randomBates(10));
var data = [1321017167, 1421017167, 1421017167, 1421017167, 1521017167, 1521017167];

var formatCount = d3.format(",.0f");

var svg = d3.select("svg"),
margin = {top: 10, right: 30, bottom: 30, left: 30},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom,
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var x = d3.scaleLinear()
.rangeRound([0, width])
.domain(d3.extent(data))

var bins = d3.histogram()
.domain(x.domain())
.thresholds(x.ticks(20))
(data);

var y = d3.scaleLinear()
.domain([0, d3.max(bins, function(d) { return d.length; })])
.range([height, 0]);

var bar = g.selectAll(".bar")
.data(bins)
.enter().append("g")
.attr("class", "bar")
.attr("transform", function(d) { return "translate(" + x(d.x0) + "," + y(d.length) + ")"; });

bar.append("rect")
.attr("x", 1)
.attr("width", x(bins[0].x1) - x(bins[0].x0) - 1)
.attr("height", function(d) { return height - y(d.length); });

bar.append("text")
.attr("dy", ".75em")
.attr("y", 6)
.attr("x", (x(bins[0].x1) - x(bins[0].x0)) / 2)
.attr("text-anchor", "middle")
.text(function(d) { return formatCount(d.length); });

g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));

</script>

PS:您必须调整矩形的水平位置。

关于javascript - 直方图的 X 轴值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49273326/

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