Skip to main content

Set a start date for Chartkick / Highcharts combination

Rails • Asked by Stan Smith

This is driving me nuts.
I'm trying to set a dynamic start date of 1 month ago for a line chart using Chartkick / HighCharts.
But can't find the right syntax.


What do you have right now?


This is my helper:
def core7_by_month

line_chart User.group_by_day(:created_at).count, library: {
title: {text: 'Church members by month', x: -20},
chart: {
type: 'spline'
},
yAxis: {
crosshair: true,
allowDecimals: false,
title: {
text: 'Church Subscribers count'
}
},
xAxis: {
crosshair: true,
title: {
text: 'Year'
}
},
plotOptions: {
line: {
pointStart: 1.month.ago
}
}
}
end

This is in the view
<%= core7_by_month %>

I found the answer.
The date range parameter is handled by the groupbydate gem.
I needed to do this:
line_chart User.group_by_day(:created_at, range: 1.month.ago.midnight..Time.now).count


Perfect. Yeah the groupdate gem is handy for that stuff. You can write it yourself, but it's pretty annoying to remember it all. Works super nice with Chartkick.


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.