0

I have column vote_option belong to Vote table and title in VoteOption table and now I want to show vote_option and title in pair in a pie chart(chartkick). So what exactly should I do? I mean what should I do with this code: <%= pie_chart Goal.group(:name).count %> to show vote_option and title like value and key of a hash?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
DinhNgocHien
  • 707
  • 3
  • 17
  • 34
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example.](http://stackoverflow.com/help/mcve) – Jim Garrison Mar 28 '15 at 04:10

1 Answers1

0

Have you tried creating a table that references the data in the two tables? For example you could combine the columns (reference by ID) from the two tables into a model table called Vote_title.

Then in your ruby code you could do something like the following:
In your controller (assuming this is on your index page):

def index
   @vote_titles = Vote_title.all
end

On your index page:

<%= pie_chart @vote_titles.group(:title).sum('vote_option') %>

In that example, the pie chart would display each title as a slice/percentage of 'vote_option' relative to the other title's vote_options. The legend on the side would list all titles and their colors. Reverse title, and vote_option in the erb code on your index page if you want the opposite.

jtlindsey
  • 4,346
  • 4
  • 45
  • 73