.on函数显示错误的dc.js图表

时间:2018-04-07 12:51:35

标签: dc.js

我陷入困境

dcCharts  = d3.selectAll("div.mychart") 
            .attr("class", "yourchart") 
            .each(function (d, i) { 
            return d ; 
         });

我检索div并执行此操作: `

 _.each(dcCharts, function (dcChart) {
    dcChart.on("filtered", function (chart, filter) {
        map.eachLayer(function (layer) {
        map.removeLayer(layer)
        }); 
    drawMap();
    });
    dc.renderAll();
});

我收到此错误:

Uncaught TypeError: dcChart.on is not a function
    at updateFile:229
    at Function.m.each.m.forEach (underscore-min.js:5)
    at updateFile:228
    at Object.<anonymous> (d3.v3.js:1996)
    at Object.event (d3.v3.js:504)
    at XMLHttpRequest.respond (d3.v3.js:1949)

1 个答案:

答案 0 :(得分:1)

对于未来的读者,这是Dynamic div creation for dc.js and implementing crossfiltered dashboard的后续行动,它提供了一些背景信息。

这里的问题是每个div的绑定数据是字符串列名,而不是图表。如果要生成图表数组,我建议在初始化图表时构建数组。 (从我编辑的答案复制到上一个问题)

var dcCharts = new Array(cols.length);
divs.each(function(col, i) {
  var dimension = cf.dimension(function(d) { return d[col]; });
  var group = dimension.group(); // or however you want to bin them
  var bar = dc.barChart(this) // pass div element as parent/root instead of using selector
      .dimension(dimension)
      .group(group)
      // ...
  dcCharts[i] = bar;
});

现在您的_.each(dcCharts, ...应该有效。