d3 path.line stroke-width with IF statement / ternary operator











up vote
1
down vote

favorite












Trying to change line stroke-width with an IF statement / ternary operator e.g. if d.country === "China" stroke-width: 2. This has to be a path.line attribute so this is what I'm attaching it to after line is called.



I've added countryName to the emissions object, I have also noticed that condition is always FALSE so the stroke-width is 0.5. Why its not TRUE?



Codepen



//Define line chart with and height 
const width = fullWidth - margin.left - margin.right;
const 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])

//Define x and y axis
let xAxis = d3.axisBottom(xScale)
.ticks(15)

let yAxis = d3.axisLeft(yScale)
.ticks(10)


//Draw svg
let svg = d3.select("body")
.append("svg")
.attr("width", fullWidth)
.attr("height", fullHeight)
.append("g")
.attr("transform", "translate(" + 53 + "," + 0 +")");

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({
country: d.countryName,
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, d =>d))

yScale
.domain([d3.max(dataset, d =>
d3.max(d.emissions, d =>
+d.amount)), 0])


//Generate line
let line = d3.line()
.curve(d3.curveBasis)
.x(d =>
xScale(d.year))
.y(d =>
yScale(d.amount));


let groups = svg.selectAll("g")
.data(dataset)
.enter()
.append("g")


groups.append("title")
.text(d => d.country)


groups.selectAll("path")
.data(d => [d.emissions])
.enter()
.append("path").classed("line", true)
.attr("d", line)
.style("stroke-width", d =>
d.country === "China" ? 10 : 0.5
)


}).catch(error => console.log(error))









share|improve this question
























  • there is no need to add country to emissions if you create your path differently
    – rioV8
    Nov 10 at 16:45










  • @rioV8 I'm new to d3. Can you please be more specific?
    – Edgar Kiljak
    Nov 10 at 16:47










  • read carefully what selectAll does
    – rioV8
    Nov 10 at 16:52















up vote
1
down vote

favorite












Trying to change line stroke-width with an IF statement / ternary operator e.g. if d.country === "China" stroke-width: 2. This has to be a path.line attribute so this is what I'm attaching it to after line is called.



I've added countryName to the emissions object, I have also noticed that condition is always FALSE so the stroke-width is 0.5. Why its not TRUE?



Codepen



//Define line chart with and height 
const width = fullWidth - margin.left - margin.right;
const 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])

//Define x and y axis
let xAxis = d3.axisBottom(xScale)
.ticks(15)

let yAxis = d3.axisLeft(yScale)
.ticks(10)


//Draw svg
let svg = d3.select("body")
.append("svg")
.attr("width", fullWidth)
.attr("height", fullHeight)
.append("g")
.attr("transform", "translate(" + 53 + "," + 0 +")");

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({
country: d.countryName,
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, d =>d))

yScale
.domain([d3.max(dataset, d =>
d3.max(d.emissions, d =>
+d.amount)), 0])


//Generate line
let line = d3.line()
.curve(d3.curveBasis)
.x(d =>
xScale(d.year))
.y(d =>
yScale(d.amount));


let groups = svg.selectAll("g")
.data(dataset)
.enter()
.append("g")


groups.append("title")
.text(d => d.country)


groups.selectAll("path")
.data(d => [d.emissions])
.enter()
.append("path").classed("line", true)
.attr("d", line)
.style("stroke-width", d =>
d.country === "China" ? 10 : 0.5
)


}).catch(error => console.log(error))









share|improve this question
























  • there is no need to add country to emissions if you create your path differently
    – rioV8
    Nov 10 at 16:45










  • @rioV8 I'm new to d3. Can you please be more specific?
    – Edgar Kiljak
    Nov 10 at 16:47










  • read carefully what selectAll does
    – rioV8
    Nov 10 at 16:52













up vote
1
down vote

favorite









up vote
1
down vote

favorite











Trying to change line stroke-width with an IF statement / ternary operator e.g. if d.country === "China" stroke-width: 2. This has to be a path.line attribute so this is what I'm attaching it to after line is called.



I've added countryName to the emissions object, I have also noticed that condition is always FALSE so the stroke-width is 0.5. Why its not TRUE?



Codepen



//Define line chart with and height 
const width = fullWidth - margin.left - margin.right;
const 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])

//Define x and y axis
let xAxis = d3.axisBottom(xScale)
.ticks(15)

let yAxis = d3.axisLeft(yScale)
.ticks(10)


//Draw svg
let svg = d3.select("body")
.append("svg")
.attr("width", fullWidth)
.attr("height", fullHeight)
.append("g")
.attr("transform", "translate(" + 53 + "," + 0 +")");

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({
country: d.countryName,
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, d =>d))

yScale
.domain([d3.max(dataset, d =>
d3.max(d.emissions, d =>
+d.amount)), 0])


//Generate line
let line = d3.line()
.curve(d3.curveBasis)
.x(d =>
xScale(d.year))
.y(d =>
yScale(d.amount));


