C3 js无法读取undefined的属性'width'

时间:2014-10-28 12:14:01

标签: javascript charts c3.js

我第一次尝试设置C3图表。

我在他们的官方网站上关注了这个例子并获得了以下HTML:

<section class="panel panel-default">
    <header class="panel-heading font-bold">C3 Test</header>
    <section class="panel-body">
        <div id="chart"></div>
    </section>
</section>

为此,我有以下javascript:

    var chart = c3.generate({
    bindto: '#chart',
    data: {
        columns: [
            ['data1', 30, 200, 100, 400, 150, 250],
            ['data2', 50, 20, 10, 40, 15, 25]
        ],
        axes: {
            data2: 'y2'
        },
        types: {
            data2: 'bar'
        }
    },
    axis: {
        y: {
            label: {
                text: 'Y Label',
                position: 'outer-middle'
            },
            tick: {
                format: d3.format("$,") // ADD
            }
        },
        y2: {
            show: true,
            label: {
                text: 'Y2 Label',
                position: 'outer-middle'
            }
        }
    }
});

然而,当我运行此操作时,我收到以下错误:

Uncaught TypeError: Cannot read property 'width' of undefined c3.min.js:3

有谁知道问题可能是什么?

2 个答案:

答案 0 :(得分:6)

好的,我找到了答案

Uncaught TypeError: Cannot read property 'width' of undefined

显然你必须等到文档加载或加载页面底部的脚本才能使用。

答案 1 :(得分:0)

您还可以在SCRIPT元素上添加defer属性。它指示脚本标签的内容要在页面加载后才能执行。

因此,您将得到以下内容:

    

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

class TestClass {

    public static void main(String args[]) throws Exception {
        Scanner sc = new Scanner(System.in);
        Set<Integer> perfectNumbers = new HashSet<>();
        for (int i = 0; i <= 20; i++) {
            perfectNumbers.add(i * i * i);
        }
        for (int i = 1; i <= 44; i++) {
            perfectNumbers.add(i * i);
        }

        int t = sc.nextInt();
        for (int k = 0; k < t; k++) {
            int db = 0;
            int n = sc.nextInt();
            int[] a = new int[1001];
            int[] b = new int[1001];
            int[] numbers = new int[n];
            for (int j = 0; j < n; j++) {
                int x = sc.nextInt();
                numbers[j] = x;
                for (Integer perfect : perfectNumbers) {
                    if (x == perfect - x) {
                        b[x]++;
                    } else if (perfect - x >= 0 && perfect - x <= 1000)
                        a[perfect - x]++;
                }
            }
            for (int j = 0; j < n; j++) {
                db += a[numbers[j]];
            }
            for (int j = 0; j <= 1000; j++) {
                if (b[j] > 1) {
                    db += b[j] * (b[j] - 1);
                }
            }

            System.out.println(db / 2);           
        }
    }
}