reports_kit
reports_kit copied to clipboard
Separate large numbers in the table with commas
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.
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
Thank you @nsoseka. How would I go about that?
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.
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
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
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
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.
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