d3 path d = MNaN,NaNLNaN,NaN
up vote
1
down vote
favorite
Trying to build a linechart (multiple lines). Initial data has been an array of object such as:
[{
2010: 8236.082,
countryName: "Afghanistan"
}]
Each line required an array of x/y pairs [[x,y],[x,y]]
. My x
and y
are year
and amount
of emissions. This means I had to restructure my data it to make it look like this:
[
{
country: "Afganistan",
emissions: [
{ year: 2019, amount: 8236.082 }
]
}
]
After data munging I'm stuck with path d = MNaN,NaNLNaN,NaN. What am I doing wrong here?
Codepen
//Define full width, full height and margins
let fullWidth = 600;
let fullHeight = 700;
let margin = {
top: 20,
left: 70,
bottom: 100,
right: 10
}
//Define line chart with and height
let width = fullWidth - margin.left - margin.right;
let height = fullHeight - margin.top - margin.bottom;
//Define x and y scale range
let xScale = d3.scaleLinear()
.range([0, width])
let yScale = d3.scaleLinear()
.range([0, height])
//Draw svg
let svg = d3.select("body")
.attr("width", fullWidth)
.attr("height", fullHeight)
.append("svg")
.append("g")
d3.json("https://api.myjson.com/bins/izmg6").then(data => {
console.log(data);
//Structure data so should be an array of arrays etc [[x,y], [x,y], [x,y]]
let years = d3.keys(data[0]).slice(0, 50);
console.log(years);
let dataset = ;
data.forEach((d, i) => {
let myEmissions = ;
years.forEach(y => {
if (d[y]) {
myEmissions.push({
year: y,
amount: d[y]
})
}
})
dataset.push({
country: d.countryName,
emissions: myEmissions
});
})
console.log(dataset);
//Define x and y domain
xScale
.domain(d3.extent(years))
yScale
.domain([d3.max(dataset, d =>
d3.max(d.emissions, d =>
+d.amount)), 0])
//Generate line
let line = d3.line()
.x(d => {
xScale(d.year);
})
.y(d => {
yScale(d.amount);
});
let groups = d3.selectAll("g")
.data(dataset)
.enter()
.append("g")
groups.append("title")
.text(d => d.country)
groups.selectAll("path")
.data(d => [d.emissions])
.enter()
.append("path")
.attr("d", line)
.attr("class", line)
}).catch(error => console.log(error))
javascript arrays object d3.js
add a comment |
up vote
1
down vote
favorite
Trying to build a linechart (multiple lines). Initial data has been an array of object such as:
[{
2010: 8236.082,
countryName: "Afghanistan"
}]
Each line required an array of x/y pairs [[x,y],[x,y]]
. My x
and y
are year
and amount
of emissions. This means I had to restructure my data it to make it look like this:
[
{
country: "Afganistan",
emissions: [
{ year: 2019, amount: 8236.082 }
]
}
]
After data munging I'm stuck with path d = MNaN,NaNLNaN,NaN. What am I doing wrong here?
Codepen
//Define full width, full height and margins
let fullWidth = 600;
let fullHeight = 700;
let margin = {
top: 20,
left: 70,
bottom: 100,
right: 10
}
//Define line chart with and height
let width = fullWidth - margin.left - margin.right;
let height = fullHeight - margin.top - margin.bottom;
//Define x and y scale range
let xScale = d3.scaleLinear()
.range([0, width])
let yScale = d3.scaleLinear()
.range([0, height])
//Draw svg
let svg = d3.select("body")
.attr("width", fullWidth)
.attr("height", fullHeight)
.append("svg")
.append("g")
d3.json("https://api.myjson.com/bins/izmg6").then(data => {
console.log(data);
//Structure data so should be an array of arrays etc [[x,y], [x,y], [x,y]]
let years = d3.keys(data[0]).slice(0, 50);
console.log(years);
let dataset = ;
data.forEach((d, i) => {
let myEmissions = ;
years.forEach(y => {
if (d[y]) {
myEmissions.push({
year: y,
amount: d[y]
})
}
})
dataset.push({
country: d.countryName,
emissions: myEmissions
});
})
console.log(dataset);
//Define x and y domain
xScale
.domain(d3.extent(years))
yScale
.domain([d3.max(dataset, d =>
d3.max(d.emissions, d =>
+d.amount)), 0])
//Generate line
let line = d3.line()
.x(d => {
xScale(d.year);
})
.y(d => {
yScale(d.amount);
});
let groups = d3.selectAll("g")
.data(dataset)
.enter()
.append("g")
groups.append("title")
.text(d => d.country)
groups.selectAll("path")
.data(d => [d.emissions])
.enter()
.append("path")
.attr("d", line)
.attr("class", line)
}).catch(error => console.log(error))
javascript arrays object d3.js
Where are you seeing a bunch of NaN values?
– Amy
Nov 8 at 14:35
@Amy If you go to the codepen and inspect the output window, the SVG consists of groups of paths with NaNs.
– Peter Collingridge
Nov 8 at 14:46
One thing, unrelated, it looks like your groups of paths are being appended outside of your SVG.
– Peter Collingridge
Nov 8 at 14:51
@PeterCollingridge Ah, it took a bit of searching but I finally see them. Thanks.document.querySelectorAll("g > path")
– Amy
Nov 8 at 14:54
@PeterCollingridge thanks has to be groups = svg.selectAll("g")
– Edgar Kiljak
Nov 8 at 14:55
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Trying to build a linechart (multiple lines). Initial data has been an array of object such as:
[{
2010: 8236.082,
countryName: "Afghanistan"
}]
Each line required an array of x/y pairs [[x,y],[x,y]]
. My x
and y
are year
and amount
of emissions. This means I had to restructure my data it to make it look like this:
[
{
country: "Afganistan",
emissions: [
{ year: 2019, amount: 8236.082 }
]
}
]
After data munging I'm stuck with path d = MNaN,NaNLNaN,NaN. What am I doing wrong here?
Codepen
//Define full width, full height and margins
let fullWidth = 600;
let fullHeight = 700;
let margin = {
top: 20,
left: 70,
bottom: 100,
right: 10
}
//Define line chart with and height
let width = fullWidth - margin.left - margin.right;
let height = fullHeight - margin.top - margin.bottom;
//Define x and y scale range
let xScale = d3.scaleLinear()
.range([0, width])
let yScale = d3.scaleLinear()
.range([0, height])
//Draw svg
let svg = d3.select("body")
.attr("width", fullWidth)
.attr("height", fullHeight)
.append("svg")
.append("g")
d3.json("https://api.myjson.com/bins/izmg6").then(data => {
console.log(data);
//Structure data so should be an array of arrays etc [[x,y], [x,y], [x,y]]
let years = d3.keys(data[0]).slice(0, 50);
console.log(years);
let dataset = ;
data.forEach((d, i) => {
let myEmissions = ;
years.forEach(y => {
if (d[y]) {
myEmissions.push({
year: y,
amount: d[y]
})
}
})
dataset.push({
country: d.countryName,
emissions: myEmissions
});
})
console.log(dataset);
//Define x and y domain
xScale
.domain(d3.extent(years))
yScale
.domain([d3.max(dataset, d =>
d3.max(d.emissions, d =>
+d.amount)), 0])
//Generate line
let line = d3.line()
.x(d => {
xScale(d.year);
})
.y(d => {
yScale(d.amount);
});
let groups = d3.selectAll("g")
.data(dataset)
.enter()
.append("g")
groups.append("title")
.text(d => d.country)
groups.selectAll("path")
.data(d => [d.emissions])
.enter()
.append("path")
.attr("d", line)
.attr("class", line)
}).catch(error => console.log(error))
javascript arrays object d3.js
Trying to build a linechart (multiple lines). Initial data has been an array of object such as:
[{
2010: 8236.082,
countryName: "Afghanistan"
}]
Each line required an array of x/y pairs [[x,y],[x,y]]
. My x
and y
are year
and amount
of emissions. This means I had to restructure my data it to make it look like this:
[
{
country: "Afganistan",
emissions: [
{ year: 2019, amount: 8236.082 }
]
}
]
After data munging I'm stuck with path d = MNaN,NaNLNaN,NaN. What am I doing wrong here?
Codepen
//Define full width, full height and margins
let fullWidth = 600;
let fullHeight = 700;
let margin = {
top: 20,
left: 70,
bottom: 100,
right: 10
}
//Define line chart with and height
let width = fullWidth - margin.left - margin.right;
let height = fullHeight - margin.top - margin.bottom;
//Define x and y scale range
let xScale = d3.scaleLinear()
.range([0, width])
let yScale = d3.scaleLinear()
.range([0, height])
//Draw svg
let svg = d3.select("body")
.attr("width", fullWidth)
.attr("height", fullHeight)
.append("svg")
.append("g")
d3.json("https://api.myjson.com/bins/izmg6").then(data => {
console.log(data);
//Structure data so should be an array of arrays etc [[x,y], [x,y], [x,y]]
let years = d3.keys(data[0]).slice(0, 50);
console.log(years);
let dataset = ;
data.forEach((d, i) => {
let myEmissions = ;
years.forEach(y => {
if (d[y]) {
myEmissions.push({
year: y,
amount: d[y]
})
}
})
dataset.push({
country: d.countryName,
emissions: myEmissions
});
})
console.log(dataset);
//Define x and y domain
xScale
.domain(d3.extent(years))
yScale
.domain([d3.max(dataset, d =>
d3.max(d.emissions, d =>
+d.amount)), 0])
//Generate line
let line = d3.line()
.x(d => {
xScale(d.year);
})
.y(d => {
yScale(d.amount);
});
let groups = d3.selectAll("g")
.data(dataset)
.enter()
.append("g")
groups.append("title")
.text(d => d.country)
groups.selectAll("path")
.data(d => [d.emissions])
.enter()
.append("path")
.attr("d", line)
.attr("class", line)
}).catch(error => console.log(error))
javascript arrays object d3.js
javascript arrays object d3.js
edited Nov 8 at 14:27
Amy
21.3k1773131
21.3k1773131
asked Nov 8 at 14:15
Edgar Kiljak
323112
323112
Where are you seeing a bunch of NaN values?
– Amy
Nov 8 at 14:35
@Amy If you go to the codepen and inspect the output window, the SVG consists of groups of paths with NaNs.
– Peter Collingridge
Nov 8 at 14:46
One thing, unrelated, it looks like your groups of paths are being appended outside of your SVG.
– Peter Collingridge
Nov 8 at 14:51
@PeterCollingridge Ah, it took a bit of searching but I finally see them. Thanks.document.querySelectorAll("g > path")
– Amy
Nov 8 at 14:54
@PeterCollingridge thanks has to be groups = svg.selectAll("g")
– Edgar Kiljak
Nov 8 at 14:55
add a comment |
Where are you seeing a bunch of NaN values?
– Amy
Nov 8 at 14:35
@Amy If you go to the codepen and inspect the output window, the SVG consists of groups of paths with NaNs.
– Peter Collingridge
Nov 8 at 14:46
One thing, unrelated, it looks like your groups of paths are being appended outside of your SVG.
– Peter Collingridge
Nov 8 at 14:51
@PeterCollingridge Ah, it took a bit of searching but I finally see them. Thanks.document.querySelectorAll("g > path")
– Amy
Nov 8 at 14:54
@PeterCollingridge thanks has to be groups = svg.selectAll("g")
– Edgar Kiljak
Nov 8 at 14:55
Where are you seeing a bunch of NaN values?
– Amy
Nov 8 at 14:35
Where are you seeing a bunch of NaN values?
– Amy
Nov 8 at 14:35
@Amy If you go to the codepen and inspect the output window, the SVG consists of groups of paths with NaNs.
– Peter Collingridge
Nov 8 at 14:46
@Amy If you go to the codepen and inspect the output window, the SVG consists of groups of paths with NaNs.
– Peter Collingridge
Nov 8 at 14:46
One thing, unrelated, it looks like your groups of paths are being appended outside of your SVG.
– Peter Collingridge
Nov 8 at 14:51
One thing, unrelated, it looks like your groups of paths are being appended outside of your SVG.
– Peter Collingridge
Nov 8 at 14:51
@PeterCollingridge Ah, it took a bit of searching but I finally see them. Thanks.
document.querySelectorAll("g > path")
– Amy
Nov 8 at 14:54
@PeterCollingridge Ah, it took a bit of searching but I finally see them. Thanks.
document.querySelectorAll("g > path")
– Amy
Nov 8 at 14:54
@PeterCollingridge thanks has to be groups = svg.selectAll("g")
– Edgar Kiljak
Nov 8 at 14:55
@PeterCollingridge thanks has to be groups = svg.selectAll("g")
– Edgar Kiljak
Nov 8 at 14:55
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
The main problem of the NaN is
let line = d3.line()
.x(d => xScale(d.year) )
.y(d => yScale(d.amount) );
But that is not the only problem of the chart.
Thanks d path now has values. Would you please be able to guide me what else is wrong?
– Edgar Kiljak
Nov 8 at 15:08
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
The main problem of the NaN is
let line = d3.line()
.x(d => xScale(d.year) )
.y(d => yScale(d.amount) );
But that is not the only problem of the chart.
Thanks d path now has values. Would you please be able to guide me what else is wrong?
– Edgar Kiljak
Nov 8 at 15:08
add a comment |
up vote
0
down vote
accepted
The main problem of the NaN is
let line = d3.line()
.x(d => xScale(d.year) )
.y(d => yScale(d.amount) );
But that is not the only problem of the chart.
Thanks d path now has values. Would you please be able to guide me what else is wrong?
– Edgar Kiljak
Nov 8 at 15:08
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
The main problem of the NaN is
let line = d3.line()
.x(d => xScale(d.year) )
.y(d => yScale(d.amount) );
But that is not the only problem of the chart.
The main problem of the NaN is
let line = d3.line()
.x(d => xScale(d.year) )
.y(d => yScale(d.amount) );
But that is not the only problem of the chart.
answered Nov 8 at 14:59
rioV8
3,7782211
3,7782211
Thanks d path now has values. Would you please be able to guide me what else is wrong?
– Edgar Kiljak
Nov 8 at 15:08
add a comment |
Thanks d path now has values. Would you please be able to guide me what else is wrong?
– Edgar Kiljak
Nov 8 at 15:08
Thanks d path now has values. Would you please be able to guide me what else is wrong?
– Edgar Kiljak
Nov 8 at 15:08
Thanks d path now has values. Would you please be able to guide me what else is wrong?
– Edgar Kiljak
Nov 8 at 15:08
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53209576%2fd3-path-d-mnan-nanlnan-nan%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Where are you seeing a bunch of NaN values?
– Amy
Nov 8 at 14:35
@Amy If you go to the codepen and inspect the output window, the SVG consists of groups of paths with NaNs.
– Peter Collingridge
Nov 8 at 14:46
One thing, unrelated, it looks like your groups of paths are being appended outside of your SVG.
– Peter Collingridge
Nov 8 at 14:51
@PeterCollingridge Ah, it took a bit of searching but I finally see them. Thanks.
document.querySelectorAll("g > path")
– Amy
Nov 8 at 14:54
@PeterCollingridge thanks has to be groups = svg.selectAll("g")
– Edgar Kiljak
Nov 8 at 14:55