Unexpected value NaN parsing y attribute

北慕城南 提交于 2020-01-04 09:12:10

问题


I am finding the error somewhere here:

d3.csv("Sales Export Friendly 2-14-2017.csv", function(data) {      
    var sales = []
    sales = data.map(function(d) { return [ +d["BookingID"], +d["Total Paid"] ]; });

    var x = d3.scaleBand()
        .domain(sales.map(function(sale){ return sale.bookingID; }))
        .range([0, width])
        .paddingInner([0.1])

    var y = d3.scaleLinear()
        .range([height, 0])
        .domain([0, d3.max(sales.map(function(sale){ return sale['Total Paid']; }))]);

I believe it's something to do with the first mapping, which returns me an array of numeric arrays, I think.


回答1:


As you know, sales is an array of arrays, it contains no objects.

That being said, it should be:

x.domain(sales.map(function(sale){ return sale[0]; }));
//index of the element in the inner array------^

And:

y.domain([0, d3.max(sales, function(sale){ return sale[1];})]);
//index of the element in the inner array--------------^

Here is a demo with this bogus data:

bookingID,totalPaid
1,23
2,19
3,42
4,61
5,22

var data = d3.csvParse(d3.select("#csv").text());

var sales = data.map(function(d) { return [ +d.bookingID, +d.totalPaid ]; });

var x = d3.scaleBand()
	.domain(sales.map(function(sale){ return sale[0]; }));
	
var y = d3.scaleLinear()
	.domain([0, d3.max(sales, function(sale){ return sale[1];})]);

console.log("x scale domain: " + x.domain());
console.log("y scale domain: " + y.domain());
pre{
  display:none;
}
<script src="https://d3js.org/d3.v4.min.js"></script>
<pre id="csv">bookingID,totalPaid
1,23
2,19
3,42
4,61
5,22</pre>


来源:https://stackoverflow.com/questions/42291605/unexpected-value-nan-parsing-y-attribute

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!