fix(charts): add X Axis Number Format control for numeric X-axis columns (#38809)

Co-authored-by: codeant-ai-for-open-source[bot] <244253245+codeant-ai-for-open-source[bot]@users.noreply.github.com>
This commit is contained in:
Enzo Martellucci
2026-03-31 12:38:07 +02:00
committed by GitHub
parent 2c9cf0bd55
commit e0a0a22542
16 changed files with 865 additions and 106 deletions

View File

@@ -0,0 +1,115 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { ControlPanelsContainerProps } from '@superset-ui/chart-controls/types';
import { GenericDataType } from '@apache-superset/core/common';
import controlPanel from '../../../src/Timeseries/Area/controlPanel';
const config = controlPanel;
const getControl = (controlName: string) => {
for (const section of config.controlPanelSections) {
if (section && section.controlSetRows) {
for (const row of section.controlSetRows) {
for (const control of row) {
if (
typeof control === 'object' &&
control !== null &&
'name' in control &&
control.name === controlName
) {
return control;
}
}
}
}
}
return null;
};
const mockControls = (
xAxisColumn: string | null,
typeGeneric: GenericDataType | null,
): ControlPanelsContainerProps => {
const columns =
xAxisColumn && typeGeneric !== null
? [{ column_name: xAxisColumn, type_generic: typeGeneric }]
: [];
return {
controls: {
// @ts-expect-error
x_axis: {
value: xAxisColumn,
},
// @ts-expect-error
datasource: {
datasource: { columns },
},
},
};
};
const timeFormatControl: any = getControl('x_axis_time_format');
const numberFormatControl: any = getControl('x_axis_number_format');
test('should include x_axis_time_format control', () => {
expect(timeFormatControl).toBeDefined();
expect(timeFormatControl.config.default).toBe('smart_date');
});
test('should include x_axis_number_format control', () => {
expect(numberFormatControl).toBeDefined();
expect(numberFormatControl.config.default).toBe('~g');
});
test('x_axis_time_format should be visible for temporal columns', () => {
const visibilityFn = timeFormatControl?.config?.visibility;
expect(visibilityFn(mockControls('date', GenericDataType.Temporal))).toBe(
true,
);
});
test('x_axis_time_format should be hidden for numeric columns', () => {
const visibilityFn = timeFormatControl?.config?.visibility;
expect(visibilityFn(mockControls('year', GenericDataType.Numeric))).toBe(
false,
);
});
test('x_axis_number_format should be visible for numeric columns', () => {
const visibilityFn = numberFormatControl?.config?.visibility;
expect(visibilityFn(mockControls('year', GenericDataType.Numeric))).toBe(
true,
);
});
test('x_axis_number_format should be hidden for temporal columns', () => {
const visibilityFn = numberFormatControl?.config?.visibility;
expect(visibilityFn(mockControls('date', GenericDataType.Temporal))).toBe(
false,
);
});
test('x_axis_number_format should be hidden for string columns', () => {
const visibilityFn = numberFormatControl?.config?.visibility;
expect(visibilityFn(mockControls('name', GenericDataType.String))).toBe(
false,
);
});

View File

@@ -16,11 +16,14 @@
* specific language governing permissions and limitations
* under the License.
*/
import { ControlPanelsContainerProps } from '@superset-ui/chart-controls/types';
import { GenericDataType } from '@apache-superset/core/common';
import controlPanel from '../../../src/Timeseries/Regular/Bar/controlPanel';
import {
StackControlOptionsWithoutStream,
StackControlsValue,
} from '../../../src/constants';
import { OrientationType } from '../../../src/Timeseries/types';
const config = controlPanel;
@@ -218,3 +221,74 @@ test('should preserve stack value when formData does not have stack property', (
expect(result).not.toHaveProperty('stack');
});
// x_axis_number_format visibility tests
const mockBarControls = (
xAxisColumn: string | null,
typeGeneric: GenericDataType | null,
orientation: string = OrientationType.Vertical,
): ControlPanelsContainerProps => {
const columns =
xAxisColumn && typeGeneric !== null
? [{ column_name: xAxisColumn, type_generic: typeGeneric }]
: [];
return {
controls: {
// @ts-expect-error
x_axis: {
value: xAxisColumn,
},
// @ts-expect-error
orientation: {
value: orientation,
},
// @ts-expect-error
datasource: {
datasource: { columns },
},
},
};
};
const numberFormatControl: any = getControl('x_axis_number_format');
const timeFormatControl: any = getControl('x_axis_time_format');
test('should include x_axis_number_format control in the panel', () => {
expect(numberFormatControl).toBeDefined();
});
test('x_axis_number_format should be visible for numeric columns in vertical orientation', () => {
const visibilityFn = numberFormatControl?.config?.visibility;
expect(visibilityFn(mockBarControls('year', GenericDataType.Numeric))).toBe(
true,
);
expect(visibilityFn(mockBarControls('price', GenericDataType.Numeric))).toBe(
true,
);
});
test('x_axis_number_format should be hidden for time columns', () => {
const visibilityFn = numberFormatControl?.config?.visibility;
expect(visibilityFn(mockBarControls('date', GenericDataType.Temporal))).toBe(
false,
);
});
test('x_axis_number_format should be hidden for non-numeric columns', () => {
const visibilityFn = numberFormatControl?.config?.visibility;
expect(visibilityFn(mockBarControls('name', GenericDataType.String))).toBe(
false,
);
expect(visibilityFn(mockBarControls('flag', GenericDataType.Boolean))).toBe(
false,
);
});
test('x_axis_time_format should be hidden for numeric columns', () => {
const visibilityFn = timeFormatControl?.config?.visibility;
expect(visibilityFn(mockBarControls('year', GenericDataType.Numeric))).toBe(
false,
);
});

View File

@@ -0,0 +1,115 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { ControlPanelsContainerProps } from '@superset-ui/chart-controls/types';
import { GenericDataType } from '@apache-superset/core/common';
import controlPanel from '../../../src/Timeseries/Regular/Line/controlPanel';
const config = controlPanel;
const getControl = (controlName: string) => {
for (const section of config.controlPanelSections) {
if (section && section.controlSetRows) {
for (const row of section.controlSetRows) {
for (const control of row) {
if (
typeof control === 'object' &&
control !== null &&
'name' in control &&
control.name === controlName
) {
return control;
}
}
}
}
}
return null;
};
const mockControls = (
xAxisColumn: string | null,
typeGeneric: GenericDataType | null,
): ControlPanelsContainerProps => {
const columns =
xAxisColumn && typeGeneric !== null
? [{ column_name: xAxisColumn, type_generic: typeGeneric }]
: [];
return {
controls: {
// @ts-expect-error
x_axis: {
value: xAxisColumn,
},
// @ts-expect-error
datasource: {
datasource: { columns },
},
},
};
};
const timeFormatControl: any = getControl('x_axis_time_format');
const numberFormatControl: any = getControl('x_axis_number_format');
test('should include x_axis_time_format control', () => {
expect(timeFormatControl).toBeDefined();
expect(timeFormatControl.config.default).toBe('smart_date');
});
test('should include x_axis_number_format control', () => {
expect(numberFormatControl).toBeDefined();
expect(numberFormatControl.config.default).toBe('~g');
});
test('x_axis_time_format should be visible for temporal columns', () => {
const visibilityFn = timeFormatControl?.config?.visibility;
expect(visibilityFn(mockControls('date', GenericDataType.Temporal))).toBe(
true,
);
});
test('x_axis_time_format should be hidden for numeric columns', () => {
const visibilityFn = timeFormatControl?.config?.visibility;
expect(visibilityFn(mockControls('year', GenericDataType.Numeric))).toBe(
false,
);
});
test('x_axis_number_format should be visible for numeric columns', () => {
const visibilityFn = numberFormatControl?.config?.visibility;
expect(visibilityFn(mockControls('year', GenericDataType.Numeric))).toBe(
true,
);
});
test('x_axis_number_format should be hidden for temporal columns', () => {
const visibilityFn = numberFormatControl?.config?.visibility;
expect(visibilityFn(mockControls('date', GenericDataType.Temporal))).toBe(
false,
);
});
test('x_axis_number_format should be hidden for string columns', () => {
const visibilityFn = numberFormatControl?.config?.visibility;
expect(visibilityFn(mockControls('name', GenericDataType.String))).toBe(
false,
);
});

View File

@@ -17,6 +17,7 @@
* under the License.
*/
import { ControlPanelsContainerProps } from '@superset-ui/chart-controls/types';
import { GenericDataType } from '@apache-superset/core/common';
import controlPanel from '../../../src/Timeseries/Regular/Scatter/controlPanel';
const config = controlPanel;
@@ -44,18 +45,22 @@ const getControl = (controlName: string) => {
const mockControls = (
xAxisColumn: string | null,
xAxisType: string | null,
typeGeneric: GenericDataType | null,
): ControlPanelsContainerProps => {
const options = xAxisType
? [{ column_name: xAxisColumn, type: xAxisType }]
: [];
const columns =
xAxisColumn && typeGeneric !== null
? [{ column_name: xAxisColumn, type_generic: typeGeneric }]
: [];
return {
controls: {
// @ts-expect-error
x_axis: {
value: xAxisColumn,
options: options,
},
// @ts-expect-error
datasource: {
datasource: { columns },
},
},
};
@@ -85,26 +90,21 @@ test('scatter chart control panel should have visibility function for x_axis_tim
const isTimeVisible = (
xAxisColumn: string | null,
xAxisType: string | null,
xAxisType: GenericDataType | null,
): boolean => {
const props = mockControls(xAxisColumn, xAxisType);
const visibilityFn = timeFormatControl?.config?.visibility;
return visibilityFn ? visibilityFn(props) : false;
};
test('x_axis_time_format control should be visible for any data types include TIME', () => {
expect(isTimeVisible('time_column', 'TIME')).toBe(true);
expect(isTimeVisible('time_column', 'TIME WITH TIME ZONE')).toBe(true);
expect(isTimeVisible('time_column', 'TIMESTAMP WITH TIME ZONE')).toBe(true);
expect(isTimeVisible('time_column', 'TIMESTAMP WITHOUT TIME ZONE')).toBe(
true,
);
test('x_axis_time_format control should be visible for temporal data types', () => {
expect(isTimeVisible('time_column', GenericDataType.Temporal)).toBe(true);
});
test('x_axis_time_format control should be hidden for data types that do NOT include TIME', () => {
expect(isTimeVisible('null', 'null')).toBe(false);
test('x_axis_time_format control should be hidden for non-temporal data types', () => {
expect(isTimeVisible(null, null)).toBe(false);
expect(isTimeVisible('float_column', 'FLOAT')).toBe(false);
expect(isTimeVisible('float_column', GenericDataType.Numeric)).toBe(false);
expect(isTimeVisible('name_column', GenericDataType.String)).toBe(false);
});
// tests for x_axis_number_format control
@@ -117,7 +117,7 @@ test('scatter chart control panel should include x_axis_number_format control in
test('scatter chart control panel should have correct default value for x_axis_number_format', () => {
expect(numberFormatControl).toBeDefined();
expect(numberFormatControl.config).toBeDefined();
expect(numberFormatControl.config.default).toBe('SMART_NUMBER');
expect(numberFormatControl.config.default).toBe('~g');
});
test('scatter chart control panel should have visibility function for x_axis_number_format', () => {
@@ -131,26 +131,20 @@ test('scatter chart control panel should have visibility function for x_axis_num
const isNumberVisible = (
xAxisColumn: string | null,
xAxisType: string | null,
xAxisType: GenericDataType | null,
): boolean => {
const props = mockControls(xAxisColumn, xAxisType);
const visibilityFn = numberFormatControl?.config?.visibility;
return visibilityFn ? visibilityFn(props) : false;
};
test('x_axis_number_format control should be visible for any floating-point data types', () => {
expect(isNumberVisible('float_column', 'FLOAT')).toBe(true);
expect(isNumberVisible('double_column', 'DOUBLE')).toBe(true);
expect(isNumberVisible('real_column', 'REAL')).toBe(true);
expect(isNumberVisible('numeric_column', 'NUMERIC')).toBe(true);
expect(isNumberVisible('decimal_column', 'DECIMAL')).toBe(true);
test('x_axis_number_format control should be visible for numeric data types', () => {
expect(isNumberVisible('float_column', GenericDataType.Numeric)).toBe(true);
expect(isNumberVisible('int_column', GenericDataType.Numeric)).toBe(true);
});
test('x_axis_number_format control should be hidden for any non-floating-point data types', () => {
expect(isNumberVisible('string_column', 'VARCHAR')).toBe(false);
expect(isNumberVisible('null', 'null')).toBe(false);
test('x_axis_number_format control should be hidden for non-numeric data types', () => {
expect(isNumberVisible('string_column', GenericDataType.String)).toBe(false);
expect(isNumberVisible(null, null)).toBe(false);
expect(isNumberVisible('time_column', 'TIMESTAMP WITHOUT TIME ZONE')).toBe(
false,
);
expect(isNumberVisible('time_column', GenericDataType.Temporal)).toBe(false);
});

View File

@@ -0,0 +1,101 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { ControlPanelsContainerProps } from '@superset-ui/chart-controls/types';
import { GenericDataType } from '@apache-superset/core/common';
import controlPanel from '../../../src/Timeseries/Regular/SmoothLine/controlPanel';
const config = controlPanel;
const getControl = (controlName: string) => {
for (const section of config.controlPanelSections) {
if (section && section.controlSetRows) {
for (const row of section.controlSetRows) {
for (const control of row) {
if (
typeof control === 'object' &&
control !== null &&
'name' in control &&
control.name === controlName
) {
return control;
}
}
}
}
}
return null;
};
const mockControls = (
xAxisColumn: string | null,
typeGeneric: GenericDataType | null,
): ControlPanelsContainerProps => {
const columns =
xAxisColumn && typeGeneric !== null
? [{ column_name: xAxisColumn, type_generic: typeGeneric }]
: [];
return {
controls: {
// @ts-expect-error
x_axis: {
value: xAxisColumn,
},
// @ts-expect-error
datasource: {
datasource: { columns },
},
},
};
};
const timeFormatControl: any = getControl('x_axis_time_format');
const numberFormatControl: any = getControl('x_axis_number_format');
test('should include x_axis_time_format control', () => {
expect(timeFormatControl).toBeDefined();
expect(timeFormatControl.config.default).toBe('smart_date');
});
test('should include x_axis_number_format control', () => {
expect(numberFormatControl).toBeDefined();
expect(numberFormatControl.config.default).toBe('~g');
});
test('x_axis_number_format should be visible for numeric columns', () => {
const visibilityFn = numberFormatControl?.config?.visibility;
expect(visibilityFn(mockControls('year', GenericDataType.Numeric))).toBe(
true,
);
});
test('x_axis_number_format should be hidden for temporal columns', () => {
const visibilityFn = numberFormatControl?.config?.visibility;
expect(visibilityFn(mockControls('date', GenericDataType.Temporal))).toBe(
false,
);
});
test('x_axis_number_format should be hidden for string columns', () => {
const visibilityFn = numberFormatControl?.config?.visibility;
expect(visibilityFn(mockControls('name', GenericDataType.String))).toBe(
false,
);
});

View File

@@ -0,0 +1,115 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { ControlPanelsContainerProps } from '@superset-ui/chart-controls/types';
import { GenericDataType } from '@apache-superset/core/common';
import controlPanel from '../../../src/Timeseries/Step/controlPanel';
const config = controlPanel;
const getControl = (controlName: string) => {
for (const section of config.controlPanelSections) {
if (section && section.controlSetRows) {
for (const row of section.controlSetRows) {
for (const control of row) {
if (
typeof control === 'object' &&
control !== null &&
'name' in control &&
control.name === controlName
) {
return control;
}
}
}
}
}
return null;
};
const mockControls = (
xAxisColumn: string | null,
typeGeneric: GenericDataType | null,
): ControlPanelsContainerProps => {
const columns =
xAxisColumn && typeGeneric !== null
? [{ column_name: xAxisColumn, type_generic: typeGeneric }]
: [];
return {
controls: {
// @ts-expect-error
x_axis: {
value: xAxisColumn,
},
// @ts-expect-error
datasource: {
datasource: { columns },
},
},
};
};
const timeFormatControl: any = getControl('x_axis_time_format');
const numberFormatControl: any = getControl('x_axis_number_format');
test('should include x_axis_time_format control', () => {
expect(timeFormatControl).toBeDefined();
expect(timeFormatControl.config.default).toBe('smart_date');
});
test('should include x_axis_number_format control', () => {
expect(numberFormatControl).toBeDefined();
expect(numberFormatControl.config.default).toBe('~g');
});
test('x_axis_time_format should be visible for temporal columns', () => {
const visibilityFn = timeFormatControl?.config?.visibility;
expect(visibilityFn(mockControls('date', GenericDataType.Temporal))).toBe(
true,
);
});
test('x_axis_time_format should be hidden for numeric columns', () => {
const visibilityFn = timeFormatControl?.config?.visibility;
expect(visibilityFn(mockControls('year', GenericDataType.Numeric))).toBe(
false,
);
});
test('x_axis_number_format should be visible for numeric columns', () => {
const visibilityFn = numberFormatControl?.config?.visibility;
expect(visibilityFn(mockControls('year', GenericDataType.Numeric))).toBe(
true,
);
});
test('x_axis_number_format should be hidden for temporal columns', () => {
const visibilityFn = numberFormatControl?.config?.visibility;
expect(visibilityFn(mockControls('date', GenericDataType.Temporal))).toBe(
false,
);
});
test('x_axis_number_format should be hidden for string columns', () => {
const visibilityFn = numberFormatControl?.config?.visibility;
expect(visibilityFn(mockControls('name', GenericDataType.String))).toBe(
false,
);
});