fix(echarts): Apply D3 locale formatting to adaptive datetime x-axis

Fixes #31790

When the "Adaptive" datetime format option was selected for x-axis in ECharts
visualizations, the custom D3 locale settings (like localized month names) were
being ignored. This was because getXAxisFormatter() was returning undefined for
SMART_DATE_ID, causing ECharts to use its default formatter instead of the
configured D3 locale formatter.

The fix ensures that when SMART_DATE_ID is selected, the smart date formatter
with proper locale support is returned, allowing custom D3 time formats with
localized month and day names to work correctly with the adaptive option.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Evan Rusackas
2025-08-12 11:43:18 -07:00
parent 01ac966b83
commit 6cb9819d10
3 changed files with 14989 additions and 16486 deletions

View File

@@ -16,8 +16,16 @@
* specific language governing permissions and limitations
* under the License.
*/
import { NumberFormats } from '@superset-ui/core';
import { getPercentFormatter } from '../../src/utils/formatters';
import {
NumberFormats,
SMART_DATE_ID,
getTimeFormatter,
} from '@superset-ui/core';
import {
getPercentFormatter,
getXAxisFormatter,
getSmartDateFormatter,
} from '../../src/utils/formatters';
describe('getPercentFormatter', () => {
const value = 0.6;
@@ -35,3 +43,41 @@ describe('getPercentFormatter', () => {
).toEqual('60.00%');
});
});
describe('getXAxisFormatter', () => {
it('should return smart date formatter when SMART_DATE_ID is specified', () => {
const formatter = getXAxisFormatter(SMART_DATE_ID);
expect(formatter).toBeDefined();
expect(formatter).toBeInstanceOf(Function);
// The formatter should be the same as getSmartDateFormatter()
const smartDateFormatter = getSmartDateFormatter();
expect(formatter).toEqual(smartDateFormatter);
});
it('should return undefined when format is not specified', () => {
const formatter = getXAxisFormatter();
expect(formatter).toBeUndefined();
});
it('should return undefined when format is empty string', () => {
const formatter = getXAxisFormatter('');
expect(formatter).toBeUndefined();
});
it('should return the appropriate time formatter for a specific format', () => {
const format = '%Y-%m-%d';
const formatter = getXAxisFormatter(format);
expect(formatter).toBeDefined();
expect(formatter).toBeInstanceOf(Function);
// Should return the same formatter as getTimeFormatter
const expectedFormatter = getTimeFormatter(format);
expect(formatter).toEqual(expectedFormatter);
});
it('should return String constructor as fallback', () => {
// This test verifies that the String constructor is returned
// Note: the current logic may not reach this case, but keeping for completeness
const formatter = getXAxisFormatter(null as any);
expect(formatter).toBeUndefined();
});
});