Question

I have a model called Data and some columns called timestamp, value1 and value2. I would like to use it with highstock chart.

Before the chart is printed I would like some calculations on it:

Summarize the result of value1 devided by value2 (value1/value2) by each day or month or year and put it in an array like [[timestamp_day, value1/value2], [...], ...].

I'm able to do the "timestamp-grouping". But I'm hanging on summarize the value1/value2.

Is there a way to do it like .sum(value1/value2)? Or is there any way to define a virtual column that does the calculation?

Thanks & best regards, Andreas

Was it helpful?

Solution

If you're trying to grab the calculated values right from the database you can pass an expression into the active record sum function.

Data.group(:timestamp).sum("value1 / value2")

OTHER TIPS

@array = @collection.collect { |c| [v.timestamp, (v.value1+v.value2 /3)] }

@collection is the collection/array of all your data

@array will be of this format after collect is executed: [[timestamp_day, value1/value2], [...], ...]

The one thing that isn't clear is what the denominator is. I use "3" here but could be anything you want. You could even call up another method to get it if it's a complex operation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top