Reputation: 547
I've included Highcharts in a simpler Rails Application and it works just fine. but when I did the same thing in a more complex Rails Application the Javascript code the Chart is not showing up.
here's what I did so far:
Gemfile
gem "highcharts-rails", "~> 3.0.0"
application.js
//= require highcharts
//= require highcharts/highcharts-more
show.html.erb
<div class="panel panel-default">
<div class="panel-heading">Chart</div>
<div id="container" style="width:100%; height:400px;">
</div>
<script type="text/javascript" charset="utf-8">
$(function () {
$(document).ready(function() {
Highcharts.setOptions({
global: {
useUTC: false
}
});
var chart;
$('#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++) {
data.push({
x: time + i * 1000,
y: Math.random()
});
}
return data;
})()
}]
});
});
});
</script>
</div>
Does anybody know what I did wrong? Google tells it might have something to do with turbolinks, but then it wouldnt work on the other application too...
And quick second question. How could I add this code to channel.js.coffee so there's no js on this page? thanks in advance!
Upvotes: 1
Views: 1937
Reputation: 806
I ran into something very similar to this when I started working with highcharts. I know this question is a bit old, but I wanted to write this for reference in case anyone else runs into this issue.
Here is what I did to get my charts to work (for reference, I'm running Rails 4.0.2 and ruby 2.1.1. and the latest highcharts with the latest highcharts-rails gem):
First, be sure that all the highcharts.js files are loaded into your assets/javascript directory.
In your application.js file, you should have the following in this order:
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require highcharts
//= require_tree .
Second, to get the code out of your show.html.erb file, I don't know about coffeescript, but you can create a js file in /assets/javascript called chart.js. Put your javascript into this file, starting with
$(function () {
Make sure that ('#container') in your chart.js matches the id of the div in your html.erb file. Like this:
/assets/javascript/chart.js
$('#container').highcharts({
show.html.erb:
<div id="container" style="width:100%; height:400px;"></div>
I didn't need to remove or turn off turbolinks to get any of this to work. However, when I set up my navbar links, I wanted my pages to refresh upon reopen (to ensure the latest information was presented, not the cached information) so in my navbar, I did remove the turbolinks functionality by adding data-no-turbolinks to the navbar div which contained my links. As follows:
<div class="nav" data-no-turbolink>
... navbar code here
</div>
Here's a link to the github repo for turbolinks - read the documentation as it gives good information on what turbolinks does, why it's helpful, and (when you don't want to use it) how to turn it off. https://github.com/rails/turbolinks/blob/master/README.md
One last thing - when I finally got my charts to render (tada!), they had the highcharts themes applied. I simply removed the highchart theme folder from /assets/javascript and they look great.
Upvotes: 1
Reputation: 111
Grab the latest Highcharts source, it fixes this problem: code.highcharts.com
Upvotes: 1