Ruby on Rails:如何从记录中生成一个数组以显示在Highcharts中? [英] Ruby on Rails: How to generate an array from records to display in Highcharts?

查看:71
本文介绍了Ruby on Rails:如何从记录中生成一个数组以显示在Highcharts中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

数据集构建在我的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表格中。



更新:



Tobago的建议给出了期望的数组数组,

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

但问题没有解决。



以下是包含来自多巴哥的建议的函数调用:

 < ;脚本> 
$($ {
$('#measures')。highcharts({
chart:{type:'column'},
title:{text:'Data质量趋势'},
xAxis:{
title:{text:'Time'}
},
yAxis:{
title:{text:'Score' }
},
系列:[{
data:<%= @ business_process_history.map {| bp | [bp.period_day,bp.score]}%>
}]
});
});

< / script>

此脚本在度量DIV中不产生任何内容,而硬编码的值列表确实会生成图形。 / p>

我尝试了几种方法来做到这一点,但仍然无法管理。



你能帮我吗?这是什么?



非常感谢,



祝好,

Fred

解决方案

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

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

1 - 正确的数组数组

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



感谢多巴哥!



Fred


I need to display a dataset as a Highcharts column chart. I don't know how to pass the data to the Javascript method of Highcharts to display my Business Process score regarding the day.

The dataset is built in my BusinessProcess controller:

@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")

This gives the expected 2 fields, 10 records result and would perfectly display in a HTML table.

UPDATE :

The suggestion by Tobago gives the expected array of arrays,

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

but the issue is not solved.

Here is the function call including suggestion from Tobago:

<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>

this script produces nothing in the measures DIV, while a hardcoded list of values does generate a graph.

I tried several ways to do it, but still I can't manage.

Can you help me on this ?

Thanks a lot,

Best regards,

Fred

解决方案

Thanks to Tobago's suggestion, the final solution is:

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

1 - The map method with the block generates the correct array of arrays

2 - The to_f method applied to score field makes sure the data type meets Highcharts expectations.

Thanks Tobago !

Fred

这篇关于Ruby on Rails:如何从记录中生成一个数组以显示在Highcharts中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