Commit 53f6f9db526 for woocommerce
commit 53f6f9db5260601dac452fac1c062c28168b74eb
Author: Lazizbek Ergashev <lazerg2@gmail.com>
Date: Fri Dec 4 07:49:03 2026 +0500
Update the Analytics chart Y axis when the report currency changes (#71178)
* Update the Analytics chart Y axis when the report currency changes
* Add a WooCommerce changelog entry for the chart currency fix
* Give each chart its own number formatter instead of changing the global d3 locale
---------
Co-authored-by: Luigi Teschio <gigitux@gmail.com>
Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com>
diff --git a/packages/js/components/changelog/71158-chart-currency-update b/packages/js/components/changelog/71158-chart-currency-update
new file mode 100644
index 00000000000..d2f220cc4a9
--- /dev/null
+++ b/packages/js/components/changelog/71158-chart-currency-update
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Update the Analytics chart Y axis to the new currency when an extension changes the report currency.
diff --git a/packages/js/components/src/chart/d3chart/chart.js b/packages/js/components/src/chart/d3chart/chart.js
index 5f84a536959..5466940bcda 100644
--- a/packages/js/components/src/chart/d3chart/chart.js
+++ b/packages/js/components/src/chart/d3chart/chart.js
@@ -45,6 +45,7 @@ class D3Chart extends Component {
getFormatParams() {
const {
+ numberFormat,
screenReaderFormat,
xFormat,
x2Format,
@@ -59,8 +60,8 @@ class D3Chart extends Component {
),
xFormat: getFormatter( xFormat, d3TimeFormat ),
x2Format: getFormatter( x2Format, d3TimeFormat ),
- yBelow1Format: getFormatter( yBelow1Format ),
- yFormat: getFormatter( yFormat ),
+ yBelow1Format: getFormatter( yBelow1Format, numberFormat ),
+ yFormat: getFormatter( yFormat, numberFormat ),
};
}
@@ -118,6 +119,7 @@ class D3Chart extends Component {
createTooltip( chart, getColorFunction, visibleKeys ) {
const {
+ numberFormat,
tooltipLabelFormat,
tooltipPosition,
tooltipTitle,
@@ -130,7 +132,7 @@ class D3Chart extends Component {
tooltip.position = tooltipPosition;
tooltip.title = tooltipTitle;
tooltip.labelFormat = getFormatter( tooltipLabelFormat, d3TimeFormat );
- tooltip.valueFormat = getFormatter( tooltipValueFormat );
+ tooltip.valueFormat = getFormatter( tooltipValueFormat, numberFormat );
tooltip.visibleKeys = visibleKeys;
tooltip.getColor = getColorFunction;
this.tooltip = tooltip;
@@ -299,6 +301,11 @@ D3Chart.propTypes = {
* ARIA properties.
*/
mode: PropTypes.oneOf( [ 'item-comparison', 'time-comparison' ] ),
+ /**
+ * A number formatting function, used instead of d3Format when `yFormat`, `yBelow1Format`
+ * or `tooltipValueFormat` is a string.
+ */
+ numberFormat: PropTypes.func,
/**
* A datetime formatting string or overriding function to format the screen reader labels.
*/
diff --git a/packages/js/components/src/chart/d3chart/legend.js b/packages/js/components/src/chart/d3chart/legend.js
index 9ae1d320175..62851b35df0 100644
--- a/packages/js/components/src/chart/d3chart/legend.js
+++ b/packages/js/components/src/chart/d3chart/legend.js
@@ -58,6 +58,7 @@ class D3Legend extends Component {
interactive,
legendDirection,
legendValueFormat,
+ numberFormat,
instanceId,
totalLabel,
} = this.props;
@@ -150,9 +151,10 @@ class D3Legend extends Component {
{ row.label }
</span>
<span className="woocommerce-legend__item-total">
- { getFormatter( legendValueFormat )(
- row.total
- ) }
+ { getFormatter(
+ legendValueFormat,
+ numberFormat
+ )( row.total ) }
</span>
</div>
</button>
@@ -205,6 +207,11 @@ D3Legend.propTypes = {
PropTypes.string,
PropTypes.func,
] ),
+ /**
+ * A number formatting function, used instead of d3Format when `legendValueFormat`
+ * is a string.
+ */
+ numberFormat: PropTypes.func,
/**
* Label to describe the legend items. It will be displayed in the legend of
* comparison charts when there are many.
diff --git a/packages/js/components/src/chart/index.js b/packages/js/components/src/chart/index.js
index ed07ebb2909..138dd567c0f 100644
--- a/packages/js/components/src/chart/index.js
+++ b/packages/js/components/src/chart/index.js
@@ -9,7 +9,7 @@ import {
createRef,
Fragment,
} from '@wordpress/element';
-import { formatDefaultLocale as d3FormatDefaultLocale } from 'd3-format';
+import { formatLocale as d3FormatLocale } from 'd3-format';
import { isEqual, partial, without } from 'lodash';
import LineGraphIcon from 'gridicons/dist/line-graph';
import StatsAltIcon from 'gridicons/dist/stats-alt';
@@ -64,6 +64,7 @@ class Chart extends Component {
this.updateDimensions = this.updateDimensions.bind( this );
this.getVisibleData = memoize( this.getVisibleData );
this.getOrderedKeys = memoize( this.getOrderedKeys );
+ this.getNumberFormat = memoize( this.getNumberFormat, isEqual );
this.setInterval = this.setInterval.bind( this );
}
@@ -99,7 +100,6 @@ class Chart extends Component {
componentDidMount() {
this.updateDimensions();
- this.setD3DefaultFormat();
window.addEventListener( 'resize', this.updateDimensions );
}
@@ -107,19 +107,19 @@ class Chart extends Component {
window.removeEventListener( 'resize', this.updateDimensions );
}
- setD3DefaultFormat() {
+ getNumberFormat( currency ) {
const {
symbol: currencySymbol,
symbolPosition,
decimalSeparator: decimal,
thousandSeparator: thousands,
- } = this.props.currency;
- d3FormatDefaultLocale( {
+ } = currency;
+ return d3FormatLocale( {
decimal,
thousands,
grouping: [ 3 ],
currency: getD3CurrencyFormat( currencySymbol, symbolPosition ),
- } );
+ } ).format;
}
getOrderedKeys( focusedKeys, visibleKeys, selectedIds = [] ) {
@@ -294,6 +294,7 @@ class Chart extends Component {
const {
baseValue,
chartType,
+ currency,
data,
dateParser,
emptyMessage,
@@ -317,6 +318,7 @@ class Chart extends Component {
yBelow1Format,
yFormat,
} = this.props;
+ const numberFormat = this.getNumberFormat( currency );
const selectedIds = filterParam
? getIdsFromQuery( query[ filterParam ] )
: [];
@@ -350,6 +352,7 @@ class Chart extends Component {
interactive={ interactiveLegend }
legendDirection={ legendDirection }
legendValueFormat={ tooltipValueFormat }
+ numberFormat={ numberFormat }
totalLabel={ totalLabel }
/>
);
@@ -451,6 +454,7 @@ class Chart extends Component {
) }
{ ! isRequesting && width > 0 && (
<D3Chart
+ key={ JSON.stringify( currency ) }
baseValue={ baseValue }
chartType={ chartType }
colorScheme={ d3InterpolateViridis }
@@ -461,6 +465,7 @@ class Chart extends Component {
interval={ interval }
margin={ margin }
mode={ mode }
+ numberFormat={ numberFormat }
orderedKeys={ orderedKeys }
screenReaderFormat={ screenReaderFormat }
tooltipLabelFormat={ tooltipLabelFormat }
diff --git a/packages/js/components/src/chart/test/currency.js b/packages/js/components/src/chart/test/currency.js
new file mode 100644
index 00000000000..dd4b9829860
--- /dev/null
+++ b/packages/js/components/src/chart/test/currency.js
@@ -0,0 +1,90 @@
+/**
+ * @jest-environment jsdom
+ */
+/**
+ * External dependencies
+ */
+import { render } from '@testing-library/react';
+import { createElement, Fragment } from '@wordpress/element';
+
+/**
+ * Internal dependencies
+ */
+import Chart from '../';
+
+const data = [
+ {
+ date: '2018-05-30T00:00:00',
+ Revenue: {
+ label: 'Revenue',
+ value: 21599,
+ },
+ },
+ {
+ date: '2018-05-31T00:00:00',
+ Revenue: {
+ label: 'Revenue',
+ value: 14205,
+ },
+ },
+];
+
+const usd = {
+ symbol: '$',
+ symbolPosition: 'left',
+ decimalSeparator: '.',
+ thousandSeparator: ',',
+};
+
+const eur = {
+ symbol: '€',
+ symbolPosition: 'right_space',
+ decimalSeparator: ',',
+ thousandSeparator: '.',
+};
+
+const getYAxisLabels = ( container ) =>
+ Array.from( container.querySelectorAll( '.axis.y-axis .tick text' ) ).map(
+ ( tick ) => tick.textContent
+ );
+
+describe( 'Chart currency', () => {
+ beforeEach( () => {
+ jest.spyOn(
+ window.HTMLElement.prototype,
+ 'offsetWidth',
+ 'get'
+ ).mockReturnValue( 600 );
+ } );
+
+ test( 'should format the Y axis with the new currency when the currency changes', () => {
+ const { container, rerender } = render(
+ <Chart data={ data } currency={ usd } valueType="currency" />
+ );
+ expect( getYAxisLabels( container ).join( ' ' ) ).toContain( '$' );
+
+ rerender(
+ <Chart data={ data } currency={ eur } valueType="currency" />
+ );
+ const labels = getYAxisLabels( container ).join( ' ' );
+ expect( labels ).toContain( '€' );
+ expect( labels ).not.toContain( '$' );
+ } );
+
+ test( 'should keep the currency of each chart separate', () => {
+ const { container } = render(
+ <Fragment>
+ <Chart data={ data } currency={ usd } valueType="currency" />
+ <Chart data={ data } currency={ eur } valueType="currency" />
+ </Fragment>
+ );
+ const [ usdChart, eurChart ] =
+ container.querySelectorAll( '.woocommerce-chart' );
+ const usdLabels = getYAxisLabels( usdChart ).join( ' ' );
+ const eurLabels = getYAxisLabels( eurChart ).join( ' ' );
+ expect( usdLabels ).toContain( '$' );
+ expect( usdLabels ).not.toContain( '€' );
+ expect( eurLabels ).toContain( '€' );
+ expect( eurLabels ).not.toContain( '$' );
+ } );
+} );
diff --git a/plugins/woocommerce/changelog/71158-chart-currency-update b/plugins/woocommerce/changelog/71158-chart-currency-update
new file mode 100644
index 00000000000..d2f220cc4a9
--- /dev/null
+++ b/plugins/woocommerce/changelog/71158-chart-currency-update
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Update the Analytics chart Y axis to the new currency when an extension changes the report currency.