gpt4 book ai didi

javascript - 2个canvasjs图表在一页的问题,

转载 作者:行者123 更新时间:2023-11-28 19:09:24 24 4
gpt4 key购买 nike

我尝试将 2 个 Canvasjs 图表放在一个 html 页面中。我已经包含了jquery,当我只放置一个图表div时它就可以工作,一旦我添加第二个图表,它就会显示错误,只显示一张图表。

我想在一个页面中显示 2 个或更多图表,可以吗?

这是我的代码。谢谢

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="canvas/js/jquery.canvasjs.min.js"></script>

<!FIRST JAVASCRIPT>
<script type="text/javascript">
window.onload = function () {
var chart = new CanvasJS.Chart("chartContainer", {
data: [
{
type: "column",
dataPoints: [
{ x: 10, y: 10 },
{ x: 20, y: 15 },
{ x: 30, y: 25 },
{ x: 40, y: 30 },
{ x: 50, y: 28 }
]
}
]
});

chart.render();
}
</script>
<!SECOND JAVASCRIPT>
<script type="text/javascript">
window.onload = function () {
var chart = new CanvasJS.Chart("chartContainer2");

chart.options.axisY = { prefix: "$", suffix: "K" };
chart.options.title = { text: "Fruits sold in First & Second Quarter" };

var series1 = { //dataSeries - first quarter
type: "column",
name: "First Quarter",
showInLegend: true
};

var series2 = { //dataSeries - second quarter
type: "column",
name: "Second Quarter",
showInLegend: true
};

chart.options.data = [];
chart.options.data.push(series1);
chart.options.data.push(series2);


series1.dataPoints = [
{ label: "banana", y: 18 },
{ label: "orange", y: 29 },
{ label: "apple", y: 40 },
{ label: "mango", y: 34 },
{ label: "grape", y: 24 }
];

series2.dataPoints = [
{ label: "banana", y: 23 },
{ label: "orange", y: 33 },
{ label: "apple", y: 48 },
{ label: "mango", y: 37 },
{ label: "grape", y: 20 }
];

chart.render();
}
</script>




</head>

<body>
<!FIRST CHART SHOW>
<div id="chartContainer1" style="height: 500px; width: 50%;"></div>
<!SECOND CHART SHOW>
<div id="chartContainer2" style="height: 500px; width: 50%;"></div>


</body>
</html>

最佳答案

首先,您不需要两个 window.onload 函数。第二个 onload 函数将覆盖第一个 onload 函数。因此,只需拥有一个并将两个图表代码都包含在其中一个中即可。

此外,创建的第一个图表需要一个 id 为 chartContainer 的元素,该元素在 DOM 中不存在。将其替换为 chartContainer1

替换

var chart = new CanvasJS.Chart("chartContainer", {...});

var chart = new CanvasJS.Chart("chartContainer1", {...});

两个图表都使用两个变量,而不是使用一个变量chart,这样该变量就不会被覆盖。像这样的事情

window.onload = function(){
var chart1 = new CanvasJS.Chart("chartContainer1", {
data: [{
type: "column",
dataPoints: [{
x: 10,
y: 10
}, {
x: 20,
y: 15
}, {
x: 30,
y: 25
}, {
x: 40,
y: 30
}, {
x: 50,
y: 28
}]
}]
});

chart1.render();

var chart2 = new CanvasJS.Chart("chartContainer2");

chart2.options.axisY = {
prefix: "$",
suffix: "K"
};
chart2.options.title = {
text: "Fruits sold in First & Second Quarter"
};

var series1 = { //dataSeries - first quarter
type: "column",
name: "First Quarter",
showInLegend: true
};

var series2 = { //dataSeries - second quarter
type: "column",
name: "Second Quarter",
showInLegend: true
};

chart2.options.data = [];
chart2.options.data.push(series1);
chart2.options.data.push(series2);


series1.dataPoints = [{
label: "banana",
y: 18
}, {
label: "orange",
y: 29
}, {
label: "apple",
y: 40
}, {
label: "mango",
y: 34
}, {
label: "grape",
y: 24
}];

series2.dataPoints = [{
label: "banana",
y: 23
}, {
label: "orange",
y: 33
}, {
label: "apple",
y: 48
}, {
label: "mango",
y: 37
}, {
label: "grape",
y: 20
}];

chart2.render();
};

这是一个演示 http://jsbin.com/rocusuhevo/edit?html,js,output

关于javascript - 2个canvasjs图表在一页的问题,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31016330/

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