Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
128
rated 0 times [  133] [ 5]  / answers: 1 / hits: 28290  / 11 Years ago, fri, april 19, 2013, 12:00:00

I'm currently using D3.js and have come across a problem that I just can't seem to solve.



I have a CSV that has a column named Set and a column named Year. I want to pull the values from these columns and use them as class names. This is what I currently have...



var circle = svg.selectAll(circle)
.data(data)
.enter()
.append(circle)
.attr(class, function(d) {
if (d[Set] == 1)
{
return set-1;
}
if (d[Set] == 2)
{
return set-2;
}
});


This works perfectly fine and gives each data-point a class name. When I try the following however, the Set class names are over written by the Year class names.



var circle = svg.selectAll(circle)
.data(data)
.enter()
.append(circle)
.attr(class, function(d) {
if (d[Set] == 1)
{
return set-1;
}
if (d[Set] == 2)
{
return set-2;
}
.attr(class, function(d) {
if (d[Year] == 2012)
{
return 2012;
}
if (d[Year] == 2013)
{
return 2013;
}
});


How can this code be rectified so that it adds on additional class names as opposed to over-writing them.



Hope someone can help.


More From » html

 Answers
2

You just want a single function that does both things don't you. Something along these lines perhaps...



var circle = svg.selectAll(circle)
.data(data)
.enter()
.append(circle)
.attr(class, function(d) {
var c = ;
if (d[Set] == 1)
{
c = set-1;
}
if (d[Set] == 2)
{
c = set-2;
}
if (d[Year] == 2012)
{
c += 2012;
}
if (d[Year] == 2013)
{
c += 2013;
}
return c;
});

[#78779] Thursday, April 18, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
arlethjeanettep

Total Points: 506
Total Questions: 96
Total Answers: 79

Location: Liberia
Member since Tue, Mar 14, 2023
1 Year ago
;