0

I am currently making a scatterplot matrix in D3. My data point structure looks like this:

      function plot(p) {
      var cell = d3.select(this);

      x.domain(domainByTrait[p.x]);
      y.domain(domainByTrait[p.y]);

      // Data points
      cell.selectAll("circle")
      .data(data)
      .enter().append("circle")
      .attr("cx", function (d) { return x(d[p.x]); })
      .attr("cy", function (d) { return y(d[p.y]); })
      .attr("r", 3)
      .style("fill", function (d) { return color(d.species); });
  }

I also have a javascript function:

      function create(columnsplit) {

      cell = svg.selectAll(".cell")
      .data(cross(traits, traits))
      .enter().append("g")
      .attr("class", "cell")
      .attr("transform", function (d) { return "translate(" + (n - d.i - 1) * size + "," + d.j * size + ")"; })
      .each(plot);

      cell.call(brush);

  }

where plot is assigned. However, I would like to replace the hardcoded d.species with the columnsplit argument that I have in my create function (unless d.species doesn't work like I originally thought). How exactly would I do this?

Here is how I the traits for the datum d are being created, from what I can understand:

domainByTrait = {},
      traits = d3.keys(data[0]).filter(function (d) { return d !== columnsplit; }),
      n = traits.length;

traits.forEach(function (trait) {
              domainByTrait[trait] = d3.extent(data, function (d) { return d[trait]; });
          });

I am using this as my template: http://bl.ocks.org/mbostock/4063663

Thanks

2
  • I would like to replace the hardcoded d.species with the columnsplit argument that I have in my create function What do you mean exactly? Why can't you just do it? Commented Aug 27, 2014 at 23:19
  • I would need to set it to d."whatever column is defined in columnsplit", but because the "." makes it a literal attribute, I can't just go d.columnsplit, because then it would look at the current datum and look for an attribute called columnsplit. Commented Aug 28, 2014 at 15:35

1 Answer 1

1

If I understand, you want to access the property named the value of "columnsplit". To do this you just use the indexer notation for accessing properties:

d[columnsplit]

In other words, the d.species syntax is equivalent to d["species"] (reference).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.