Search code examples
ruby-on-railschartkick

How to join points by line?


I am using chartkick and gem "groupdate" in my project.

<%= area_chart SugarLevel.group_by_minute(created_at).sum(:mmol) %>

I want to keep continuing line from one point to another. I do something wrong with groupdate. If there is no data at some minute, the line has to start from previous point and has to finish at next point. enter image description here

It has to be graph without gaps. Like that: enter image description here


Solution

  • Method in gem "groupdate" group_by_something uses Hash to store a data. For example:

     @hash = SugarLevel.group_by_minute(:created_at).sum(:mmol) # => {Thu, 01 Jun 2017 17:39:00 UTC +00:00=>#<BigDecimal:2add9a8,'0.77E1',18(18)>, Thu, 01 Jun 2017 17:40:00 UTC +00:00=>0, Thu, 01 Jun 2017 17:41:00 UTC +00:00=>0, Thu, 01 Jun 2017 17:42:00 UTC +00:00=>0, Thu, 01 Jun 2017 17:43:00 UTC +00:00=>0}
    

    We can clear this hash as we want:

    @hash.select{|k, v| v != 0} # Select all keys which do not consist 0
    

    And eventually, we can put itin a view: <%= area_chart @hash %>