mardi 21 juin 2016

Undefined object in d3.js sort function

I am using the follow JavaScript to try to sort a bar chart in d3.js, however whenever this code is run, the parameter b in the sortItems function is always undefined and everything fails.

This is the error produced:

Uncaught TypeError: Cannot read property 'data' of undefined

var sortOrder = false;
$('#sort').on('click', function() {

  sortOrder = !sortOrder;
  sortItems = function(a, b) {
    if (sortOrder) {
      return a.data.value - b.data.value;
    }
    return b.data.value - a.data.value;
  };

  charts.chart.selectAll("rect")
    .sort(sortItems)
    .transition()
    .delay(function(d, i) {
      return i * 50;
    })
    .duration(1000)
    .attr("x", function(d, i) {
      return xScale(i);
    });
}

Any ideas what I'm doing wrong?




Aucun commentaire:

Enregistrer un commentaire