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))









share|improve this question
























  • 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















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))









share|improve this question
























  • 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













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))









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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.






share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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.






share|improve this answer





















  • 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















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.






share|improve this answer





















  • 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













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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Hercules Kyvelos

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud