0

I've got this JS highchart that I'm trying to pass some values into that are calculated in my pages_controller. I've tried this with global and instance variables and neither seem to work in rendering. I guess the value isn't passed in.

In my pages_controller I have:

> @nil_ref = (@counts[nil]/total.to_f)*10
> @email_ref = (@counts["email"]/total.to_f)*10
> @cpc_ref = (@counts["cpc"]/total.to_f)*10
> @display_ref = (@counts["display"]/total.to_f)*10

And then in my /public/highcharts.js file I have:

var chart1; // globally available
   $(document).ready(function () {
       var chart = new Highcharts.Chart({
           chart: {
               renderTo: 'container',
               type: 'pie'
           },
           xAxis: {
               categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
           },
           yAxis: {   
           },
           legend: {
               layout: 'vertical',
               floating: true,
               backgroundColor: '#eeeeee',
               align: 'right',
               verticalAlign: 'top',
               y: 60,
               x: -60
           },
           tooltip: {
               formatter: function() {
                   return '<b>'+ this.series.name +'</b><br/>'+
                       this.x +': '+ this.y;
               }
           },
           plotOptions: {
           },
           series: [{
               data: [<%= @nil_ref %>, <%= @email_ref %>, <%= @cpc_ref %>, <%= @display_ref %>] //These need to be global vars        
           }]
       });
   });

When I inspect the element in Chrome, I get Uncaught SyntaxError: Unexpected token at the line with the Ruby tags in the JS.

Any ideas how to successfully pass these values in?

1 Answer 1

1

if the RoR data are strings you need to do like so:

data: ["<%= @nil_ref %>", "<%= @email_ref %>", ...
Sign up to request clarification or add additional context in comments.

2 Comments

Does that matter? Same syntax? The assets around the graph are loading now, just not the actual pie chart itself.
I don't know about HeighCharts but you need to check first if it works with adding manual data, next you need to check what RoR is generating, look in the source and check on that line to see how the data array looks like. (you can even show us the source using pastebin) and finaly if one of the array elements is not a number, and it's a string you need to add "" around that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.