After using data().enter().append() in D3.js, individual columns/values in a dataset can be retrieved by simply using d.valuename. But I want to find the maximum value in a CSV column for a linear scale. Since the scale is not preceeded by any data calls, I’m really uncertain on how to specify the right column from which to find the maximum value.
This is my failed attempt. What do I substitute d.column1 with?
d3.csv("file.csv", function (data) {
var x = d3.scale.linear()
.domain([0, d3.max(d.column1)])
.range([0, 960]);
EDIT:
Ok, I got a bit further by looking at a similar example. I don’t understand why my code is not working now.
d3.csv("file.csv", function (data) {
data.forEach(function(d) {
d.column1 = +d.column1;
});
var max = d3.max(data, function(d) { return d.column1; });
var x = d3.scale.linear()
.domain([0, max])
.range([0, 960]);
EDIT #2:
In case the problem derives from the data itself, here’s a snippet. I have tried removing all quotes, and also keeping the quotes for everything but the top row, but nothing works.
"product", "price"
"bread",20
"meat",100
"meat, sausage",200
EDIT #3:
Ok, I promise, last edit. But just to be absolutely clear; I’m using the x-scale to determine the width of the bars in a bar chart: .attr("width", x) and the scale returns NaN.
I just realized what the problem was. No wonder we couldn’t figure this one out together. All the code above is fine. It’s just that when I called the x-scale to determine the width of my bars, I didn’t specify which column in the data would be input to the scale. I had
But now I changed that row to:
Thanks for your help and patience.