【问题标题】:d3.js graph data attributes case senstived3.js 图形数据属性区分大小写
【发布时间】:2015-12-02 19:35:48
【问题描述】:

我正在使用 d3.js 图表,它所依赖的数据如下所示:

 var graph = {
                                      "nodes" : [{"name" : "Appointments"},{"name" : "Store Visit"},{"name" : "Visit History"},{"name" : "Resume Store Visit"}], 
                                      "links" : [{"source":"Store Visit","target":"Appointments","value":6},
                                                {"source":"Appointments","target":"Visit History","value":7},
                                                {"source":"Visit History","target":"Store Visit","value":2},
                                                {"source":"Resume Store Visit","target":"Visit History","value":1},
                                                {"source":"Appointments","target":"Resume Store Visit","value":5}]
                                                };

我的 REST 服务返回完全相同的数据,但属性名称是 Source、Target 和 Value(注意首字母大写)

这会出错

Uncaught TypeError: Cannot read property 'weight' of undefinedl.start @ d3.v3.min.js:4(anonymous function) @ userflowd3.html:64(anonymous function) @ d3.v3.min.js:1t @ d3.v3.min.js:1u @ d3.v3.min.js:1

那么,json 返回 data.items 将属性值替换为小写的方式是什么?

【问题讨论】:

    标签: javascript d3.js graph


    【解决方案1】:

    所以我不得不这样做 喜欢

      var gLinks = JSON.stringify(data.items);
                                          gLinks = gLinks.replace(/Source/g,'source');
                                          gLinks = gLinks.replace(/Target/g,'target');
                                          gLinks = gLinks.replace(/Value/g,'value');
                                          var arr = JSON.parse(gLinks);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-12
      • 2012-07-15
      • 1970-01-01
      相关资源
      最近更新 更多