From 12aa32c3ae4af6b9e7ae09cb5876fc36f3006252 Mon Sep 17 00:00:00 2001 From: Kyra de Haan Date: Fri, 17 Jul 2026 14:21:36 +0200 Subject: [PATCH] Prototype of making 1990 values visible in CO2 charts --- .../javascripts/lib/views/table_view.coffee | 63 +++++++++++++++++-- .../table_templates/_table_template.html | 42 +++++++++---- 2 files changed, 87 insertions(+), 18 deletions(-) diff --git a/app/assets/javascripts/lib/views/table_view.coffee b/app/assets/javascripts/lib/views/table_view.coffee index b47cdccaa4..a264d08a94 100644 --- a/app/assets/javascripts/lib/views/table_view.coffee +++ b/app/assets/javascripts/lib/views/table_view.coffee @@ -10,6 +10,8 @@ class @TableView _.template($("#chart-table-template").html(), { data: @seriesData(), totals: @totalsData(), + total1990: @total1990Data(), + has1990: @has1990(), formatLabel: @options.labelFormatter(), formatValue: @options.valueFormatter(), formatTitle: @options.titleFormatter(), @@ -17,13 +19,56 @@ class @TableView endYear: App.settings.get('end_year'), }) + # True when the chart has 1990 series; the table then gains a 1990 column. + has1990: -> @model.year_1990_series().length > 0 + # Creates an array of hashes, each one containing the ChartSerie, and the - # present and future values. + # 1990 (when the chart has 1990 series), present, and future values. seriesData: -> - _.map @options.sorter()(@getSeries()), (serie) -> - serie: serie - present: serie.safe_present_value() - future: serie.safe_future_value() + rows = _.map @options.sorter()(@getSeries()), (serie) => + serie: serie + year_1990: @year1990Value(serie) + present: serie.safe_present_value() + future: serie.safe_future_value() + + extraRows = @unmatched1990Rows(rows) + return rows unless extraRows.length + + # Insert the extra 1990 rows above the target lines. + targetRows = _.filter(rows, (row) -> row.serie.get('is_target_line')) + normalRows = _.reject(rows, (row) -> row.serie.get('is_target_line')) + + normalRows.concat(extraRows, targetRows) + + # The 1990 value for a serie: target lines take the value of the target line + # at position 1990 with the same label, other series that of the 1990 serie + # with the same label. Null when the chart has neither. + year1990Value: (serie) -> + label = serie.get('label_key') + + match = if serie.get('is_target_line') + _.find @model.target_series(), (s) -> + s.get('label_key') == label && s.get('target_line_position') == '0' + else + _.find @model.year_1990_series(), (s) -> + s.get('label_key') == label + + if match then match.safe_present_value() else null + + # Rows for 1990 series which don't share a label with any other row, such as + # the consolidated 1990 series in the CO2 emissions chart. These only have a + # 1990 value. + unmatched1990Rows: (rows) -> + labels = _.map rows, (row) -> row.serie.get('label_key') + + unmatched = _.reject @model.year_1990_series(), (serie) -> + _.contains(labels, serie.get('label_key')) + + _.map unmatched, (serie) -> + serie: serie + year_1990: serie.safe_present_value() + present: null + future: null # Creates an array where the first element is the sum of the present values, # and the second is the sum of the future values. @@ -37,6 +82,14 @@ class @TableView else [] + # The sum of the 1990 values, or null when the chart has no 1990 series or + # shows no totals. + total1990Data: -> + if @options.showTotal() && @has1990() + _.sum(@model.values_1990()) + else + null + # Options which determine how to render each series in the table. renderingOptions: -> { diff --git a/app/views/output_elements/table_templates/_table_template.html b/app/views/output_elements/table_templates/_table_template.html index 2c620de1a8..a498cfaff9 100644 --- a/app/views/output_elements/table_templates/_table_template.html +++ b/app/views/output_elements/table_templates/_table_template.html @@ -4,35 +4,51 @@ <%= I18n.t("output_elements.common.unit") %> + <% if (has1990) { %> + 1990 + <% } %> <%= startYear %> <%= endYear %> <% _.each(data, function(row) { - var unit = formatValue(row.present).split(' ')[1]; - var presentValue = formatValue(row.present).split(' ')[0]; - var futureValue = formatValue(row.future).split(' ')[0]; + var reference = row.present !== null ? row.present : row.year_1990; + var unit = formatValue(reference).split(' ')[1]; %> <%= formatLabel(row.serie) %> <%= unit %> - <%= presentValue %> - <%= futureValue %> + <% if (has1990) { %> + <% if (row.year_1990 !== null) { %> + <%= formatValue(row.year_1990).split(' ')[0] %> + <% } else { %> + — + <% } %> + <% } %> + <% if (row.present !== null) { %> + <%= formatValue(row.present).split(' ')[0] %> + <% } else { %> + — + <% } %> + <% if (row.future !== null) { %> + <%= formatValue(row.future).split(' ')[0] %> + <% } else { %> + — + <% } %> <% }) %> - <% if (totals.length > 0) { - var totalUnit = formatValue(totals[0]).split(' ')[1]; - var totalPresentValue = formatValue(totals[0]).split(' ')[0]; - var totalFutureValue = formatValue(totals[1]).split(' ')[0]; - %> + <% if (totals.length > 0) { %> <%= I18n.t("output_elements.common.total") %> - <%= totalUnit %> - <%= totalPresentValue %> - <%= totalFutureValue %> + <%= formatValue(totals[0]).split(' ')[1] %> + <% if (has1990) { %> + <%= formatValue(total1990).split(' ')[0] %> + <% } %> + <%= formatValue(totals[0]).split(' ')[0] %> + <%= formatValue(totals[1]).split(' ')[0] %> <% } %>