let groups = svg.selectAll("g")
.data(dataset)
.enter()
.append("g")


groups.append("title")
.text(d => d.country)


groups.selectAll("path")
.data(d => [d.emissions])
.enter()
.append("path").classed("line", true)
.attr("d", line)
.style("stroke-width", d =>
d.country === "China" ? 10 : 0.5
)


}).catch(error => console.log(error))









share|improve this question















Trying to change line stroke-width with an IF statement / ternary operator e.g. if d.country === "China" stroke-width: 2. This has to be a path.line attribute so this is what I'm attaching it to after line is called.



I've added countryName to the emissions object, I have also noticed that condition is always FALSE so the stroke-width is 0.5. Why its not TRUE?



Codepen



//Define line chart with and height 
const width = fullWidth - margin.left - margin.right;
const 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])

//Define x and y axis
let xAxis = d3.axisBottom(xScale)
.ticks(15)

let yAxis = d3.axisLeft(yScale)
.ticks(10)


//Draw svg
let svg = d3.select("body")
.append("svg")
.attr("width", fullWidth)
.attr("height", fullHeight)
.append("g")
.attr("transform", "translate(" + 53 + "," + 0 +")");

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({
country: d.countryName,
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, d =>d))

yScale
.domain([d3.max(dataset, d =>
d3.max(d.emissions, d =>
+d.amount)), 0])


//Generate line
let line = d3.line()
.curve(d3.curveBasis)
.x(d =>
xScale(d.year))
.y(d =>
yScale(d.amount));


let groups = svg.selectAll("g")
.data(dataset)
.enter()
.append("g")


groups.append("title")
.text(d => d.country)


groups.selectAll("path")
.data(d => [d.emissions])
.enter()
.append("path").classed("line", true)
.attr("d", line)
.style("stroke-width", d =>
d.country === "China" ? 10 : 0.5
)


}).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 10 at 16:25

























asked Nov 10 at 16:19









Edgar Kiljak

19312




19312












  • there is no need to add country to emissions if you create your path differently
    – rioV8
    Nov 10 at 16:45










  • @rioV8 I'm new to d3. Can you please be more specific?
    – Edgar Kiljak
    Nov 10 at 16:47










  • read carefully what selectAll does
    – rioV8
    Nov 10 at 16:52


















  • there is no need to add country to emissions if you create your path differently
    – rioV8
    Nov 10 at 16:45










  • @rioV8 I'm new to d3. Can you please be more specific?
    – Edgar Kiljak
    Nov 10 at 16:47










  • read carefully what selectAll does
    – rioV8
    Nov 10 at 16:52
















there is no need to add country to emissions if you create your path differently
– rioV8
Nov 10 at 16:45




there is no need to add country to emissions if you create your path differently
– rioV8
Nov 10 at 16:45












@rioV8 I'm new to d3. Can you please be more specific?
– Edgar Kiljak
Nov 10 at 16:47




@rioV8 I'm new to d3. Can you please be more specific?
– Edgar Kiljak
Nov 10 at 16:47












read carefully what selectAll does
– rioV8
Nov 10 at 16:52




read carefully what selectAll does
– rioV8
Nov 10 at 16:52












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










What rioV8 meant is that you already have your group selection, so you just need to use groups to append new elements.



groups is a selection of all your g, it's where you want to append your paths. The same way you're not selecting again to add the titles.



groups
.append("path").classed("line", true)
.attr("d", d=> line(d.emissions))
.style("stroke-width", d =>
d.country === "China" ? 5 : 0.5
)





share|improve this answer





















  • Thank you! But because there's a group with data already it has to be an array for the line function [ d.emissions ]?
    – Edgar Kiljak
    Nov 10 at 20:00










  • because now there is an object with 3 keys and values instead of two that represent x and y value?
    – Edgar Kiljak
    Nov 10 at 20:17






  • 1




    because your groups let groups = svg.selectAll("g") .data(dataset) .enter() .append("g") is a selection of g carrying your dataset, then when you append and use the bound data which is your array of{country, emissions }, then you just need to pass to your line function .attr("d", d=> line(d.emissions))
    – cal_br_mar
    Nov 11 at 1:58













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%2f53240908%2fd3-path-line-stroke-width-with-if-statement-ternary-operator%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
1
down vote



accepted










What rioV8 meant is that you already have your group selection, so you just need to use groups to append new elements.



groups is a selection of all your g, it's where you want to append your paths. The same way you're not selecting again to add the titles.



groups
.append("path").classed("line", true)
.attr("d", d=> line(d.emissions))
.style("stroke-width", d =>
d.country === "China" ? 5 : 0.5
)





