代码流程通过java脚本中的回调

时间:2015-08-04 09:17:34

标签: javascript d3.js underscore.js chord-diagram

-----------------File1.html-------------------------

      <body>
        <div id="tooltip"></div>
        <script src="lib/d3.js"></script>
        <script src="lib/underscore.js"></script>
        <script src="js/mapper.js"></script>
        <script>
        d3.json('data/readme.json', function (error, data) {
            var mpr = chordMpr(data);
            _.each(data, function (elem) {
              mpr.addToMap(name(elem.name))
            })

            mpr.setFilter(function (row, a, b) {
            /*  alert(JSON.stringify(a) +"-------"+JSON.stringify(row)+"-------"+JSON.stringify(b));
                alert(a.name + "--" + row.name + "--" + b.name); */
                return (name(row.name) === a.name)
              })
              .setAccessor(function (recs, a, b) {

                if (!recs[0]) return 0;
                var n = 0;
                _.each(recs, function (r) {
                    //alert(r.imports)
                  _.each(r.imports, function (i) {
                     // alert(JSON.stringify(b) + "----" + i);
                    if (name(i) === b.name) n++;
                  });
                });
                return n;
              });
            alert(/* "*****" + mpr.getMatrix() + "-----" + */ JSON.stringify(mpr.getMap()));
            drawChords(mpr.getMatrix(), mpr.getMap());
          });

          function name(name) {
            return name.substring(0, name.lastIndexOf(".")).substring(6);
          }

---------------------------File1.html-----------------------------


------------------------------mapper.js---------------------------
//*******************************************************************
//  CHORD MAPPER 
//*******************************************************************
function chordMpr (data) {
    alert(data + "kimi is faster than you");
  var mpr = {}, mmap = {}, n = 0,
      matrix = [], filter, accessor;

  mpr.setFilter = function (fun) {
      alert("inside filter");
    filter = fun;
    return this;
  },
  mpr.setAccessor = function (fun) {
    accessor = fun;
    return this;
  },
  mpr.getMatrix = function () {
    matrix = [];
    _.each(mmap, function (a) {
      if (!matrix[a.id]) matrix[a.id] = [];
      _.each(mmap, function (b) {
       var recs = _.filter(data, function (row) {
          return filter(row, a, b);
        })
        matrix[a.id][b.id] = accessor(recs, a, b);
      });
    });
    return matrix;
  },
  mpr.getMap = function () {
    return mmap;
  },
  mpr.printMatrix = function () {
    _.each(matrix, function (elem) {
      console.log(elem);
    })
  },
  mpr.addToMap = function (value, info) {
    if (!mmap[value]) {
      mmap[value] = { name: value, id: n++, data: info }
    }
  },
  mpr.addValuesToMap = function (varName, info) {
      alert("inside addValuesToMap  " + varName + info);
    var values = _.uniq(_.pluck(data, varName));
    alert(values);
    //values is the list of countries in the importer[i] column in CSV form
    _.map(values, function (v) {
        //v is individual country in the importer[i] column
      if (!mmap[v]) {
        mmap[v] = { name: v, id: n++, data: info }
      }
    });
    return this;
  }
  return mpr;
}
//*******************************************************************
//  CHORD READER
//*******************************************************************
function chordRdr (matrix, mmap) {
  return function (d) {
    var i,j,s,t,g,m = {};
    if (d.source) {
      i = d.source.index; j = d.target.index;
      s = _.where(mmap, {id: i });
      t = _.where(mmap, {id: j });
      m.sname = s[0].name;
      m.sdata = d.source.value;
      m.svalue = +d.source.value;
      m.stotal = _.reduce(matrix[i], function (k, n) { return k + n }, 0);
      m.tname = t[0].name;
      m.tdata = d.target.value;
      m.tvalue = +d.target.value;
      m.ttotal = _.reduce(matrix[j], function (k, n) { return k + n }, 0);
    } else {
      g = _.where(mmap, {id: d.index });
      m.gname = g[0].name;
      m.gdata = g[0].data;
      m.gvalue = d.value;
    }
    m.mtotal = _.reduce(matrix, function (m1, n1) { 
      return m1 + _.reduce(n1, function (m2, n2) { return m2 + n2}, 0);
    }, 0);
    return m;
  }
}

我在分析上述代码中的代码流时遇到了困难。 在mpr.setFilter(函数(行,a,b))调用中,什么是row,a和b?我找不到这个名字的变量。如何将调用从一个函数转换为另一个函数。 请帮忙。

1 个答案:

答案 0 :(得分:1)

setFilter是一个函数,它接受存储到“成员”变量filter的函数的参数。当用户调用setFilter并传递其功能时,他们必须知道合同它需要三个参数。这些是您的rowab参数,它们未在此处分配,只是为了使函数稍后匹配。

如果再跟踪一下,那么传入setFilter并存储在变量filter中的匿名函数随后会在mapper.js中的getMatrix函数中调用。你会看到它被称为row的参数,传递ab的参数。

JavaScript不是很有趣吗?