简体   繁体   English

在一页上呈现多个CanvasJS图表时出现问题

[英]Problem Rendering Multiple CanvasJS charts on one page

I have a page (stats.php) that calls php pages containing canvasjs scripts for rendering charts on them. 我有一个页面(stats.php),该页面调用包含canvas.js脚本以在其上呈现图表的php页面。 I can get the pages (top_airlines.php and top_aircraft.php) that contain each individual chart to render, however, when I try to get them on the single stats.php page only one of them will actually render. 我可以获取包含要渲染的每个单独图表的页面(top_airlines.php和top_aircraft.php),但是,当我尝试将它们显示在单个stats.php页面上时,实际上只有其中一个可以渲染。

It utilizes JSON, which I am not at all familiar with and am using the example that was given to me by their help desk a couple of years ago. 它利用了我完全不熟悉的JSON,并使用了几年前他们的服务台提供给我的示例。 I've attempted to modify the code so that it should, in theory, load the chart. 我试图修改代码,以便从理论上讲应该加载图表。 Again, they'll load on their independent pages it's just when I try to call them on a single page together that all code breaks loose. 同样,它们将加载到其独立的页面上,只是当我尝试一起在单个页面上调用它们时,所有代码都松散了。

I am curious to think that maybe it is related to the javascript code for 我很好奇,可能与它的javascript代码有关

$(document).ready(function ()` $(document).ready(function()`

TOP AIRLINES (top_airlines.php) 顶级航空公司(top_airlines.php)

<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js”></script>
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js”></script>
<script src=”http://globe-trekking.com/vg/includes/js/jquery.canvasjs.min.js”></script>

<script type=”text/javascript”>
window.onload = function () {
CanvasJS.addColorSet(“blueShades2”,
[//colorSet Array
“#074b83”,
“#085a9d”,
“#0a69b7”,
“#0b78d1”,
“#0c87eb”,
“#2196f3”,
“#4daaf6”,
“#79bff8”,
“#a6d4fa”,
“#d2eafd”
]);

$(document).ready(function () {
$.getJSON(“http://globe-trekking.com/vg/charts/top_airlines_data.php”, function (result) {

var chartAirlines = new CanvasJS.Chart(“top_10_airlines_chart”, {
animationEnabled: false,
colorSet: “blueShades2”,
toolTip:{content: “{name}”},
data: [
{
type: “bar”,
indexLabelFontSize: 22,
dataPoints: result
}
]
});

chartAirlines.render();
});
});
}
</script>

<div id=”top_10_airlines_chart” style=”width: 100%; height: 300px;”></div>

TOP AIRCRAFT (top_aircraft.php) 顶级飞机(top_aircraft.php)

<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js”></script>
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js”></script>
<script src=”http://globe-trekking.com/vg/includes/js/jquery.canvasjs.min.js”></script>

<script type=”text/javascript”>

window.onload = function () {
CanvasJS.addColorSet(“blueShades”,
[//colorSet Array
“#074b83”,
“#085a9d”,
“#0a69b7”,
“#0b78d1”,
“#0c87eb”,
“#2196f3”,
“#4daaf6”,
“#79bff8”,
“#a6d4fa”,
“#d2eafd”
]);

$(document).ready(function () {

$.getJSON(“http://globe-trekking.com/vg/charts/top_aircraft_data.php”, function (result) {

var chartAircraft = new CanvasJS.Chart(“top_10_airplanes_chart”, {
animationEnabled: false,
colorSet: “blueShades”,
toolTip:{content: “{name}”},
data: [
{
type: “bar”,
indexLabelFontSize: 22,
dataPoints: result
}
]
});

chartAircraft.render();
});
});
}
</script>

<div id=”top_10_airplanes_chart” style=”width: 100%; height: 300px;”></div>

I'm calling them on the stats.php page by using the following located in a certain location on the stats.php page. 我通过使用位于stats.php页面某个位置的以下内容在stats.php页面上调用它们。

The issue seems to happen due to overriding of window.onload event. 该问题似乎是由于覆盖window.onload事件而发生的。 Changing it to jQuery ready / load should work fine in your case. 在您的情况下,将其更改为jQuery ready / load应该可以正常工作。 Here is the sample project . 这是示例项目

You can also try out the ways suggested in this stackoverflow thread . 您也可以尝试该stackoverflow线程中建议的方法。

$(document).ready(function () {
    CanvasJS.addColorSet(“blueShades”,
    [//colorSet Array
        “#074b83”,
        “#085a9d”,
        “#0a69b7”,
        “#0b78d1”,
        “#0c87eb”,
        “#2196f3”,
        “#4daaf6”,
        “#79bff8”,
        “#a6d4fa”,
        “#d2eafd”
    ]);
});

PS: dataPoints are hardcoded in sample-project. PS:dataPoints在示例项目中进行了硬编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM