Highcharts 不会显示在 Chrome 应用程序中

Highcharts won't display in 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>

答案在这个 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>

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