简体   繁体   English

使用 d3 的条形图

[英]Bar chart using d3

I've got my d3 code and it's displaying no errors in the console but it's also not showing my bar chart.我有我的 d3 代码,它在控制台中没有显示错误,但它也没有显示我的条形图。 I'm a bit lost as to where I'm going wrong since I'm not getting any errors and it is reading in my data.由于我没有收到任何错误并且它正在读取我的数据,因此我对我出错的地方有点迷茫。 Any guidance would be appreciated.任何指导将不胜感激。

async function drawBars() {
//setting the margin of svg
const margin = {top: 50, right: 50, bottom: 50, left: 50};

//set the width and heigh using the current width and height of the div
const width = 600 - margin.left - margin.right;
const height = 600 - margin.top - margin.bottom;

//create svg and append to visualisation div
const svg = d3.select("#visualisation").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 + ")");

//set the ranges for the scales
const x = d3.scaleBand()
.range([0, width]);

const y = d3.scaleLinear()
.range([height, 0]);

const data = await d3.csv("./../remote_work.csv")
console.log(data);

//scale the range of the data in the domains
x.domain(data.map(function(d) {
return d.Benefits; 
}));
y.domain([0, d3.max(data, function(d) {
return d.Percentage;
})]);

svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) {
    return x(d.Benefits);
})
.attr("width", x.bandwidth())
.attr("y", function(d) { 
    return y(d.Percentage);
})
.attr("height", function(d) {
    return height - y(d.Percentage);
});

//add the axis
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));

svg.append("g")
.call(d3.axisLeft(y));

}
drawBars()

You need to cast the d.Percentage to a number using either Number(d.Percentage) or +d.Percentage .您需要使用Number(d.Percentage)+d.Percentaged.Percentage转换为数字。

This is because the CSV file reader doesn't know whether any part of it is a number, whether it is a string that just happens to be made up completely of numbers, or whether this is the only value that is also a number, and the same column in another row is actually a string.这是因为 CSV 文件阅读器不知道它的任何部分是否是数字,是否是恰好完全由数字组成的字符串,或者这是否是唯一一个也是数字的值,并且另一行中的同一列实际上是一个字符串。

That is why it doesn't take any chances and just treats everything as a string.这就是为什么它不冒任何风险,只是将所有内容都视为字符串。 This means that it's up to you to make sure the data is of the right type.这意味着您需要确保数据的类型正确。

I'd do this by changing your code to something like this:我会通过将您的代码更改为以下内容来做到这一点:

const data = (await d3.csv(...).then(
  rows => rows.map(d => ({
    Benefits: d.Benefits,
    Percentage: Number(d.Percentage)
  })));

My working snippet with generated data:我的工作片段与生成的数据:

 const data = ["Lease car", "401(k)", "Flexible hours"] .map(v => ({ Benefits: v, Percentage: Math.random().toString() })) .map(d => ({ Benefits: d.Benefits, Percentage: Number(d.Percentage) })); function drawBars() { //setting the margin of svg const margin = { top: 50, right: 50, bottom: 50, left: 50 }; //set the width and heigh using the current width and height of the div const width = 600 - margin.left - margin.right; const height = 600 - margin.top - margin.bottom; //create svg and append to visualisation div const svg = d3.select("#visualisation").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 + ")"); //set the ranges for the scales const x = d3.scaleBand() .range([0, width]); const y = d3.scaleLinear() .range([height, 0]); //scale the range of the data in the domains x.domain(data.map(function(d) { return d.Benefits; })); y.domain([0, d3.max(data, function(d) { return d.Percentage; })]); svg.selectAll(".bar") .data(data) .enter().append("rect") .attr("class", "bar") .attr("x", function(d) { return x(d.Benefits); }) .attr("width", x.bandwidth()) .attr("y", function(d) { return y(d.Percentage); }) .attr("height", function(d) { return height - y(d.Percentage); }); //add the axis svg.append("g") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)); svg.append("g") .call(d3.axisLeft(y)); } drawBars()
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script> <div id="visualisation"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM