如何使用javascript

时间:2016-06-04 19:24:58

标签: javascript arrays json d3.js

我正在使用d3.js图表​​库,而且我使用雷达图表extension,它似乎只以一种方式接受数据对象。我没有尝试更改扩展程序的代码,而是认为将数据操作为其编码接受的类型会更容易。如果有意义吗?

无论如何,这是我的代码;

我的JSON:

{
    "questions": ["Staff is courteous and polite","Attentive staff","Modern brand","Innovative brand","Good employer","Company I trust","Place for kids and family","Place for young people","Affordable food"],
    "organizations": ["MC", "BK", "KFC"],
    "dates": ["Jan", "Feb", "Mar"],
    "values": [ [
            [40, 15, 13],
            [25, 24, 14],
            [1, 23, 20]] ... etc etc

使用Javascript:

d3.json("data.json", function(error, data) {

    var newValue =[];

    var yellow = [
        [
            {"label":"A","value":6},
            {"label":"B","value":4},
            {"label":"C","value":6},
            {"label":"D","value":5.5},
            {"label":"E","value":8},
            {"label":"F","value":7},
            {"label":"G","value":9},
            {"label":"H","value":10},
            {"label":"I","value":3.5}
        ]
    ];
    if (error) {
        console.log(error);
    }
    else {
        data = data;
    }

     var newValue = [];
        var orgS = "MC";
        var dateS = "Jan";
        for (var question = 0; question < data.questions.length; question++) {
            var organization = data.organizations.indexOf(orgS);
            var date = data.dates.indexOf(dateS);
            newValue.push({
                label: data.questions[question],
                value: data.values[question][organization][date]
            });
        }

    console.log(newValue);
    console.log(yellow);


});

控制台输出结构:

enter image description here

所以我的问题是,如何将我的数据输出到控制台,如var&#34; yellow&#34; (图中的底部)?

我试图在newValue.push周围包裹[],但它没有返回所需的效果。

我希望这是可能的,我们非常感谢任何建议!

这是一个所有设置的plnk -

https://plnkr.co/edit/EBcxa39sal0PAOJxSYKb?p=preview

(哦 - 我真的不确定这个问题的标题应该是什么,欢迎编辑/建议一个新标题以更准确地描述问题)。

1 个答案:

答案 0 :(得分:1)

虽然我不明白这一点......

d3.json("data.json", function(error, data) {

    var newValue =[];

    var yellow = [
        [
            {"label":"A","value":6},
            {"label":"B","value":4},
            {"label":"C","value":6},
            {"label":"D","value":5.5},
            {"label":"E","value":8},
            {"label":"F","value":7},
            {"label":"G","value":9},
            {"label":"H","value":10},
            {"label":"I","value":3.5}
        ]
    ];
    if (error) {
        console.log(error);
    }
    else {
        data = data;
    }

     var newValue = [];
        var orgS = "MC";
        var dateS = "Jan";
        for (var question = 0; question < data.questions.length; question++) {
            var organization = data.organizations.indexOf(orgS);
            var date = data.dates.indexOf(dateS);
            newValue.push({
                label: data.questions[question],
                value: data.values[question][organization][date]
            });
        }

    var newArrayValue = [];
    newArrayValue.push(newValue);

    console.log(newValue);
    console.log(newArrayValue);
    console.log(yellow);


});

https://plnkr.co/edit/UrmWt5AgqAuJWe8cO1Bi?p=preview