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))
javascript arrays object d3.js
add a comment |
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))
javascript arrays object d3.js
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 whatselectAlldoes
– rioV8
Nov 10 at 16:52
add a comment |
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))
javascript arrays object d3.js
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
javascript arrays object d3.js
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 whatselectAlldoes
– rioV8
Nov 10 at 16:52
add a comment |
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 whatselectAlldoes
– 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
add a comment |
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
)
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 groupslet groups = svg.selectAll("g") .data(dataset) .enter() .append("g")is a selection ofgcarrying 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
add a comment |
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
)
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 groupslet groups = svg.selectAll("g") .data(dataset) .enter() .append("g")is a selection ofgcarrying 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
add a comment |
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
)
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 groupslet groups = svg.selectAll("g") .data(dataset) .enter() .append("g")is a selection ofgcarrying 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
add a comment |
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
)
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
)
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 groupslet groups = svg.selectAll("g") .data(dataset) .enter() .append("g")is a selection ofgcarrying 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
add a comment |
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 groupslet groups = svg.selectAll("g") .data(dataset) .enter() .append("g")is a selection ofgcarrying 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
add a comment |
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%2f53240908%2fd3-path-line-stroke-width-with-if-statement-ternary-operator%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
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
selectAlldoes– rioV8
Nov 10 at 16:52