Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从外部文件到nvd3的JSON数据_Javascript_Json_D3.js_Nvd3.js - Fatal编程技术网

Javascript 从外部文件到nvd3的JSON数据

Javascript 从外部文件到nvd3的JSON数据,javascript,json,d3.js,nvd3.js,Javascript,Json,D3.js,Nvd3.js,免责声明:我是d3新手。 手头的问题:将外部JSON文件传递到nvd3piechart JSON数据: [ { "Code": "UOU", "Result": 8 }, { "Code": "HCA", "Result": 3 }, { "Code": "ACM", "Result": 5 }, { "Code": "TRG", "Result": 1.7 } ] var testdata; d3.json("../da

免责声明:我是d3新手。 手头的问题:将外部
JSON
文件传递到
nvd3
piechart

JSON数据:

[
{
    "Code": "UOU",
    "Result": 8
},
{
    "Code": "HCA",
    "Result": 3
},
{
    "Code": "ACM",
    "Result": 5
},
{
    "Code": "TRG",
    "Result": 1.7
}
]
var testdata;    
d3.json("../data/testdata.json", function(error, dataSet){
    if(error) return console.warn(error);
    testdata = dataSet;

nv.addGraph function() {
    var width = 500,
        height = 500;

    var chart = nv.models.pieChart()
        .x(function(d) { return d.key })
        .y(function(d) { return d.y })
        .color(d3.scale.category10().range())
        .width(width)
        .height(height);

      d3.select("#test1")
          .datum(testdata)
        .transition().duration(1200)
          .attr('width', width)
          .attr('height', height)
          .call(chart);

    chart.dispatch.on('stateChange', function(e) { nv.log('New State:', JSON.stringify(e)); });

    return chart;
        }
});
代码:

[
{
    "Code": "UOU",
    "Result": 8
},
{
    "Code": "HCA",
    "Result": 3
},
{
    "Code": "ACM",
    "Result": 5
},
{
    "Code": "TRG",
    "Result": 1.7
}
]
var testdata;    
d3.json("../data/testdata.json", function(error, dataSet){
    if(error) return console.warn(error);
    testdata = dataSet;

nv.addGraph function() {
    var width = 500,
        height = 500;

    var chart = nv.models.pieChart()
        .x(function(d) { return d.key })
        .y(function(d) { return d.y })
        .color(d3.scale.category10().range())
        .width(width)
        .height(height);

      d3.select("#test1")
          .datum(testdata)
        .transition().duration(1200)
          .attr('width', width)
          .attr('height', height)
          .call(chart);

    chart.dispatch.on('stateChange', function(e) { nv.log('New State:', JSON.stringify(e)); });

    return chart;
        }
});
我的问题: 将外部
JSON
文件绑定/解析到
nvd3
的最佳方法是什么

这个代码有什么问题

错误:

[
{
    "Code": "UOU",
    "Result": 8
},
{
    "Code": "HCA",
    "Result": 3
},
{
    "Code": "ACM",
    "Result": 5
},
{
    "Code": "TRG",
    "Result": 1.7
}
]
var testdata;    
d3.json("../data/testdata.json", function(error, dataSet){
    if(error) return console.warn(error);
    testdata = dataSet;

nv.addGraph function() {
    var width = 500,
        height = 500;

    var chart = nv.models.pieChart()
        .x(function(d) { return d.key })
        .y(function(d) { return d.y })
        .color(d3.scale.category10().range())
        .width(width)
        .height(height);

      d3.select("#test1")
          .datum(testdata)
        .transition().duration(1200)
          .attr('width', width)
          .attr('height', height)
          .call(chart);

    chart.dispatch.on('stateChange', function(e) { nv.log('New State:', JSON.stringify(e)); });

    return chart;
        }
});
未捕获的语法错误:意外的令牌函数(可能与数据绑定无关,不确定)


“code”属性的值应该是一个用引号括起来的字符串,不是吗?“name”:value(不需要JSON解析)。使用“名称”:“值”将需要类似于JSON.parse()的内容。您错过了
nv.addGraph
之后)。我已经尝试过了。它根本不起作用。实际上,错误将指向本机nv.d3.js文件行:8419。不过,感谢您将其带到讨论中。