Highcharts不会显示在Chrome应用中

时间:2015-01-29 17:18:33

标签: javascript jquery google-chrome-extension highcharts google-chrome-app

我正在尝试从highcharts.com嵌入图表(使用jquery)。如果我只是在chrome本身中打开windows.html,图表就可以正常运行,但它不会在我的Chrome应用中加载。只是一个空白的屏幕。我在文件夹中有所有标准的.js文件,但这里没有包含它们。

我从这里的例子开始: https://developer.chrome.com/apps/first_app

background.js

chrome.app.runtime.onLaunched.addListener(function() {
  chrome.app.window.create('window.html', {
    'bounds': {
      'width': 400,
      'height': 500
    }
  });
});

清单文件:

{
  "name": "Hello World!",
  "description": "My first Chrome App.",
  "version": "0.1",
  "manifest_version": 2,
  "app": {
    "background": {
      "scripts": ["background.js"]
    }
  },
  "icons": { "16": "calculator-16.png", "128": "calculator-128.png" }
}

window.html

<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Highcharts Example</title>

        <script type="text/javascript" src="jquery.min.js"></script>
        <style type="text/css">
${demo.css}
        </style>
        <script type="text/javascript">
$(function () {
    $(document).ready(function () {
        Highcharts.setOptions({
            global: {
                useUTC: false
            }
        });

        $('#container').highcharts({
            chart: {
                type: 'spline',
                animation: Highcharts.svg, // don't animate in old IE
                marginRight: 10,
                events: {
                    load: function () {

                        // set up the updating of the chart each second
                        var series = this.series[0];
                        setInterval(function () {
                            var x = (new Date()).getTime(), // current time
                                y = Math.random();
                            series.addPoint([x, y], true, true);
                        }, 1000);
                    }
                }
            },
            title: {
                text: 'Live random data'
            },
            xAxis: {
                type: 'datetime',
                tickPixelInterval: 150
            },
            yAxis: {
                title: {
                    text: 'Value'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                formatter: function () {
                    return '<b>' + this.series.name + '</b><br/>' +
                        Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' +
                        Highcharts.numberFormat(this.y, 2);
                }
            },
            legend: {
                enabled: false
            },
            exporting: {
                enabled: false
            },
            series: [{
                name: 'Random data',
                data: (function () {
                    // generate an array of random data
                    var data = [],
                        time = (new Date()).getTime(),
                        i;

                    for (i = -19; i <= 0; i += 1) {
                        data.push({
                            x: time + i * 1000,
                            y: Math.random()
                        });
                    }
                    return data;
                }())
            }]
        });
    });
});
        </script>
  </head>
  <body>
    <script src="highcharts.js"></script>
    <script src="exporting.js"></script>

    <div id="container" style="min-width: 200px; height: 200px; margin: 0 auto"></div>
  </body>
</html>

1 个答案:

答案 0 :(得分:2)

答案在于这个JSFiddle:http://jsfiddle.net/jarrodek/sdr57h3g/

<body>
    <!-- Firstly show content, the app will looks more responsive -->
    <div id="container" style="min-width: 200px; height: 200px; margin: 0 auto"></div>
    <!-- now you have content loaded and you can execute you code -->
    <script type="text/javascript" src="jquery.min.js"></script>
    <script src="highcharts.js"></script>
    <script src="exporting.js"></script>
    <!-- move your code to external file (panel below) -->
    <script src="myapp.js"></script>
</body>

现在您可以确保在代码运行之前已加载所有依赖项。