user2242044
user2242044

Reputation: 9213

Highcharts won't display in Chrome App

I am trying to embed a chart from highcharts.com (using jquery). The graphs works fine if I just open windows.html in chrome itself, but it will not load in my chrome app. Just a blank screen. I have all the standard .js files in the folder, but didn't include them here.

I started with the example here: 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
    }
  });
});

manifest file:

{
  "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>

Upvotes: 1

Views: 1770

Answers (1)

Pawel Uchida-Psztyc
Pawel Uchida-Psztyc

Reputation: 3838

The answer is in this 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>

Now you can be sure that all dependencies has been loaded before your code run.

Upvotes: 2

Related Questions