Skip to content

Commit 32b921f

Browse files
committed
CP Changes 2: addendum 2
1 parent 5ae3d67 commit 32b921f

4 files changed

Lines changed: 75 additions & 16 deletions

File tree

app/assets/stylesheets/tpi/pages/company.scss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,14 @@
524524
}
525525
}
526526

527+
&__metric-label {
528+
font-size: 14px;
529+
color: #666666;
530+
margin-bottom: 0.75rem;
531+
font-weight: 500;
532+
text-align: center;
533+
}
534+
527535
&__assumptions {
528536
padding: 15px 30px;
529537

app/services/api/charts/cp_assessment.rb

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ def emissions_data
6666
# ]
6767
def emissions_data_from_assessment
6868
data = if assessment&.emissions&.size == 1
69-
data_with_marker_settings
69+
format_emissions_data_with_marker_settings(assessment&.emissions)
7070
else
71-
assessment&.emissions&.transform_keys(&:to_i)
71+
format_emissions_data(assessment&.emissions || {})
7272
end
7373
{
7474
name: company_series_name,
@@ -82,18 +82,6 @@ def emissions_data_from_assessment
8282
}
8383
end
8484

85-
def data_with_marker_settings
86-
[{
87-
y: assessment&.emissions&.first&.second,
88-
x: assessment&.emissions&.first&.first&.to_i,
89-
marker: {
90-
symbol: 'circle',
91-
enabled: true,
92-
radius: 3
93-
}
94-
}]
95-
end
96-
9785
def years_with_targets
9886
return unless assessment&.years_with_targets&.any?
9987
return unless assessment&.emissions&.any?
@@ -151,11 +139,31 @@ def emissions_data_from_sector_benchmarks
151139
name: benchmark.scenario,
152140
sector: sector.name,
153141
subsector: benchmark.subsector,
154-
data: benchmark.emissions.transform_keys(&:to_i)
142+
data: format_emissions_data(benchmark.emissions)
155143
}
156144
end.reverse
157145
end
158146

147+
def format_emissions_data(emissions_hash)
148+
emissions_hash.transform_keys(&:to_i).transform_values do |value|
149+
value.present? ? value.round(2) : nil
150+
end
151+
end
152+
153+
def format_emissions_data_with_marker_settings(emissions_hash)
154+
value = emissions_hash.first&.second
155+
156+
[{
157+
y: value.present? ? value.round(2) : nil,
158+
x: emissions_hash.first&.first&.to_i,
159+
marker: {
160+
symbol: 'circle',
161+
enabled: true,
162+
radius: 3
163+
}
164+
}]
165+
end
166+
159167
def sector_benchmarks_for_chart
160168
selected_region = regional_view? ? assessment.region : nil
161169
match_key = assessment.subsector_name

app/views/tpi/companies/_cp_assessment.html.erb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,18 @@
77
<% end %>
88

99
<% if assessment.emissions.any? %>
10+
<% if assessment.company.sector.name == 'Chemicals' %>
11+
<div class="carbon-performance__metric-label">
12+
Emissions intensity (tCO2e/US$1,000 (2019))
13+
</div>
14+
<% end %>
15+
1016
<%= react_component('charts/cp-performance', {
1117
dataUrl: emissions_chart_data_tpi_company_path(assessment.company, cp_assessment_id: assessment.id, view: params[:view]),
1218
unit: assessment.unit,
1319
companySelector: false,
14-
sectorUrl: tpi_sector_path(assessment.company.sector.slug)
20+
sectorUrl: tpi_sector_path(assessment.company.sector.slug),
21+
sectorName: assessment.company.sector.name
1522
}) %>
1623
<% else %>
1724
<div class="notification" style="text-align: center; padding: 2rem; color: #6b7280; font-style: italic;">

spec/services/api/charts/cp_assessment_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,4 +329,40 @@
329329
end
330330
end
331331
end
332+
333+
describe 'decimal formatting' do
334+
let(:sector) { create(:tpi_sector, name: 'Test Sector', categories: %w[Company]) }
335+
let(:company) { create(:company, :published, sector: sector) }
336+
let!(:benchmark) do
337+
create(:cp_benchmark,
338+
sector: sector,
339+
category: 'Company',
340+
scenario: '1.5 Degrees',
341+
release_date: 6.months.ago,
342+
emissions: {'2020' => 123.456789, '2021' => 456.789012})
343+
end
344+
let(:assessment) do
345+
create(:cp_assessment,
346+
sector: sector,
347+
cp_assessmentable: company,
348+
publication_date: 3.months.ago,
349+
assessment_date: 3.months.ago,
350+
last_reported_year: 2021,
351+
emissions: {'2020' => 100.123456, '2021' => 200.987654})
352+
end
353+
354+
subject { described_class.new(assessment, 'global') }
355+
356+
it 'formats benchmark emissions to 2 decimal places' do
357+
benchmark_data = subject.emissions_data.find { |s| s[:type] == 'area' }[:data]
358+
expect(benchmark_data[2020]).to eq(123.46)
359+
expect(benchmark_data[2021]).to eq(456.79)
360+
end
361+
362+
it 'formats company emissions to 2 decimal places' do
363+
company_data = subject.emissions_data.find { |s| s[:name] == company.name }[:data]
364+
expect(company_data[2020]).to eq(100.12)
365+
expect(company_data[2021]).to eq(200.99)
366+
end
367+
end
332368
end

0 commit comments

Comments
 (0)