1

RailsCast #223 を Highcharts と Rails で見ましたが、単純な for ループと hash ループを試したところ、グラフがレンダリングされません。最初から可変数のシリーズを試しています。静的シリーズ作品。

例から直接 HighCharts 積み上げ列の元のコード

$('#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]
    }]       
});

以下は、DB からのプルを開始する前に、何かを機能させるためだけに埋め込まれた Ruby です。いくつかのシリーズを作成するための単純な for ループだけで、特別なことは何もしません。チャートを再実行すると、ブラウザでレンダリングされません。注: スクリプトは、app/assets/javascripts 内のファイル project.js 内にあり、$(function() {ブロック内にあります。

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

ここに新しいファイル 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 %>
        ]

    });
});
4

1 に答える 1