Ruby on Rails: How to generate an array from recor

2019-08-14 05:06发布

问题:

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

回答1:

Try:

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


回答2:

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