reports_kit icon indicating copy to clipboard operation
reports_kit copied to clipboard

Separate large numbers in the table with commas

Open billy-whizz opened this issue 5 years ago • 8 comments

Hi, is there a way to display large figures in the table with a comma to make them easy to understand at first glace? I know in the views I could have used the number_with_delimiter method, but am not sure how that works with the charts.

billy-whizz avatar Oct 05 '19 16:10 billy-whizz

i think to make things easy for you you will have to write a custom data searching method and format the numbers the way you want before passing it onto the gem for rendering. If you need assistance on how to go about that let me know

nsoseka avatar Oct 05 '19 17:10 nsoseka

Thank you @nsoseka. How would I go about that?

billy-whizz avatar Oct 05 '19 17:10 billy-whizz

So you check out the docs on how to setup custom data methods. checkout this link https://www.reportskit.co/subcategories/data_methods Then you can format your data as you normally would in rails or ruby, in your custom method.

nsoseka avatar Oct 05 '19 19:10 nsoseka

Thanks for pointing that out. In app/services/my_data_methods.rb I have used this method: def self.delimit_figures ActionController::Base.helpers.number_to_currency(authors.salary, precision: 0) end

Not much success though

billy-whizz avatar Oct 06 '19 06:10 billy-whizz

okay require the number_to_currency_helper

require "active_support/number_helper/number_converter"

  module Helpers
    extend ActiveSupport::NumberHelper
  end

Define a custom number_to_currency method

  def number_to_currency(amount)
    Helpers.number_to_currency(amount)
  end

This method you can then call within your custom data method. If you need more support you can post your entire data method if you will, so that i can show you how to navigate using the method in it

nsoseka avatar Oct 07 '19 08:10 nsoseka

Thanks a lot!

After requiring the helper, here is the method in my_data_methods.rb:

def self.delimit_figures Author.number_to_currency(salary) end Any ideas are welcome.,thanks

billy-whizz avatar Oct 07 '19 20:10 billy-whizz

This method returns the value of Author.number_to_currency(salary) and not a JSON object. So the view of reports_kit can't render it. Because you have a custom data method, you need to do the querying yourself and then format the data before return it as JSON. So if I had an idea of the type of data you were trying to retrieve then i could be of more help.

nsoseka avatar Oct 08 '19 07:10 nsoseka

Hi, so I recently returned to this but yet to get a solution. I think am yet to get the hang of it. My authors table has a salary field, with data type as decimal. In the views, I have a chart which shows a summary of authors' salaries by country. I would like the totals/figures to be well formatted for easy understanding. My my_data_methods.rb looks like this now:

class MyDataMethods

 require "active_support/number_helper/number_converter"

  module Helpers
    extend ActiveSupport::NumberHelper
  end

  def number_to_currency(amount)
    Helpers.number_to_currency(amount)
  end

   def self.delimit_figures(properties)
    salaries = Author.salary 
    salaries.number_to_currency 
  end

end

My salary_chart.yml looks like this:

--- 
data_method: MyDataMethods.delimit_figures
dimensions: 
  - country
  - genre
filters: 
  - country
  - genre
format: table
measure: 
  aggregation: total_salary
  key: author
  name: "Total Salaries by Country"
report_options: 
  aggregations: 
    - 
      from: columns
      label: Total
      operator: sum
    - 
      from: rows
      label: Total
      operator: sum

In the view:

<%= render_report 'salary_chart' do |report| %>
  <%= report.form do |f| %>
   <%= f.multi_autocomplete :country, placeholder: 'Genre...' %>
    <%= f.multi_autocomplete :genre, placeholder: 'County...' %>
  <% end %>

I think the only task is in the data method part. Thanks

billy-whizz avatar Oct 26 '19 08:10 billy-whizz