gpt4 book ai didi

javascript - chart.renderTo 不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 04:50:31 24 4
gpt4 key购买 nike

我正在尝试在引导轮播的同一页面上动态创建 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();
}

我有一个像这样的 div:

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

如您所见,我有“chart.renderTo”,但我总是得到同样的错误:

Highcharts Error #13

Rendering div not found

This error occurs if the chart.renderTo option is misconfugured so that Highcharts is unable to find the HTML element to render the chart in.

我的变量选项是这样的:

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]
}]
}

这怎么可能?

最佳答案

如果您的目标是保持这种动态,通过能够使用多个 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);

关于javascript - chart.renderTo 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18742065/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com