【问题标题】:embedded ruby not working in highcharts嵌入式 ruby​​ 在 highcharts 中不起作用
【发布时间】:2013-04-08 18:58:23
【问题描述】:

我在 Highcharts 和 Rails 上查看了 RailsCast #223,当我尝试简单的 for 循环和哈希循环时,图表甚至没有呈现。我正在尝试从可变数量的系列开始。静态系列作品。

直接来自示例的 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]
    }]       
});

下面是我的嵌入式 Ruby,只是为了在我开始从 DB 中提取之前让某些东西工作。它并没有做任何花哨的事情,只是一个简单的 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 %>
        ]

    });
});

【问题讨论】:

  • 你放置 ruby​​ 代码的文件的扩展名是什么
  • 文件是app/assets/javascripts/project.js
  • 重命名为project.js.erb,然后重试。 Ruby 不解析没有erb 扩展名的文件
  • 根据 Jashwant 的建议创建 show.js.erb 并编辑问题以匹配
  • Jashwant 的解决方案不起作用吗?

标签: ruby ruby-on-rails-3 highcharts embedded-ruby


【解决方案1】:

我遇到了同样的问题。主要问题在这一行

name: <%= arrr %>,

这是错误的。正确的是

name: "<%= arrr %>",

检查差异。它是"。也检查 Ryan 的 railscasts。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多