Overlay time series data in Chartkick

45 views Asked by At

With chartkick can I strip the date so only the time is left so the data can be overlaid on a 24 hour abscissa?

<% the_date_time_span = the_date..the_date + 23.hour %>
<% other_date_time_span = other_date..other_date + 23.hour %>

<%= line_chart [
  {name: "One day", data: HourByHour.where(datetime: other_date_time_span).pluck(:datetime, :enphase)},
  {name: "Another day", data: HourByHour.where(datetime: the_date_time_span).pluck(:datetime, :enphase)}      
  ] %>

or all in one statement

<%= line_chart [
      {name: "One day", data: Hour.where(datetime: other_date_time_span).pluck(:datetime, :enphase)},
      {name: "Another day", data: Hour.where(datetime: the_date_time_span).pluck(:datetime, :enphase)}      
      ]
    %>

where

 <% the_date_time_span = the_date..the_date + 23.hour %>
 <% other_date_time_span = other_date..other_date + 23.hour %>

As expected the abscissa is spread over both days. And they can be months apart.

As expected the abscissa is spread over both days

HourByHour.where(datetime: other_date_time_span).pluck(:datetime, :enphase) results in the following

[[Thu, 28 Dec 2023 00:00:00.000000000 PST -08:00, 0.0],
[Thu, 28 Dec 2023 01:00:00.000000000 PST -08:00, 0.0],
[Thu, 28 Dec 2023 02:00:00.000000000 PST -08:00, 0.0],
[Thu, 28 Dec 2023 03:00:00.000000000 PST -08:00, 0.0],
[Thu, 28 Dec 2023 04:00:00.000000000 PST -08:00, 0.0],
[Thu, 28 Dec 2023 05:00:00.000000000 PST -08:00, 0.0],
[Thu, 28 Dec 2023 06:00:00.000000000 PST -08:00, 0.0],
[Thu, 28 Dec 2023 07:00:00.000000000 PST -08:00, 72.0],
[Thu, 28 Dec 2023 08:00:00.000000000 PST -08:00, 520.0],
[Thu, 28 Dec 2023 09:00:00.000000000 PST -08:00, 1346.0],
[Thu, 28 Dec 2023 10:00:00.000000000 PST -08:00, 1802.0],
[Thu, 28 Dec 2023 11:00:00.000000000 PST -08:00, 2688.0],
[Thu, 28 Dec 2023 12:00:00.000000000 PST -08:00, 2771.0],
[Thu, 28 Dec 2023 13:00:00.000000000 PST -08:00, 2379.0],
[Thu, 28 Dec 2023 14:00:00.000000000 PST -08:00, 1320.0],
[Thu, 28 Dec 2023 15:00:00.000000000 PST -08:00, 803.0],
[Thu, 28 Dec 2023 16:00:00.000000000 PST -08:00, 109.0],
[Thu, 28 Dec 2023 17:00:00.000000000 PST -08:00, 0.0],
[Thu, 28 Dec 2023 18:00:00.000000000 PST -08:00, 0.0],
[Thu, 28 Dec 2023 19:00:00.000000000 PST -08:00, 0.0],
[Thu, 28 Dec 2023 20:00:00.000000000 PST -08:00, 0.0],
[Thu, 28 Dec 2023 21:00:00.000000000 PST -08:00, 0.0],
[Thu, 28 Dec 2023 22:00:00.000000000 PST -08:00, 0.0],
[Thu, 28 Dec 2023 23:00:00.000000000 PST -08:00, 0.0]]
1

There are 1 answers

3
Zegarek On

Ruby has Date and DateTime but doesn't offer actual Time (of day, without date part). To add one, you could consider Time-of-Day gem.

A workaround would be unifying the date across your timestamps so that they all end up overlaying on it, leaving only varying time of day. DB<>fiddle demo

.pluck('now()::date+datetime::time')

This would extract the time part from each of your values, placing it on the current date.