Skip to main content

Chart Size Convert

General • Asked by Emrah Yıldırım


I have a table called Download, the data in this table comes in bytes.
I want to prepare a chart with Chartkick. However, with the following code, the data comes in bytes. I want it to come in the form of Mb or Gb.

<%= line_chart Item.group_by_month(:created_at).sum(:download) %>

Can this code be used with something like number_to_human_size?


Is there anyone who can help me with this?



Hey Emrah,

I'm not really sure what you're after, did you try it? What was the error?

See the docs -



I've looked at this link before. Actually, there's no mistake. The data from the Download table comes in the form of bytes, and I want to show this data in the form of megabyte, gigabyte and Terebyte.

<% @tasks.each do |task| %>
   <tr class="gradeX">
     <td><%="%H:%M:%S") %></td>
      <td><%= number_to_human_size(task.downloads) %></td>
      <td><%= number_to_human_size(task.uploads) %></td>
 <% end %>

I managed to show it using Number_to_human_size on the index page with the code above.

However, when I want to display the same process with Chartkick gem file, the data appears as byte.

What's important is how do I add a method like Number_to_human_size to the following Chatkick code?

<%= line_chart Item.group_by_month(:created_at).sum(:download) %>


Ahh ok, I see what you're doing.

The best I can come up with is to take the return of your code and apply the number_to_human_size with an additional map like so:

<%= line_chart Item.group_by_month(:created_at).sum(:download).map{|k,v| [k, number_to_human_size(v)]} %>

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 18,000+ developers who get early access to new screencasts, articles, guides, updates, and more.

By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

More of a social being? We're also on Twitter and YouTube.