Ruby on Rails:将字符串数组传递给highchart函数

问题描述 投票:2回答:1

我是Ruby on Rails的新手,目前正在使用高清图形工作。我需要一个在x轴上带有类别的柱形图,其中类别实际上应该是用户的名字(用户名不多,所以实际上确实有意义)。

不幸的是,我的图表没有显示我在调用图表函数之前定义(或从数据中检索)的字符串数组类别。通过像categories: <%= @users.map { |i| i.name } %>这样的东西来恢复名称也不起作用。

这是我在show.html.erb尝试做的最小例子:

<% quantities = [1,2,3] %>
<% names = [5,6,7] %>
<% names2 = ['A','B','C'] %>

<div id="container" style="width:60%;height:300px;"></div>
<script>
   $(function () {
      $('#container').highcharts({
         chart: {
            type: 'column'
         },
         xAxis: {
            categories: [ <%= names[0] %>, <%= names[1] %>, "test" ]
         },
         series: [{
            showInLegend: false,
            data: <%= quantities %>
         }]
      });
   });
</script> 

使用整数数组names作为图表不是问题,但不知何故,字符串数组names2没有被传递,并且当包含而不是names时,图表根本不显示。在函数内定义字符串数组也不是问题。

是否有可能通过字符串数组或从数据中检索它?

提前致谢!

javascript ruby-on-rails arrays string highcharts
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.