Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 58 additions & 5 deletions app/assets/javascripts/lib/views/table_view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,65 @@ class @TableView
_.template($("#chart-table-template").html(), {
data: @seriesData(),
totals: @totalsData(),
total1990: @total1990Data(),
has1990: @has1990(),
formatLabel: @options.labelFormatter(),
formatValue: @options.valueFormatter(),
formatTitle: @options.titleFormatter(),
startYear: App.settings.get('start_year'),
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.
Expand All @@ -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: ->
{
Expand Down
42 changes: 29 additions & 13 deletions app/views/output_elements/table_templates/_table_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,51 @@
<tr>
<td></td>
<th class="left"><%= I18n.t("output_elements.common.unit") %></th>
<% if (has1990) { %>
<th>1990</th>
<% } %>
<th><%= startYear %></th>
<th><%= endYear %></th>
</tr>
</thead>
<tbody>
<% _.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];
%>
<tr>
<th><%= formatLabel(row.serie) %></th>
<td class="left"><%= unit %></td>
<td title="<%= formatTitle(row.present) %>"><%= presentValue %></td>
<td title="<%= formatTitle(row.future) %>"><%= futureValue %></td>
<% if (has1990) { %>
<% if (row.year_1990 !== null) { %>
<td title="<%= formatTitle(row.year_1990) %>"><%= formatValue(row.year_1990).split(' ')[0] %></td>
<% } else { %>
<td>&mdash;</td>
<% } %>
<% } %>
<% if (row.present !== null) { %>
<td title="<%= formatTitle(row.present) %>"><%= formatValue(row.present).split(' ')[0] %></td>
<% } else { %>
<td>&mdash;</td>
<% } %>
<% if (row.future !== null) { %>
<td title="<%= formatTitle(row.future) %>"><%= formatValue(row.future).split(' ')[0] %></td>
<% } else { %>
<td>&mdash;</td>
<% } %>
</tr>
<% }) %>
</tbody>
<% 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) { %>
<tfoot>
<tr>
<th><%= I18n.t("output_elements.common.total") %></th>
<td class="left"><%= totalUnit %></td>
<td><%= totalPresentValue %></td>
<td><%= totalFutureValue %></td>
<td class="left"><%= formatValue(totals[0]).split(' ')[1] %></td>
<% if (has1990) { %>
<td><%= formatValue(total1990).split(' ')[0] %></td>
<% } %>
<td><%= formatValue(totals[0]).split(' ')[0] %></td>
<td><%= formatValue(totals[1]).split(' ')[0] %></td>
</tr>
</tfoot>
<% } %>
Expand Down