Search code examples
ruby-on-railschartkick

How to plot a column chart rails


In the code below, I would like to increment variable a, then at the end, I would like to plot the graph by using the variable a.

<% a = 0 %>
<% something.each do |some|%>
  <% a = a + 1%>
<% end %>
<%= column_chart [{name: "column 1", data: "a"} ]%>

When I do it now, the graph does not plot anything.


Solution

  • First, you need to create the chart data. Then you need to pass this data to column_chart as an argument. The following example, a is actually the index of the somethings collection.

    data = []
    somethings.each_with_index do |something, a|
      data << [someting.name, a]
    end
    
    # data => [['name1', 0], ['name2', 1], ['name3', 2], ..]
    
    column_chart data