share|improve this answer





















  • Thank you! But because there's a group with data already it has to be an array for the line function [ d.emissions ]?
    – Edgar Kiljak
    Nov 10 at 20:00










  • because now there is an object with 3 keys and values instead of two that represent x and y value?
    – Edgar Kiljak
    Nov 10 at 20:17






  • 1




    because your groups let groups = svg.selectAll("g") .data(dataset) .enter() .append("g") is a selection of g carrying your dataset, then when you append and use the bound data which is your array of{country, emissions }, then you just need to pass to your line function .attr("d", d=> line(d.emissions))
    – cal_br_mar
    Nov 11 at 1:58

















up vote
1
down vote



accepted










What rioV8 meant is that you already have your group selection, so you just need to use groups to append new elements.



groups is a selection of all your g, it's where you want to append your paths. The same way you're not selecting again to add the titles.



groups
.append("path").classed("line", true)
.attr("d", d=> line(d.emissions))
.style("stroke-width", d =>
d.country === "China" ? 5 : 0.5
)





share|improve this answer





















  • Thank you! But because there's a group with data already it has to be an array for the line function [ d.emissions ]?
    – Edgar Kiljak
    Nov 10 at 20:00










  • because now there is an object with 3 keys and values instead of two that represent x and y value?
    – Edgar Kiljak
    Nov 10 at 20:17






  • 1




    because your groups let groups = svg.selectAll("g") .data(dataset) .enter() .append("g") is a selection of g carrying your dataset, then when you append and use the bound data which is your array of{country, emissions }, then you just need to pass to your line function .attr("d", d=> line(d.emissions))
    – cal_br_mar
    Nov 11 at 1:58















up vote
1
down vote



accepted







up vote
1
down vote



accepted






What rioV8 meant is that you already have your group selection, so you just need to use groups to append new elements.



groups is a selection of all your g, it's where you want to append your paths. The same way you're not selecting again to add the titles.



groups
.append("path").classed("line", true)
.attr("d", d=> line(d.emissions))
.style("stroke-width", d =>
d.country === "China" ? 5 : 0.5
)





share|improve this answer












What rioV8 meant is that you already have your group selection, so you just need to use groups to append new elements.



groups is a selection of all your g, it's where you want to append your paths. The same way you're not selecting again to add the titles.



groups
.append("path").classed("line", true)
.attr("d", d=> line(d.emissions))
.style("stroke-width", d =>
d.country === "China" ? 5 : 0.5
)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 19:25









cal_br_mar

47625




47625












  • Thank you! But because there's a group with data already it has to be an array for the line function [ d.emissions ]?
    – Edgar Kiljak
    Nov 10 at 20:00










  • because now there is an object with 3 keys and values instead of two that represent x and y value?
    – Edgar Kiljak
    Nov 10 at 20:17






  • 1




    because your groups let groups = svg.selectAll("g") .data(dataset) .enter() .append("g") is a selection of g carrying your dataset, then when you append and use the bound data which is your array of{country, emissions }, then you just need to pass to your line function .attr("d", d=> line(d.emissions))
    – cal_br_mar
    Nov 11 at 1:58




















  • Thank you! But because there's a group with data already it has to be an array for the line function [ d.emissions ]?
    – Edgar Kiljak
    Nov 10 at 20:00










  • because now there is an object with 3 keys and values instead of two that represent x and y value?
    – Edgar Kiljak
    Nov 10 at 20:17






  • 1




    because your groups let groups = svg.selectAll("g") .data(dataset) .enter() .append("g") is a selection of g carrying your dataset, then when you append and use the bound data which is your array of{country, emissions }, then you just need to pass to your line function .attr("d", d=> line(d.emissions))
    – cal_br_mar
    Nov 11 at 1:58


















Thank you! But because there's a group with data already it has to be an array for the line function [ d.emissions ]?
– Edgar Kiljak
Nov 10 at 20:00




Thank you! But because there's a group with data already it has to be an array for the line function [ d.emissions ]?
– Edgar Kiljak
Nov 10 at 20:00












because now there is an object with 3 keys and values instead of two that represent x and y value?
– Edgar Kiljak
Nov 10 at 20:17




because now there is an object with 3 keys and values instead of two that represent x and y value?
– Edgar Kiljak
Nov 10 at 20:17




1




1




because your groups let groups = svg.selectAll("g") .data(dataset) .enter() .append("g") is a selection of g carrying your dataset, then when you append and use the bound data which is your array of{country, emissions }, then you just need to pass to your line function .attr("d", d=> line(d.emissions))
– cal_br_mar
Nov 11 at 1:58






because your groups let groups = svg.selectAll("g") .data(dataset) .enter() .append("g") is a selection of g carrying your dataset, then when you append and use the bound data which is your array of{country, emissions }, then you just need to pass to your line function .attr("d", d=> line(d.emissions))
– cal_br_mar
Nov 11 at 1:58




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240908%2fd3-path-line-stroke-width-with-if-statement-ternary-operator%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







Popular posts from this blog

The Sandy Post

Danny Elfman

Pages that link to "Head v. Amoskeag Manufacturing Co."