diff --git a/app/helpers/value_groups_helper.rb b/app/helpers/value_groups_helper.rb index c8e96d07..5362ed0c 100644 --- a/app/helpers/value_groups_helper.rb +++ b/app/helpers/value_groups_helper.rb @@ -1,17 +1,13 @@ module ValueGroupsHelper def value_group_pie_data(value_group) - value_group.children - .map do |child| - { - label: to_accountable_title(Accountable.from_type(child.name)), - percent_of_total: child.percent_of_total.round(1).to_f, - value: child.sum.amount.to_f, - currency: child.sum.currency.iso_code, - bg_color: accountable_bg_class(child.name), - fill_color: accountable_fill_class(child.name) - } - end - .filter { |child| child[:value] > 0 } - .to_json + value_group.children.filter { |c| c.sum > 0 }.map do |child| + { + label: to_accountable_title(Accountable.from_type(child.name)), + percent_of_total: child.percent_of_total.round(1).to_f, + formatted_value: format_money(child.sum, precision: 0), + bg_color: accountable_bg_class(child.name), + fill_color: accountable_fill_class(child.name) + } + end.to_json end end diff --git a/app/javascript/controllers/pie_chart_controller.js b/app/javascript/controllers/pie_chart_controller.js index f68fe5be..1bb41447 100644 --- a/app/javascript/controllers/pie_chart_controller.js +++ b/app/javascript/controllers/pie_chart_controller.js @@ -5,6 +5,7 @@ import * as d3 from "d3"; export default class extends Controller { static values = { data: Array, + total: String, label: String, }; @@ -38,7 +39,7 @@ export default class extends Controller { #draw() { this.#d3Container.attr("class", "relative"); - this.#d3Content.html(this.#contentSummaryTemplate(this.dataValue)); + this.#d3Content.html(this.#contentSummaryTemplate()); const pie = d3 .pie() @@ -75,23 +76,17 @@ export default class extends Controller { this.#d3Svg .selectAll(".arc path") .attr("class", (d) => d.data.fill_color); - this.#d3ContentMemo.html(this.#contentSummaryTemplate(this.dataValue)); + this.#d3ContentMemo.html(this.#contentSummaryTemplate()); }); } - #contentSummaryTemplate(data) { - const total = data.reduce((acc, cur) => acc + cur.value, 0); - const currency = data[0].currency; - - return `${this.#currencyValue({ - value: total, - currency, - })} ${this.labelValue}`; + #contentSummaryTemplate() { + return `${this.totalValue} ${this.labelValue}`; } #contentDetailTemplate(datum) { return ` - ${this.#currencyValue(datum)} + ${datum.formatted_value}
${currencyPrefix}${integerPart}.${fractionalPart}
`; - } - get #radius() { return Math.min(this.#d3ViewboxWidth, this.#d3ViewboxHeight) / 2; } diff --git a/app/views/pages/dashboard/_allocation_chart.html.erb b/app/views/pages/dashboard/_allocation_chart.html.erb index 4c665afb..f61573b6 100644 --- a/app/views/pages/dashboard/_allocation_chart.html.erb +++ b/app/views/pages/dashboard/_allocation_chart.html.erb @@ -10,8 +10,8 @@