chart.renderTo不起作用

nie*_*lsv 4 javascript jquery rendering highcharts

我正在尝试在bootstrap轮播中的同一页面上动态创建highcharts.

我有一个像这样的函数"createChart":

createChart(questiontitle, answers);

function createChart(questiontitle, answers){
    chart = new Highcharts.Chart(options); // Create new chart with general options
    chart.renderTo(container);
    for (var i = 0; i < answers.length; i++) {
        categories.push(answers[i].Text);
    }
    console.log(categories);
    chart.xAxis[0].setCategories(categories);
    chart.setTitle({ text: 'eerzera' });
    // redraw chart
    chart.redraw();
}
Run Code Online (Sandbox Code Playgroud)

我有这样的div:

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
Run Code Online (Sandbox Code Playgroud)

你可以看到我有"chart.renderTo",但我总是得到同样的错误:

Highcharts错误#13

渲染div未找到

如果chart.renderTo选项配置错误,则会发生此错误,因此Highcharts无法找到用于呈现图表的HTML元素.

我的变量选项是这样的:

var options = {
        chart: {
            type: 'bar'
        },
        subtitle: {
            text: 'Source: Wikipedia.org'
        },
        xAxis: {
            categories: ['Africa', 'America', 'Asia', 'Europe', 'Oceania'],
            title: {
                text: null
            }
        },
        yAxis: {
            min: 0,
            title: {
                text: 'Population (millions)',
                align: 'high'
            },
            labels: {
                overflow: 'justify'
            }
        },
        tooltip: {
            valueSuffix: ' millions'
        },
        plotOptions: {
            bar: {
                dataLabels: {
                    enabled: true
                }
            }
        },
        legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'top',
            x: -40,
            y: 100,
            floating: true,
            borderWidth: 1,
            backgroundColor: '#FFFFFF',
            shadow: true
        },
        credits: {
            enabled: false
        },
        series: [{
            name: 'Year 1800',
            data: [107, 31, 635, 203, 2]
        }, {
            name: 'Year 1900',
            data: [133, 156, 947, 408, 6]
        }, {
            name: 'Year 2008',
            data: [973, 914, 4054, 732, 34]
        }]
    }
Run Code Online (Sandbox Code Playgroud)

这怎么可能?

jlb*_*ggs 7

If your goal is to keep this dynamic, by being able to build multiple charts with multiple renderTo's but using the same options, you can do it like this:

change

function createChart(questiontitle, answers){
    chart = new Highcharts.Chart(options); 
    chart.renderTo(container);
Run Code Online (Sandbox Code Playgroud)

to:

function createChart(questiontitle, answers){
    chart = $('#container').highcharts(options); 
Run Code Online (Sandbox Code Playgroud)

or, if not using jQuery,

function createChart(questiontitle, answers){
    options.chart.renderTo = 'container';
    chart = new Highcharts.Chart(options); 
Run Code Online (Sandbox Code Playgroud)


NLF*_*NLF 5

如果有人在谷歌搜索时偶然发现这一点,当您在 中指定元素时,如果该元素是 ID,则renderTo不应包含。#

如果你有这个元素<div id="graph-container"></div>

这失败了: renderTo: '#graph-container'

这有效: renderTo: 'graph-container'

参考 Highcharts 文档