Skip to content

Commit

Permalink
Set ticks on y axis to -£100 instead of £-100 (#820)
Browse files Browse the repository at this point in the history
* search box throws error message on typing. fixed.

* re-register locales en and en-us with Plotly

* fix issue 148.

* Lint

---------

Co-authored-by: Nikhil Woodruff <[email protected]>
  • Loading branch information
abhcs and nikhilwoodruff authored Oct 27, 2023
1 parent c442f8c commit 877fe03
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 23 deletions.
12 changes: 12 additions & 0 deletions src/api/language.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,15 @@ export function cardinal(number) {
const rem = number % 100;
return number + (suffixes[(rem - 20) % 10] || suffixes[rem] || suffixes[0]);
}

export function currencyString(number, metadata, maximumFractionDigits) {
return number.toLocaleString("en-US", {
style: "currency",
currency: metadata.countryId === "uk" ? "GBP" : "USD",
maximumFractionDigits: maximumFractionDigits,
});
}

export function localeString(metadata) {
return metadata.countryId === "uk" ? "en" : "en-US";
}
12 changes: 4 additions & 8 deletions src/pages/policy/output/AverageImpactByDecile.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useContext } from "react";
import Plot from "react-plotly.js";
import { ChartLogo } from "../../../api/charts";
import { cardinal } from "../../../api/language";
import { cardinal, currencyString, localeString } from "../../../api/language";
import { formatVariableValue } from "../../../api/variables";
import HoverCard, { HoverCardContext } from "../../../layout/HoverCard";
import useMobile from "../../../layout/Responsive";
Expand Down Expand Up @@ -33,11 +33,7 @@ export default function AverageImpactByDecile(props) {
value < 0 ? style.colors.DARK_GRAY : style.colors.BLUE,
),
},
text: yArray.map(
(value) =>
metadata.currency +
value.toLocaleString("en-GB", { maximumFractionDigits: 0 }),
),
text: yArray.map((value) => currencyString(value, metadata, 0)),
textangle: 0,
...(useHoverCard
? {
Expand Down Expand Up @@ -77,8 +73,7 @@ export default function AverageImpactByDecile(props) {
},
yaxis: {
title: "Average change",
tickprefix: metadata.countryId === "uk" ? "£" : "$",
tickformat: ",.0f",
tickformat: "$,.0f",
},
...(useHoverCard
? {}
Expand Down Expand Up @@ -107,6 +102,7 @@ export default function AverageImpactByDecile(props) {
config={{
displayModeBar: false,
responsive: true,
locale: localeString(metadata),
}}
style={{
width: "100%",
Expand Down
12 changes: 4 additions & 8 deletions src/pages/policy/output/AverageImpactByWealthDecile.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useContext } from "react";
import Plot from "react-plotly.js";
import { ChartLogo } from "../../../api/charts";
import { cardinal } from "../../../api/language";
import { cardinal, currencyString, localeString } from "../../../api/language";
import { formatVariableValue } from "../../../api/variables";
import HoverCard, { HoverCardContext } from "../../../layout/HoverCard";
import useMobile from "../../../layout/Responsive";
Expand Down Expand Up @@ -33,11 +33,7 @@ export default function AverageImpactByWealthDecile(props) {
value < 0 ? style.colors.DARK_GRAY : style.colors.BLUE,
),
},
text: yArray.map(
(value) =>
metadata.currency +
value.toLocaleString("en-GB", { maximumFractionDigits: 0 }),
),
text: yArray.map((value) => currencyString(value, metadata, 0)),
textangle: 0,
...(useHoverCard
? {
Expand Down Expand Up @@ -77,8 +73,7 @@ export default function AverageImpactByWealthDecile(props) {
},
yaxis: {
title: "Average change",
tickprefix: metadata.countryId === "uk" ? "£" : "$",
tickformat: ",.0f",
tickformat: "$,.0f",
},
...(useHoverCard
? {}
Expand Down Expand Up @@ -107,6 +102,7 @@ export default function AverageImpactByWealthDecile(props) {
config={{
displayModeBar: false,
responsive: true,
locale: localeString(metadata),
}}
style={{
width: "100%",
Expand Down
6 changes: 3 additions & 3 deletions src/pages/policy/output/BudgetaryImpact.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useContext } from "react";
import Plot from "react-plotly.js";
import { ChartLogo } from "../../../api/charts";
import { aggregateCurrency } from "../../../api/language";
import { aggregateCurrency, localeString } from "../../../api/language";
import HoverCard, { HoverCardContext } from "../../../layout/HoverCard";
import useMobile from "../../../layout/Responsive";
import DownloadableScreenshottable from "./DownloadableScreenshottable";
Expand Down Expand Up @@ -129,8 +129,7 @@ export default function BudgetaryImpact(props) {
},
yaxis: {
title: "Budgetary impact (bn)",
tickprefix: metadata.currency,
tickformat: ",.1f",
tickformat: "$,.1f",
},
...(useHoverCard
? {}
Expand All @@ -157,6 +156,7 @@ export default function BudgetaryImpact(props) {
config={{
displayModeBar: false,
responsive: true,
locale: localeString(metadata),
}}
style={{
width: "100%",
Expand Down
6 changes: 3 additions & 3 deletions src/pages/policy/output/DetailedBudgetaryImpact.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useContext } from "react";
import Plot from "react-plotly.js";
import { ChartLogo } from "../../../api/charts";
import { aggregateCurrency } from "../../../api/language";
import { aggregateCurrency, localeString } from "../../../api/language";
import HoverCard, { HoverCardContext } from "../../../layout/HoverCard";
import useMobile from "../../../layout/Responsive";
import Screenshottable from "../../../layout/Screenshottable";
Expand Down Expand Up @@ -100,8 +100,7 @@ export default function DetailedBudgetaryImpact(props) {
},
yaxis: {
title: "Budgetary impact (bn)",
tickprefix: metadata.currency,
tickformat: ",.1f",
tickformat: "$,.1f",
},
...(useHoverCard
? {}
Expand Down Expand Up @@ -130,6 +129,7 @@ export default function DetailedBudgetaryImpact(props) {
config={{
displayModeBar: false,
responsive: true,
locale: localeString(metadata),
}}
style={{
width: "100%",
Expand Down
12 changes: 12 additions & 0 deletions src/plotly_locales/locale-en-us.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// same as plotly.js/src/locale-en-us.js except currency symbol

"use strict";

module.exports = {
moduleType: "locale",
name: "en-US",
format: {
date: "%m/%d/%Y",
currency: ["$", ""],
},
};
63 changes: 63 additions & 0 deletions src/plotly_locales/locale-en.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// same as plotly.js/src/locale-en.js except currency symbol

"use strict";

module.exports = {
moduleType: "locale",
name: "en",
dictionary: {
"Click to enter Colorscale title": "Click to enter Colourscale title",
},
format: {
days: [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
],
shortDays: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
months: [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
],
shortMonths: [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
],
periods: ["AM", "PM"],
dateTime: "%a %b %e %X %Y",
date: "%d/%m/%Y",
time: "%H:%M:%S",
decimal: ".",
thousands: ",",
grouping: [3],
currency: ["£", ""],
year: "%Y",
month: "%b %Y",
dayMonth: "%b %-d",
dayMonthYear: "%b %-d, %Y",
},
};
5 changes: 5 additions & 0 deletions src/redesign/components/PolicyEngine.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import Header from "./Header";
import Testimonials from "./Testimonials";
import CalculatorInterstitial from "./CalculatorInterstitial";
import CitizensEconomicCouncil from "./CitizensEconomicCouncil";
import loc_en from "../../plotly_locales/locale-en.js";
import loc_en_us from "../../plotly_locales/locale-en-us.js";

const PolicyPage = lazy(() => import("../../pages/PolicyPage"));
const HouseholdPage = lazy(() => import("../../pages/HouseholdPage"));
Expand All @@ -34,6 +36,9 @@ function ScrollToTop() {
}

export default function PolicyEngine({ pathname }) {
var Plotly = require("plotly.js/dist/plotly.js");
Plotly.register(loc_en);
Plotly.register(loc_en_us);
const COUNTRIES = ["us", "uk", "ca", "ng", "il"];

// First, check if the country is specified (.org/[country]/...)
Expand Down
2 changes: 1 addition & 1 deletion src/redesign/components/TextBox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default function TextBox({
placeholder={placeholder}
id={id}
enterKeyHint={enterKeyHint}
onChange={(e) => onChange(e.target.value)}
onChange={(e) => onChange?.(e.target.value)}
style={{
border: "none",
borderBottom: `1px solid ${style.colors.WHITE}`,
Expand Down

0 comments on commit 877fe03

Please sign in to comment.