score:31

the general rule when using a data-join is that you want a one-to-one mapping from data to elements. so, if you have two series in your scatterplot, you’ll want two container elements (such as g elements) to represent the series. since you currently have only one `data` array, you’ll also want to use array.map to convert the data representation into two parallel arrays with the same representation. this way, you don’t have to duplicate code for each series.

say your data was represented in a csv file with one column for the x-values, and multiple other columns for the y-values of each series:

``````x,y1,y2
5,90,22
25,30,25
45,50,80
65,55,9
85,25,95
``````

if you want the code to be completely generic, you first need to compute the series’ names, such as `["y1", "y2"]`. (if you added a third column to the csv file, it might be `["y1", "y2", "y3"]`.) you can compute the names using d3.keys, which extracts the named properties from an object. for example, `d3.keys({foo: 1, bar: 2})` returns `["foo", "bar"]`.

``````// compute the series names ("y1", "y2", etc.) from the loaded csv.
var seriesnames = d3.keys(data[0])
.filter(function(d) { return d !== "x"; })
.sort();
``````

now that you have the series names, you can create an array of arrays of points. the outer array represents the series (of which there are two) and the inner arrays store the data points. you can simultaneously convert the points to a consistent representation (objects with `x` and `y` properties), allowing you to reuse code across series.

``````// map the data to an array of arrays of {x, y} tuples.
var series = seriesnames.map(function(series) {
return data.map(function(d) {
return {x: +d.x, y: +d[series]};
});
});
``````

note this code uses the `+` operator to coerce the csv values to numbers. (csv files are untyped, so they are initially strings.)

now that you’ve mapped your data to a regular format, you can create g elements for each series, and then circle elements within for each point. the resulting svg structure will look like this:

``````<g class="series">
<circle class="point" r="4.5" cx="1" cy="2"/>
<circle class="point" r="4.5" cx="3" cy="2"/>
…
</g>
<g class="series">
<circle class="point" r="4.5" cx="5" cy="4"/>
<circle class="point" r="4.5" cx="7" cy="6"/>
…
</g>
``````

and the corresponding d3 code:

``````// add the points!
svg.selectall(".series")
.data(series)
.enter().append("g")
.attr("class", "series")
.style("fill", function(d, i) { return z(i); })
.selectall(".point")
.data(function(d) { return d; })
.enter().append("circle")
.attr("class", "point")
.attr("r", 4.5)
.attr("cx", function(d) { return x(d.x); })
.attr("cy", function(d) { return y(d.y); });
``````

i’ve also added a bit of code to assign each series a unique color by adding a fill style to the containing g element. there are lots of different ways to do this, of course. (you might want to be more specific about the color for each series, for example.) i’ve also left out the code that computes the domains of your x and y scales (as well as rendering the axes), but if you want to see the entire working example:

score:5

place the two circles for each data point into a single `svg:g` element. this produces a one-to-one mapping for the data to elements but still allows you to show two different points.

``````var nodeenter = vis1.selectall("circle")
.data(dataset)
.enter()
.insert("svg:g");

nodeenter.insert("svg:circle")
.attr("cx", function (d) { return 100 - d.xvar})
.attr("cy", function (d) { return 100 - d.yvar1})
.attr("r", 2)
.style("fill", "green");

nodeenter.insert("svg:circle")
.attr("cx", function (d) { return 100 - d.xvar})
.attr("cy", function (d) { return 100 - d.yvar2})
.attr("r", 2)
.style("fill", "blue");
``````

working jsfiddle.