I am fairly new to ruby and playing with ChartKick and not seeing any lines on the chart. I allow users to enter numbers 1-10 and I want to display all those values in a line_chart. I am returning all the values below in the loop and I want those numbers in the chart as well.
I know I am getting the data now because the colors are showing on the right for each value listed below. I have attached a screen shot to help. I would appreciate any help I can get. Thanks in advance!
Here is my view
<div class="col-xs-6">
<h3>Numbers Added</h3>
<%= line_chart @numbers, {height: "400px", library: {hAxis: {title: "All Entered Numbers"}, vAxis: {title: "Numbers", viewWindow: {min: 0, max:10}}}} %>
</div>
</div>
<table>
<tr>
<th>All Numbers</th>
</tr>
<% @numbers.each do |number| %>
<tr>
<td><%= number.value %></td>
<td><%= link_to 'Show', number_path(number) %></td>
<td><%= link_to 'Edit', edit_number_path(number) %></td>
<td><%= link_to 'Destroy', number_path(number),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<%= link_to 'Add new number', new_number_path %>
</table>
Here is my controller
class NumbersController < ApplicationController
def index
@numbers = Number.all
end
Screen shot:
A line chart is basically just a X-Y Coordinate System.
To Draw a Point you need an X and an Y Coordinate. But you gave only 1 coordinate in an Array like this:
What you actually need is an Hash like this:
To achiev that you can use following code:
This does multiple things at once. First it creates an Array with the numbers from 0 to the count (Exlusive the count) of your data:
The next step is to collect all values from your numbers, @number is actually an array of ActiveRecord Objects, but you only want to have the values:
This function calls values on every object in @numbers and creates an Array with the results. Now we have two Arrays:
The .zip combines them, by taking one of each into an new Array, now we have the following:
No we can just cast this into a hash with Hash[...] and voila we have this: