未捕获的TypeError:无法读取未定义的属性"createElementNS"

7 javascript jquery nvd3.js

我正在使用nvd3绘制折线图,​​当我通过div nvd3来绘制图表时,它给我这个错误

未捕获的TypeError:无法读取未定义的属性"createElementNS"

这是代码:

 var chartDiv = 'line-chart'+ counter++;
   tmpl = $($('#charts-panel-template').clone().html());
                tmpl.find('.feature-line-chart').attr('id', chartDiv);
 var  div=tmpl.find('.feature-line-chart#'+chartDiv);
           chartsPanel.append(tmpl);
    nv.addGraph(function() {
    var chart;
    var width = 1024, height = 500;
    chart = nv.models.lineChart()
            // .color(sparkChart.colors)
            .width(width).height(height);
    //modify the x.axes
        chart.x(function(d,i) { 
              return d.x;
        });

    //giving chart margin
    chart.margin({right: 40});

    $(div).empty();
    //create chart

    var svg = d3.select(div).append('svg')
         .datum(data)
         .transition()
         .duration(500)
         .call(chart)
         .attr('width', width)
         .attr('height', height);
Run Code Online (Sandbox Code Playgroud)

我的问题,

  • 我做错了
  • 是我所缺少的

par*_*ent 18

我刚遇到同样的问题.

您不能将jquery引用传递给d3.select():

 d3.select($element) //no bueno
Run Code Online (Sandbox Code Playgroud)

您必须传递实际的DOM节点,或者您最终想出的ID.

d3.select($element[0])
Run Code Online (Sandbox Code Playgroud)

要么

d3.select("#id")
Run Code Online (Sandbox Code Playgroud)


小智 3

我已经得到了解决方案,尽管 nvd3 olny 需要 id,但我在 nvd3 中传递类和 id

 var  div=tmpl.find('.feature-line-chart#'+chartDiv);
Run Code Online (Sandbox Code Playgroud)

变得

var  div='#'+chartDiv;
Run Code Online (Sandbox Code Playgroud)