Search code examples
ruby-on-railsenumspie-chartchartkick

Display a custom string of enum status in a pie chart Rails


I'm using Chartkick Gem to display a pie chart about medications' statuses. The status its a enum field like the following

enum status: { good: 0, near_expiry: 1, expired: 2}

Currently im showing the next pie chart: https://i.sstatic.net/Z7bfe.png

Behind the graphic:

 <%= pie_chart @medications.group(:status).count %>

I want to display the statuses name with space instead of before defined in the dictionary. For example, "Good", "Near Expiry", "Expired". It is possible? How can i do that?


Solution

  • Since you're in a Rails project, you could use Hash#transform_keys:

    {"good" => 0, "near_expiry"=>1, "expired"=>2}.transform_keys do |key| 
      key.split('_').map(&:capitalize).join(' ')
    end
    => {"Good"=>0, "Near Expiry"=>1, "Expired"=>2}