4

我正在尝试在引导轮播的同一页面上动态创建高图。

我有一个像这样的函数“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();
}

我有一个这样的div:

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

如您所见,我有“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]
        }]
    }

这怎么可能?

4

4 回答 4

7

如果您的目标是保持这种动态,通过能够使用多个 renderTo 构建多个图表但使用相同的选项,您可以这样做:

改变

function createChart(questiontitle, answers){
    chart = new Highcharts.Chart(options); 
    chart.renderTo(container);

至:

function createChart(questiontitle, answers){
    chart = $('#container').highcharts(options); 

或者,如果不使用 jQuery,

function createChart(questiontitle, answers){
    options.chart.renderTo = 'container';
    chart = new Highcharts.Chart(options); 
于 2013-09-11T19:05:55.447 回答
4

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

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

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

这有效: renderTo: 'graph-container'

参考 Highcharts 文档

于 2014-02-08T21:51:44.323 回答
4

像这样的东西:chart.renderTo(container);在 Highcharts 中不存在。

设置renderTo使用选项:

var options = {
    chart: {
        type: 'bar',
        renderTo: 'container'
    },
于 2013-09-11T13:22:33.950 回答
1

你可能面临和我一样的问题。尝试将您的 highchart 脚本放在您的 div 声明之后,就像在下面的示例中一样,这样它就可以识别您的 div 的 id:

<head>
    <title>HighCharts :D</title>
    <script src="http://code.highcharts.com/highcharts.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
</head>
<body>
<div id="container" style="width: 600px; height: 400px; margin: 0 auto">
</div>
<script type="text/javascript">
    var myChart = Highcharts.chart('container', {
        chart: {
            type: 'bar'
        },
        title: {
            text: 'Fruit Consumption'
        },
        xAxis: {
            categories: ['Apples', 'Bananas', 'Oranges']
        },
        yAxis: {
            title: {
                text: 'Fruit eaten'
            }
        },
        series: [{
            name: 'Jane',
            data: [1, 0, 4]
        }, {
            name: 'John',
            data: [5, 7, 3]
        }]
    });
    myChart.renderTo('container');

</script>
</body>
于 2017-03-03T13:04:54.423 回答