问题
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