1

I am new to javascript and was trying to plot a scatterplot with multiple symbols. I came across this page.

  1. What's the difference between symbolTypes['circle'] and symbolTypes.circle() in the solution below? The latter works while the former throws an error.
  2. If "symbolTypes" is a dictionary, shouldn't we use symbolTypes['circle'] ?

They use sort of a dictionary as a symbol generator in the accepted solution using

// symbol generators
var symbolTypes = {
    "triangleDown": d3.svg.symbol().type("triangle-down"),
    "circle": d3.svg.symbol().type("circle")
};

Then in the later part of the solution the author uses:

svg.selectAll("path")
.data(dataSet)
.enter().append("path")
.attr("class", "dot")
// position it, can't use x/y on path, so translate it
.attr("transform", function(d) { 
    return "translate(" + (x(d.hour) + (Math.random() * 12 - 6)) + "," +  y(d.yval) + ")"; 
})
// assign d from our symbols
.attr("d", function(d,i){
    if (d.bar === "0") // circle if bar === 0
        return symbolTypes.circle();
    else
        return symbolTypes.triangleDown();
})
  1. symbolTypes['circle'] --> I get an error using this to bind circle symbol with my data using attr().
  2. symbolTypes.circle() --> Using this, I can add circle as an attribute to the path element.
patro
  • 111
  • 2
  • 10
  • 1
    This could help: https://stackoverflow.com/q/3246928/5385381, particularly the answer from BenAlabaster – ksav Sep 28 '18 at 14:46
  • 1
    Try again with `symbolTypes['circle']()` – ksav Sep 28 '18 at 14:54
  • 1
    That did work. :) Thanks! – patro Sep 28 '18 at 15:31
  • 1
    Great. Be sure to have a read of the linked question, as it will do a good job of explaining the difference between `symbolTypes.circle` and `symbolTypes.circle()` – ksav Sep 28 '18 at 17:45

0 Answers0