gpt4 book ai didi

javascript - d3js v5 x 轴条形图

转载 作者:行者123 更新时间:2023-11-29 20:53:02 25 4
gpt4 key购买 nike

我尝试了很多数据集,但不知道为什么,data_histo.csv 有问题。 x 轴似乎是反向的,然后无法显示条形图。使用 data_histo2.csvdata_histo3.csv,一切都很好。

最好有解释!

<!DOCTYPE html>
<html>

<head>
<script src="https://unpkg.com/d3@5.0.0/dist/d3.min.js"></script>
</head>

<body>


<svg class="histogramme" width="960" height="600"></svg>

<script>

//select
let svgHisto = d3.select(".histogramme")

//dimension
let margin = {top: 20, right: 10, bottom: 20, left: 80}
let width = +svgHisto.attr("width") - margin.left - margin.right
let height = +svgHisto.attr("height") - margin.top - margin.bottom;

let g1 = svgHisto.append("g")
.attr("transform",`translate(${margin.left}, ${margin.top})`);

//data
d3.csv("data_histo.csv").then(function(data) {
console.log(data);

//define x and y axis
let x = d3.scaleLinear();
let y = d3.scaleBand();

x.domain(d3.extent(data, function(d) { return d.value0; })).nice()
.rangeRound([0, width]);
y.domain(data.map(function(d) { return d.libreg; }))
.rangeRound([0, height]).padding(0.1);

//add x axis
g1.append("g")
.attr("class", "axis x_axis")
.attr("transform",`translate(0,${height})`)
.call(d3.axisBottom(x));

//add y axis
g1.append("g")
.attr("class", "axis y_axis")
.call(d3.axisLeft(y));

//bar chart
g1.selectAll(".bar1")
.data(data)
.enter().append("rect")
.attr("class", "bar bar1")
.attr("x", function(d) {return x(Math.min(0,d.value0)); })
.attr("y", function(d) { return y(d.libreg) + 10; })
.attr("width", 0)
.attr("height", y.bandwidth() - 20);

//animate
g1.selectAll(".bar1")
.transition()
.duration(1000)
.delay(function(d,i){return i*100})
.attr("width", function(d) { return x(d.value0); });

});

</script>

</body>

</html>

使用data_histo.csv

"codreg","libreg","year0","value0","year1","value1"
"03","Guyane",2009,4,2014,4.6
"04","La Réunion",2009,8.2,2014,9.8
"11","Île-de-France",2009,12.6,2014,13.9
"01","Guadeloupe",2009,13.3,2014,15.8
"32","Hauts-de-France",2009,14.7,2014,16.1
"02","Martinique",2009,14.7,2014,17.6
"44","Grand Est",2009,16.5,2014,18
"84","Auvergne-Rhône-Alpes",2009,16.8,2014,18.3
"52","Pays de la Loire",2009,17.1,2014,18.6
"28","Normandie",2009,17.2,2014,19
"53","Bretagne",2009,18.5,2014,20.2
"24","Centre-Val de Loire",2009,18.7,2014,20.4
"27","Bourgogne-Franche-Comté",2009,18.8,2014,20.7
"76","Occitanie",2009,19.3,2014,20.8
"93","Provence-Alpes-Côte d''Azur",2009,19.5,2014,21.3
"94","Corse",2009,20.2,2014,21.5
"75","Nouvelle-Aquitaine",2009,20.2,2014,21.8

enter image description here使用 data_histo2.csv

"codreg","libreg","year0","value0","year1","value1"
"84","Auvergne-Rhône-Alpes",2013,39.1,2017,41.7
"27","Bourgogne-Franche-Comté",2013,42.3,2017,44.4
"53","Bretagne",2013,39.6,2017,44.7
"24","Centre-Val de Loire",2013,40.5,2017,46.8
"94","Corse",2013,24.2,2017,30.8
"44","Grand Est",2013,41.3,2017,45.4
"01","Guadeloupe",2013,55.5,2017,56.5
"03","Guyane",2013,33.1,2017,33.2
"32","Hauts-de-France",2013,45.8,2017,47.3
"11","Île-de-France",2013,40.1,2017,42.6
"02","Martinique",2013,52.5,2017,50.2
"28","Normandie",2013,42.6,2017,46.2
"75","Nouvelle-Aquitaine",2013,40,2017,44.4
"76","Occitanie",2013,40.3,2017,43.7
"52","Pays de la Loire",2013,40.6,2017,45.8
"93","Provence-Alpes-Côte d''Azur",2013,38.5,2017,42.6
"04","La Réunion",2013,54.2,2017,54.6
"06","Mayotte",2013,,2017,

enter image description here

这是我的代码:https://plnkr.co/edit/B8qEQ4dlUdRHhkQvzjZx?p=preview

最佳答案

您的代码有两个问题:

D3 将 csv/tsv/dsv 条目解析为文本。因此,当您加载 csv 时,您会得到如下所示的行:

{
"codreg": "03",
"libreg": "Guyane",
"year0": "2009",
"value0": "4",
"year1": "2014",
"value1": "4.6"
}

当您使用范围设置比例时,您并没有使用数字范围。您可以将您的数据强制转换成这样的数字:

data.forEach(function(d) {
d.value0 = +d.value0;
})

其次,如果您这样做,您会注意到条形图的放置有一些奇怪的行为:

enter image description here

您可以看到条形图从绘图区域的左侧开始。原因是您使用的是线性刻度,并像这样绘制条形图的开始:

.attr("x", function(d) {return x(Math.min(0,d.value0)); })

您希望条形图从 x(4) 开始 - 这是用 y 轴标记截距的 x 值所在的位置。相反,您使用的是 x(0),它自然会位于您想要的位置的左侧。这适用于您的第二个示例,因为 x(0) 也恰好是截取 y 轴的 x 值。相反,您可以简单地使用:

.attr("x",0)

这标记了绘图区域的左边缘,这可能是您希望将所有条形图锚定到的位置。

这是一个 fork 的 plunkr .

需要注意的一点是,最短的条形图始终是不可见的:起点和终点相同。这是因为比例范围从最小值到最大值 - 标记绘图区域左边界的最小值是最短条的值。要更改此设置,您可以修改比例尺的域,也许使用 0 作为第一个值,然后使用 d3.max 找到最高值。

关于javascript - d3js v5 x 轴条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50826721/

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