如何动态加载highcharts

发布于 2025-01-01 05:56:39 字数 1111 浏览 1 评论 0原文

我正在尝试动态加载 highcharts,如下面的代码所示: 它无法识别 highcharts 变量。如果我通过静态方式加载高图表,它就可以工作。请帮忙。

我正在使用 jquery 版本 4 和 highcharts 最新版本 1.0

<html>
<head>
//loading jquery
<script src="js/libs/jquery-1.4.2.js" type="text/javascript"></script>
<script type="text/javascript">
var chart1;

$(document).ready(function() {
$.getScript('js/libs/highcharts.src.js', function() {

            });


  chart1 = new Highcharts.Chart({
         chart: {
            renderTo: 'chart1',
            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]
         }]
      });
   });


</script>


</head>

<body>
<div id="chart1"></div>

</div>
</body>
</html>

I am trying to load highcharts dynamically as shown in below code: It is not able to recognize highcharts variable. If I load highcharts by static means, it works. Please help.

I am using jquery version 4 and highcharts latest version 1.0

<html>
<head>
//loading jquery
<script src="js/libs/jquery-1.4.2.js" type="text/javascript"></script>
<script type="text/javascript">
var chart1;

$(document).ready(function() {
$.getScript('js/libs/highcharts.src.js', function() {

            });


  chart1 = new Highcharts.Chart({
         chart: {
            renderTo: 'chart1',
            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]
         }]
      });
   });


</script>


</head>

<body>
<div id="chart1"></div>

</div>
</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉梦枕江山 2025-01-08 05:56:39

$.getScript() 是一个异步操作,这意味着它将获取脚本,但不会等待脚本完成后再处理其余代码。因此,您需要等待代码已被检索的某种指示。如果您查看文档,您会发现$.getScript() 已成功回调函数就是为了这个目的。事实上,您的代码中已经有一个空的 success 函数,只需将 Highcharts 声明移到那里即可。换句话说,执行以下操作:

$(document).ready(function() {
    $.getScript('js/libs/highcharts.src.js', function() {
        chart1 = new Highcharts.Chart({
            // Highcharts options
        });
    });
});

$.getScript() is an asynchronous operation, meaning it will go fetch the script but won't wait for it to be done before processing the rest of the code. Therefore you need to wait for some sort of indication that the code has been retrieved. If you look at the docs you will notice that $.getScript() has a success callback function just for this purpose. In fact, you already have an empty success function in your code, just move the Highcharts declaration there. In other words, do this:

$(document).ready(function() {
    $.getScript('js/libs/highcharts.src.js', function() {
        chart1 = new Highcharts.Chart({
            // Highcharts options
        });
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文