【问题标题】:Ruby on Rails: How to generate an array from records to display in Highcharts?Ruby on Rails:如何从记录生成数组以在 Highcharts 中显示?
【发布时间】:2014-08-20 16:01:39
【问题描述】:

我需要将数据集显示为 Highcharts 柱形图。我不知道如何将数据传递给 Highcharts 的 Javascript 方法以显示我当天的业务流程得分。

数据集构建在我的 BusinessProcess 控制器中:

@business_process_history = DmMeasure.where("period_id between ? and ? and ODQ_object_id = ?", first_period_id, current_period_id, "BP-#{@business_process.id}").select("period_day, score").order("period_id")

这给出了预期的 2 个字段、10 条记录的结果,并且可以完美地显示在 HTML 表格中。

更新:

多巴哥的建议给出了预期的数组数组,

[["20140820", #<BigDecimal:54655c8,'0.997E2',18(45)>], ...]

但问题没有解决。

这里是函数调用,包括来自多巴哥的建议:

<script>
$(function () { 
$('#measures').highcharts({
    chart: {type: 'column'},
    title: {text: 'Data quality trend'},
    xAxis: { 
        title: {text: 'Time'}
        },
    yAxis: {
        title: {text: 'Score'}
    },
    series: [{
        data: <%= @business_process_history.map { |bp| [bp.period_day, bp.score] } %>
    }]
  });
});

</script>

此脚本不会在度量 DIV 中生成任何内容,而硬编码的值列表会生成图表。

我尝试了几种方法,但仍然无法做到。

你能帮我解决这个问题吗?

非常感谢,

最好的问候,

弗雷德

【问题讨论】:

    标签: javascript ruby-on-rails arrays highcharts


    【解决方案1】:

    试试:

    @business_process_history.map { |bp| [bp.period_day, bp.score] }
    

    【讨论】:

    • 是的,我之前确实尝试过,使用 .inspect 方法进行事件,但 highcharts 仍然没有产生任何结果...
    • 好的,我发现 BigDecimal 数据类型不满足 highcharts。我将 .to_f 添加到 score 字段以使其正常工作。谢谢!
    【解决方案2】:

    感谢多巴哥的建议,最终的解决方案是:

    @business_process_history.map { |bp| [bp.period_day, bp.score.to_f] }
    

    1 - 带有块的 map 方法生成正确的数组数组

    2 - 应用于 score 字段的 to_f 方法确保数据类型符合 Highcharts 的预期。

    感谢多巴哥!

    弗雷德

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-24
      相关资源
      最近更新 更多