Adam12344
Adam12344

Reputation: 1053

Rails Chartkick: Date format in column_chart

I'm using Chartkick in Rails and it looks like the column_chart date format is different than the line_chart date format. I've checked the data that I'm putting in and it is identical between the charts.

The line chart has an x-axis (which I want) that looks like: Nov 2016, Jan 2017, Mar 2017

The column chart: 2016-11-01, 2016-12-01, 2017-01-01...

<%= line_chart [
    {name: "Posts", data: @posts_over_time },
    {name: "Comments", data: @comments_over_time }
    ],
    xtitle: "Date",
    ytitle: "Engagement"
%>

<%= column_chart [
    {name: "Accepted", data: @number_charges_accepted },
    {name: "Declined", data: @number_charges_declined }
    ],
    stacked: true,
    xtitle: "Date",
    ytitle: "Transactions",
    # Additions I've tried:
    library: {
    format: 'MM/YY'
    },
    discrete: true
%>

Upvotes: 0

Views: 1265

Answers (1)

Adam12344
Adam12344

Reputation: 1053

Got it working based off advice here: https://github.com/ankane/chartkick/issues/352

@number_charges_declined = PastTransaction.where(project_id: @project.id).where(status: 'failed').group_by_month(:created_at, last: 6).count
@number_charges_accepted = PastTransaction.where(project_id: @project.id).where(status: 'successful').group_by_month(:created_at, last: 6).count
# Format dates for column_chart
@number_charges_declined = @number_charges_declined.transform_keys.with_index{ |key, i| if i % 2 == 0 then key.strftime("%b %Y") else " "*i end }
@number_charges_accepted = @number_charges_accepted.transform_keys.with_index{ |key, i| if i % 2 == 0 then key.strftime("%b %Y") else " "*i end }

Upvotes: 1

Related Questions