节点js,NVD3.js无法读取undefined的属性baseval

时间:2013-09-20 06:12:18

标签: node.js d3.js nvd3.js

对于以下数据,

historicalBarChart = [ 
  {
    key: "Cumulative Return",
    values: [
      { 
        "label" : "A" ,
        "value" : 29.765957771107
      } , 
      { 
        "label" : "B" , 
        "value" : 0
      } , 
      { 
        "label" : "C" , 
        "value" : 32.807804682612
      } , 
      { 
        "label" : "D" , 
        "value" : 196.45946739256
      } , 
      { 
        "label" : "E" ,
        "value" : 0.19434030906893
      } , 
      { 
        "label" : "F" , 
        "value" : 98.079782601442
      } , 
      { 
        "label" : "G" , 
        "value" : 13.925743130903
      } , 
      { 
        "label" : "H" , 
        "value" : 5.1387322875705
      }
    ]
  }
];

我将使用带有Node js服务器的NVD3.js生成条形图。

我的Nodejs代码生成svg: ** NVD3svgnode.js **

var http = require('http'),
    url = require('url'),
    jsdom = require('jsdom'),
    child_proc = require('child_process'),
    w = 400,
    h = 400,          
    scripts = ["file://"+__dirname+"/d3.min.js",
               "file://"+__dirname+"/d3.layout.min.js",
                "file://"+__dirname+"/nv.d3.min.js",
               "file://"+__dirname+"/bar.js"],

    htmlStub = '<!DOCTYPE html><div id="bar" style="width:'+w+'px;height:'+h+'px;"><svg xmlns="http://www.w3.org/2000/svg" version="1.1"></svg></div>';

    http.createServer(function (req, res) {
           res.writeHead(200, {'Content-Type': 'image/svg+xml'});
           jsdom.env({features:{QuerySelector:true}, html:htmlStub, scripts:scripts,   done:function(errors, window) {
                var chart1= window.bar();

       d3.select('#bar svg')
         .datum(historicalBarChart)
         .transition().duration(500)
         .call(chart1);
          }});
}).listen(8888);

生成条形码的我的NVD3代码是: 的 bar.js

function bar(){
var chart = nv.models.discreteBarChart()
      .x(function(d) { return d.label })
      .y(function(d) { return d.value })
      .staggerLabels(true)
      .tooltips(false)
      .showValues(true);
return chart;
}

执行节点js NVD3svgnode.js时,我收到以下异常

    file:///Users/administrator/Downloads/nodejs/d3.js:5347
        var t = g.transform.baseVal.consolidate();
                           ^
**strong text**TypeError: Cannot read property 'baseVal' of undefined
    at d3.transform (file:///Users/administrator/Downloads/nodejs/d3.js:5347:28)
    at Object.d3.transform (file:///Users/administrator/Downloads/nodejs/d3.js:5350:7)
    at d3_interpolateTransform (file:///Users/administrator/Downloads/nodejs/d3.js:5394:35)
    at Object.<anonymous> (file:///Users/administrator/Downloads/nodejs/d3.js:7697:32)
    at d3_Map.<anonymous> (file:///Users/administrator/Downloads/nodejs/d3.js:7858:31)
    at d3_Map.d3_class.forEach (file:///Users/administrator/Downloads/nodejs/d3.js:271:13)
    at start (file:///Users/administrator/Downloads/nodejs/d3.js:7857:28)
    at Object.callback (file:///Users/administrator/Downloads/nodejs/d3.js:7851:38)
    at d3_timer_mark (file:///Users/administrator/Downloads/nodejs/d3.js:2075:80)
    at d3_timer_step [as _onTimeout] (file:///Users/administrator/Downloads/nodejs/d3.js:2048:15)

对此问题的任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

我建议您删除代码中的transition().duration(500)调用,这是我认为在服务器端无用(它在服务器上执行,而不是在客户端执行,因此用户不会看到转换并且您的堆栈跟踪指的是转换效果,因此它可能会解决您的问题。

(我有同样的问题并删除我的过渡修复它)。