Skip to main content

Chart Size Convert

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

Hi

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?


799dbaadbef34693ed42a2c51862c852

Is there anyone who can help me with this?

Respects


Dda4fed833fa3b14b5aaa24846f7debb

Hey Emrah,

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

See the docs - https://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#method-i-number_to_human_size


799dbaadbef34693ed42a2c51862c852

Hey

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><%= Time.at(task.intime).utc.strftime("%H:%M:%S") %></td>
      <td><%= number_to_human_size(task.downloads) %></td>
      <td><%= number_to_human_size(task.uploads) %></td>
   </tr>
 <% 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) %>


Dda4fed833fa3b14b5aaa24846f7debb

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.