0

I have a trouble executing the following snippet. It is callback code for a larger python Bokeh plotting. Since my experience with javascript is limited I was drawing from this question on how to loop over an array in javascript.

This is the modified objects2 = ColumnDataSource(data=dict(x=[],y1=[],y2=[],y3=[], y4=[]))

  var inds = cb_obj.get('selected')['1d'].indices; // Obtains column numbers
  var d1 = m1.get('data'); // Obtains matrix with numbered columns that correspond to inds
  var d2 = s2.get('data'); // Plotted data columns that get modified according to inds
  var ys = ['y1','y2','y3','y4']; // Array of names of columns in d2/s2
  var j, y, select= ys.slice(0,inds.length+1), // Limit number of columns to number of inds

  //This piece is from the linked question -  a loop through select (e.g only 'y1' and 'y2')
  var len = select.length;
  for(j=0; j<len; ++j) {
    if (j in select) {
      y = selected[j]; 
      // For each selected name of a column
      d2[y] = [];  // Erase current value
      // Fill the column from d1  (12 is just the length of the column)
      for (var i = 0; i < 12; i++) {
        d2[y].push(d1[inds[j]][i]),
      }
    }
  }
  s2.trigger('change');

The second loop was working just fine for a predefined number of y1,y2, when only two indices were passed to it. But my goal is to make the selection size dynamic i.e for a varying number of indices like [1,5,65,76] or [1,8] which is passed down from outside to inds, the code is to fill up appropriate number of ys so either y1,y2,y3,y4 or just y1,y2 with data from d1/m1.

From what I can tell the code "should" be doing just that , but I am obviously not seeing something.

For comparison here is the previous version of code that worked, but was limited to exactly two indices.

  var inds = cb_obj.get('selected')['1d'].indices;
  var d1 = m1.get('data'); 
  var d2 = s2.get('data');
  d2['y'] = []
  d2['y2'] = []
  for (i = 0; i < 12; i++) {
    d2['y1'].push(d1[inds['0']][i]),
    d2['y2'].push(d1[inds['1']][i])
  }
  s2.trigger('change');

Here is the whole thing (Python) with sample data for better visualization.

Community
  • 1
  • 1
Andrzej Novák
  • 136
  • 1
  • 8
  • Side-note: you didn't define the `i` variable. You should have `var i` somewhere. – Matt Browne Jul 18 '15 at 16:05
  • I'm not sure what you are trying to achieve here, please define dynamic in your context, because as I went through the code, I thought it was already. Defining your data structure would help a lot too, I guess. (like a preview of what is displayed in console or something else, I kinda struggle visualizing it). – axelduch Jul 18 '15 at 16:08
  • It would be useful to create a demo at http://jsfiddle.net/ with sample data to be able to review the code. – Sotiris Jul 18 '15 at 16:08
  • You have no `selected` variable and we don't really know what you're asking about. Using `12` is obviously not dynamic. We don't need a jsFiddle demo. Just clearly describe where you're confused. –  Jul 18 '15 at 16:20
  • @squint I tried to address the problems in the edit. – Andrzej Novák Jul 18 '15 at 17:33

1 Answers1

0

I figured it out. There was an extra comma at the end of this line

d2[y].push(d1[inds[j]][i])
Andrzej Novák
  • 136
  • 1
  • 8