Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase significant digits #6173

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function BarElectricityBreakdownChart({
const tickValue = isHourly ? formatPower(maxPower, 1) : formatEnergy(maxPower, 1);
return tickValue.toString().replace(/[\d.]+/, '0');
}
return isHourly ? formatPower(t) : formatEnergy(t);
return isHourly ? formatPower(t, 2) : formatEnergy(t, 2);
};
return (
<svg className="w-full overflow-visible" height={height}>
Expand Down
4 changes: 2 additions & 2 deletions web/src/features/charts/tooltips/ChartTooltips.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ it('Breakdown chart tooltip', () => {
it('Emmisions chart tooltip', () => {
cy.mount(<EmissionChartTooltip zoneDetail={zoneDetailMock} />);
cy.contains('Carbon emissions');
cy.contains(`1.4 kt of CO₂eq`);
cy.contains(`1.42 kt of CO₂eq`);
cy.contains('5');
});

Expand All @@ -52,6 +52,6 @@ it('Emmisions chart tooltip mobile', () => {
cy.viewport(500, 500);
cy.mount(<EmissionChartTooltip zoneDetail={zoneDetailMock} />);
cy.contains('Carbon emissions');
cy.contains(`1.4 kt of CO₂eq`);
cy.contains(`1.42 kt of CO₂eq`);
cy.contains('5');
});
22 changes: 11 additions & 11 deletions web/src/utils/formatting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,31 @@ describe('formatEnergy', () => {

it('handles default number kWh', () => {
const actual = formatEnergy(0.002_234_567);
const expected = '2.2 kWh';
const expected = '2.23 kWh';
expect(actual).toBe(expected);
});

it('handles MWh', () => {
const actual = formatEnergy(1.234_567);
const expected = '1.2 MWh';
const expected = '1.23 MWh';
expect(actual).toBe(expected);
});

it('handles GWh', () => {
const actual = formatEnergy(1222.234_567);
const expected = '1.2 GWh';
const expected = '1.22 GWh';
expect(actual).toBe(expected);
});

it('handles TWh', () => {
const actual = formatEnergy(1_222_000.234_567);
const expected = '1.2 TWh';
const expected = '1.22 TWh';
expect(actual).toBe(expected);
});

it('Converts PWh to TWh', () => {
const actual = formatEnergy(1_222_000_000.234_567);
const expected = '1200 TWh';
const expected = '1220 TWh';
expect(actual).toBe(expected);
});

Expand Down Expand Up @@ -79,25 +79,25 @@ describe('formatPower', () => {

it('handles default number kW', () => {
const actual = formatPower(0.002_234_567);
const expected = '2.2 kW';
const expected = '2.23 kW';
expect(actual).toBe(expected);
});

it('handles MW', () => {
const actual = formatPower(1.234_567);
const expected = '1.2 MW';
const expected = '1.23 MW';
expect(actual).toBe(expected);
});

it('handles GW', () => {
const actual = formatPower(1222.234_567);
const expected = '1.2 GW';
const expected = '1.22 GW';
expect(actual).toBe(expected);
});

it('handles TW', () => {
const actual = formatPower(1_222_000.234_567);
const expected = '1.2 TW';
const expected = '1.22 TW';
expect(actual).toBe(expected);
});

Expand Down Expand Up @@ -170,11 +170,11 @@ describe('formatCo2', () => {
});
it('handles megatonnes close to 1Gt rounding down', () => {
const actual = formatCo2(994_320_320_231_123);
const expected = '990 Mt';
const expected = '994 Mt';
expect(actual).toBe(expected);
});
it('handles values up to 1 trillion grams, rounding up', () => {
const actual = formatCo2(999_000_000_000_000);
const actual = formatCo2(999_900_000_000_000);
const expected = '1 Gt';
expect(actual).toBe(expected);
});
Expand Down
4 changes: 2 additions & 2 deletions web/src/utils/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as d3 from 'd3-format';
import { TimeAverages } from './constants';
import { EnergyUnits, PowerUnits } from './units';

const DEFAULT_NUM_DIGITS = 2;
const DEFAULT_NUM_DIGITS = 3;

function addSpaceBetweenNumberAndUnit(inputString: string) {
// Use a regular expression to add a space between the number and unit
Expand Down Expand Up @@ -61,7 +61,7 @@ export const formatCo2 = function (grams: number, valueToMatch?: number): string
return addSpaceBetweenNumberAndUnit(`${d3.format(`,.${decimals}~r`)(grams / 1e6)}t`);
}
// tonnes or above with significant figures as a default
return addSpaceBetweenNumberAndUnit(`${d3.format(',.2~s')(grams / 1e6)}t`);
return addSpaceBetweenNumberAndUnit(`${d3.format(',.3~s')(grams / 1e6)}t`);
};

const scalePower = function (maxPower: number | undefined, isPower = false) {
Expand Down