1

For the following lines of code, once I enter my locationArray into the data, the iteration runs for the length of the array, but starts on the final iteration such that i is always = 1 (the array has a length of two nested arrays). Why won't it start with array[0]?

var canvas = d3.select("#map-canvas");
setTimeout(function(){
  console.log(canvas)

  canvas
    .selectAll(".marker")
    .append('svg')
    .attr('width', 400)
    .attr('height', 400)
    .style("position", "relative")
    .style("right", 150)
    .style("bottom", function() {
      return 150;
    })
var marker = canvas.selectAll(".marker")
  console.log(marker)
  marker
  .selectAll('svg')
    .data(locationsArray)
    .enter()
    .append('circle')
    .attr("fill", "magenta")
    .attr("r", 15)
    .attr("cx", 165)
    .attr("cy", 165)
    .attr("class", function(d, i) {
      console.log(d + i)
    if (d[3] >= 30 && d[3] < 60) {
      d[2] == 1 ? result = "smallBadVibe" :
      d[2] == 2 ? result = "smallNeutralVibe" : result = "smallGoodVibe";
      console.log(result)
      return result;
    }    
justin
  • 57
  • 4
  • 1
    See https://stackoverflow.com/questions/19726553/d3-missing-first-value-in-array and section 5 of [this tutorial](https://www.airpair.com/d3.js/getting-started-tutorial). – Lars Kotthoff Jul 05 '15 at 20:47
  • 1
    Yes that solved it thank you Lars. I simply added a .selectAll('div') after selecting the 'svg'. I am having another issue however, in that I really want to run the entire code above for each point of data, and instead I run the entire data for each instance of ".marker", is there a way I could call the data or start the iteration sequence for the data such that each iteration of the data binds that index of the array to that instance of a marker? – justin Jul 05 '15 at 22:10
  • I don't understand what you're saying -- what do you mean by "iteration of the data"? – Lars Kotthoff Jul 05 '15 at 22:15
  • I did read your article btw and it helped clarify some things for me, though it does leave me like for an alternative for indexing that I cannot wrap my head around just yet. – justin Jul 05 '15 at 22:15
  • I guess I mean each time a data point in the array evaluates through the code that follows the data entering. – justin Jul 05 '15 at 22:21
  • So since I enter the data on the child of what I want to start it on, it happens twice, once per each of the parents. What I want is to use the data on the child, but each point of data goes to a separate child from another parent. Only childs. – justin Jul 05 '15 at 22:23
  • Sorry, still no idea. You don't seem to have any hierarchical data, so I don't know where the children and parents come from. I also don't know what you mean by "enter data on the child" or "data point in the array evaluates". Could you provide a complete example of what you're looking for? – Lars Kotthoff Jul 05 '15 at 22:39
  • I have a plunker here you I would appreciate you looking at. Just search for the code and ignore the rest. There are two point with two different sized pulses. I am trying to put one of each pulse on each point. Thanks Lars. http://plnkr.co/edit/rlGo4qXOeUj4VzjxYv5f?p=preview – justin Jul 05 '15 at 22:59
  • What do you mean by one of each pulse on each point? Just one pulse per point? – Lars Kotthoff Jul 05 '15 at 23:04
  • yes. if you look closely there are two rings emanating at each dot expanding at the same speed, to different sizes(one bigger one smaller) and then repeating. They are actually two different functions. I am trying to get one on one point and one on the other. – justin Jul 05 '15 at 23:20
  • Right. I would make what pulse you want part of the data so that you can set the corresponding class immediately instead of through a series of conditions (which is likely where the problem lies). – Lars Kotthoff Jul 06 '15 at 00:40
  • Thanks for your time and help Lars! – justin Jul 06 '15 at 00:47
  • For now I did a work around since I need it to dynamically set the values and am still too noob to implement those things in time to present. If you're interested you can check out this plunker at lines 390-399, where I setup the workaround and then I set the if() at 431, and unshift() during class assignments or lacks thereof. When I restructure it I will figure out a way to do it like you suggested. Thanks again for all your help. http://plnkr.co/edit/rlGo4qXOeUj4VzjxYv5f?p=preview – justin Jul 06 '15 at 01:27

0 Answers0