Question

I viewed RailsCast #223 on Highcharts and Rails and when I tried a simple for loop and a hash loop the chart doesn't even render. I'm trying for a variable number of series to start with. Static series works.

Original code for HighCharts stacked column straight from examples

$('#container').highcharts({
    chart: {
        type: 'column'
    },
    title: {
        text: 'Stacked column chart'
    },
    xAxis: {
        categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas']
    },
    yAxis: {
        min: 0,
        title: {
            text: 'Total fruit consumption'
        },
        stackLabels: {
            enabled: true,
            style: {
                fontWeight: 'bold',
                color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
            }
        }
    },
    legend: {
        align: 'right',
        x: -100,
        verticalAlign: 'top',
        y: 20,
        floating: true,
        backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColorSolid) || 'white',
        borderColor: '#CCC',
        borderWidth: 1,
        shadow: false
    },
    tooltip: {
        formatter: function() {
            return '<b>'+ this.x +'</b><br/>'+
                this.series.name +': '+ this.y +'<br/>'+
                'Total: '+ this.point.stackTotal;
        }
    },
    plotOptions: {
        column: {
            stacking: 'normal',
            dataLabels: {
                enabled: true,
                color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white'
            }
        }
    },
    series: [{
        name: 'John',
        data: [5, 3, 4, 7, 2]
    }, {
        name: 'Jane',
        data: [2, 2, 3, 2, 1]
    }, {
        name: 'Joe',
        data: [3, 4, 4, 2, 5]
    }]       
});

Below is my embedded Ruby just to get something working before I start pulling from DB. It doesn't do anything fancy just a simple for loop to create a couple of series. The chart doesn't render in the browser when I rerun it. Note: The script is in a file project.js within app/assets/javascripts and is in a $(function() { block.

series: [        
  <% xarray = [1, 2, 3, 4, 5] %> 
  <% for x in xarray %>
   {
    name: "<%= x %>",
    data: [5, 3, 4, 7, 2]
   }
  <% end %>

Here is new file show.js.erb

$(function() {
   $('#container').highcharts({
        chart: {
            type: 'column'
        },
        title: {
            text: 'Resource Load by Project'
        },
        xAxis: {
            categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas']
        },
        yAxis: {
            min: 0,
            title: {
                text: 'Total hours worked'
            },
            stackLabels: {
                enabled: true,
                style: {
                    fontWeight: 'bold',
                    color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
                }
            }
        },
        legend: {
            align: 'right',
            x: -100,
            verticalAlign: 'top',
            y: 20,
            floating: true,
            backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColorSolid) || 'white',
            borderColor: '#CCC',
            borderWidth: 1,
            shadow: false
        },
        tooltip: {
            formatter: function() {
                return '<b>'+ this.x +'</b><br/>'+
                    this.series.name +': '+ this.y +'<br/>'+
                    'Total: '+ this.point.stackTotal;
            }
        },
        plotOptions: {
            column: {
                stacking: 'normal',
                dataLabels: {
                    enabled: true,
                    color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white'
                }
            }
        },
        series: [
          <% arr = [1, 2, 3, 4, 5] %>
          <% for arrr in arr %>
          {
            name: <%= arrr %>,
            data: [5, 3, 4, 7, 2]
          }
          <% end %>
        ]

    });
});

No correct solution

OTHER TIPS

I was having this same issue. The main problem is on this line

name: <%= arrr %>,

This is wrong. The correct one will be

name: "<%= arrr %>",

Check the difference. It is ". Check Ryan's railscasts too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top