渲染nvd3条形图时出错

时间:2014-02-28 17:21:57

标签: charts d3.js nvd3.js

我一直收到这个错误:

Uncaught TypeError: Cannot call method 'map' of undefined

运行此代码时:

        var testdata = [
                {
                    label: "One",
                    x: 5,
                    y: 10
                },
                {
                    label: "2",
                    x: 6,
                    y: 15
                },
                {
                    label: "3",
                    x: 5,
                    y: 25
                },
                {
                    label: "4",
                    x: 10,
                    y: 0
                }
    ];

    nv.addGraph(function () {
        var width = nv.utils.windowSize().width - 40,
            height = nv.utils.windowSize().height / 2 - 40;

        var chart = nv.models.multiBar()
            .stacked(true)
            .width(width)
            .height(height);

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

        return chart;
    });

1 个答案:

答案 0 :(得分:0)

问题是test_data格式不正确。它应该包含在一个包含一个对象的数组中,该对象定义了“keys”和“values”字段

    var testdata = [
        {
            keys: "returns",

            values: [
                {
                    label: "One",
                    x: 5,
                    y: 10
                },
                {
                    label: "2",
                    x: 6,
                    y: 15
                },
                {
                    label: "3",
                    x: 5,
                    y: 25
                },
                {
                    label: "4",
                    x: 10,
                    y: 0
                },
            ]
        }
    ];