mirror of
https://github.com/apache/superset.git
synced 2026-05-12 19:35:17 +00:00
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>
(cherry picked from commit e0a0a22542)
This commit is contained in:
committed by
Michael S. Molina
parent
850c33fa6b
commit
ddb285b4c3
@@ -17,7 +17,10 @@
|
||||
* under the License.
|
||||
*/
|
||||
import { t } from '@apache-superset/core/translation';
|
||||
import { getColumnLabel, QueryFormColumn } from '@superset-ui/core';
|
||||
import { GenericDataType } from '@apache-superset/core/common';
|
||||
import {
|
||||
checkColumnType,
|
||||
ControlPanelConfig,
|
||||
ControlPanelsContainerProps,
|
||||
ControlSubSectionHeader,
|
||||
@@ -181,6 +184,30 @@ const config: ControlPanelConfig = {
|
||||
...sharedControls.x_axis_time_format,
|
||||
default: 'smart_date',
|
||||
description: `${D3_TIME_FORMAT_DOCS}. ${TIME_SERIES_DESCRIPTION_TEXT}`,
|
||||
visibility: ({ controls }: ControlPanelsContainerProps) =>
|
||||
checkColumnType(
|
||||
getColumnLabel(controls?.x_axis?.value as QueryFormColumn),
|
||||
controls?.datasource?.datasource,
|
||||
[GenericDataType.Temporal],
|
||||
),
|
||||
disableStash: true,
|
||||
resetOnHide: false,
|
||||
},
|
||||
},
|
||||
],
|
||||
[
|
||||
{
|
||||
name: 'x_axis_number_format',
|
||||
config: {
|
||||
...sharedControls.x_axis_number_format,
|
||||
default: '~g',
|
||||
mapStateToProps: undefined,
|
||||
visibility: ({ controls }: ControlPanelsContainerProps) =>
|
||||
checkColumnType(
|
||||
getColumnLabel(controls?.x_axis?.value as QueryFormColumn),
|
||||
controls?.datasource?.datasource,
|
||||
[GenericDataType.Numeric],
|
||||
),
|
||||
},
|
||||
},
|
||||
],
|
||||
|
||||
@@ -17,8 +17,15 @@
|
||||
* under the License.
|
||||
*/
|
||||
import { t } from '@apache-superset/core/translation';
|
||||
import { ensureIsArray, JsonArray } from '@superset-ui/core';
|
||||
import {
|
||||
ensureIsArray,
|
||||
getColumnLabel,
|
||||
JsonArray,
|
||||
QueryFormColumn,
|
||||
} from '@superset-ui/core';
|
||||
import { GenericDataType } from '@apache-superset/core/common';
|
||||
import {
|
||||
checkColumnType,
|
||||
ControlPanelConfig,
|
||||
ControlPanelsContainerProps,
|
||||
ControlSetRow,
|
||||
@@ -154,6 +161,13 @@ function createAxisControl(axis: 'x' | 'y'): ControlSetRow[] {
|
||||
Boolean(controls?.orientation.value === OrientationType.Vertical);
|
||||
const isHorizontal = (controls: ControlStateMapping) =>
|
||||
Boolean(controls?.orientation.value === OrientationType.Horizontal);
|
||||
const isNumericXAxis = (controls: ControlStateMapping) =>
|
||||
checkColumnType(
|
||||
getColumnLabel(controls?.x_axis?.value as QueryFormColumn),
|
||||
controls?.datasource?.datasource,
|
||||
[GenericDataType.Numeric],
|
||||
);
|
||||
|
||||
return [
|
||||
[
|
||||
{
|
||||
@@ -163,7 +177,23 @@ function createAxisControl(axis: 'x' | 'y'): ControlSetRow[] {
|
||||
default: 'smart_date',
|
||||
description: `${D3_TIME_FORMAT_DOCS}. ${TIME_SERIES_DESCRIPTION_TEXT}`,
|
||||
visibility: ({ controls }: ControlPanelsContainerProps) =>
|
||||
isXAxis ? isVertical(controls) : isHorizontal(controls),
|
||||
(isXAxis ? isVertical(controls) : isHorizontal(controls)) &&
|
||||
!isNumericXAxis(controls),
|
||||
disableStash: true,
|
||||
resetOnHide: false,
|
||||
},
|
||||
},
|
||||
],
|
||||
[
|
||||
{
|
||||
name: 'x_axis_number_format',
|
||||
config: {
|
||||
...sharedControls.x_axis_number_format,
|
||||
default: '~g',
|
||||
mapStateToProps: undefined,
|
||||
visibility: ({ controls }: ControlPanelsContainerProps) =>
|
||||
(isXAxis ? isVertical(controls) : isHorizontal(controls)) &&
|
||||
isNumericXAxis(controls),
|
||||
disableStash: true,
|
||||
resetOnHide: false,
|
||||
},
|
||||
|
||||
@@ -17,7 +17,10 @@
|
||||
* under the License.
|
||||
*/
|
||||
import { t } from '@apache-superset/core/translation';
|
||||
import { getColumnLabel, QueryFormColumn } from '@superset-ui/core';
|
||||
import { GenericDataType } from '@apache-superset/core/common';
|
||||
import {
|
||||
checkColumnType,
|
||||
ControlPanelConfig,
|
||||
ControlPanelsContainerProps,
|
||||
ControlSubSectionHeader,
|
||||
@@ -146,6 +149,30 @@ const config: ControlPanelConfig = {
|
||||
...sharedControls.x_axis_time_format,
|
||||
default: 'smart_date',
|
||||
description: `${D3_TIME_FORMAT_DOCS}. ${TIME_SERIES_DESCRIPTION_TEXT}`,
|
||||
visibility: ({ controls }: ControlPanelsContainerProps) =>
|
||||
checkColumnType(
|
||||
getColumnLabel(controls?.x_axis?.value as QueryFormColumn),
|
||||
controls?.datasource?.datasource,
|
||||
[GenericDataType.Temporal],
|
||||
),
|
||||
disableStash: true,
|
||||
resetOnHide: false,
|
||||
},
|
||||
},
|
||||
],
|
||||
[
|
||||
{
|
||||
name: 'x_axis_number_format',
|
||||
config: {
|
||||
...sharedControls.x_axis_number_format,
|
||||
default: '~g',
|
||||
mapStateToProps: undefined,
|
||||
visibility: ({ controls }: ControlPanelsContainerProps) =>
|
||||
checkColumnType(
|
||||
getColumnLabel(controls?.x_axis?.value as QueryFormColumn),
|
||||
controls?.datasource?.datasource,
|
||||
[GenericDataType.Numeric],
|
||||
),
|
||||
},
|
||||
},
|
||||
],
|
||||
|
||||
@@ -17,7 +17,10 @@
|
||||
* under the License.
|
||||
*/
|
||||
import { t } from '@apache-superset/core/translation';
|
||||
import { getColumnLabel, QueryFormColumn } from '@superset-ui/core';
|
||||
import { GenericDataType } from '@apache-superset/core/common';
|
||||
import {
|
||||
checkColumnType,
|
||||
ControlPanelConfig,
|
||||
ControlPanelsContainerProps,
|
||||
ControlSubSectionHeader,
|
||||
@@ -112,53 +115,28 @@ const config: ControlPanelConfig = {
|
||||
...sharedControls.x_axis_time_format,
|
||||
default: 'smart_date',
|
||||
description: `${D3_TIME_FORMAT_DOCS}. ${TIME_SERIES_DESCRIPTION_TEXT}`,
|
||||
visibility: ({ controls }: ControlPanelsContainerProps) => {
|
||||
// check if x axis is a time column
|
||||
const xAxisColumn = controls?.x_axis?.value;
|
||||
const xAxisOptions = controls?.x_axis?.options;
|
||||
|
||||
if (!xAxisColumn || !Array.isArray(xAxisOptions)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const xAxisType = xAxisOptions.find(
|
||||
option => option.column_name === xAxisColumn,
|
||||
)?.type;
|
||||
|
||||
return (
|
||||
typeof xAxisType === 'string' &&
|
||||
xAxisType.toUpperCase().includes('TIME')
|
||||
);
|
||||
},
|
||||
visibility: ({ controls }: ControlPanelsContainerProps) =>
|
||||
checkColumnType(
|
||||
getColumnLabel(controls?.x_axis?.value as QueryFormColumn),
|
||||
controls?.datasource?.datasource,
|
||||
[GenericDataType.Temporal],
|
||||
),
|
||||
disableStash: true,
|
||||
resetOnHide: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'x_axis_number_format',
|
||||
config: {
|
||||
...sharedControls.x_axis_number_format,
|
||||
visibility: ({ controls }: ControlPanelsContainerProps) => {
|
||||
// check if x axis is a floating-point column
|
||||
const xAxisColumn = controls?.x_axis?.value;
|
||||
const xAxisOptions = controls?.x_axis?.options;
|
||||
|
||||
if (!xAxisColumn || !Array.isArray(xAxisOptions)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const xAxisType = xAxisOptions.find(
|
||||
option => option.column_name === xAxisColumn,
|
||||
)?.type;
|
||||
|
||||
if (typeof xAxisType !== 'string') {
|
||||
return false;
|
||||
}
|
||||
|
||||
const typeUpper = xAxisType.toUpperCase();
|
||||
|
||||
return ['FLOAT', 'DOUBLE', 'REAL', 'NUMERIC', 'DECIMAL'].some(
|
||||
t => typeUpper.includes(t),
|
||||
);
|
||||
},
|
||||
default: '~g',
|
||||
mapStateToProps: undefined,
|
||||
visibility: ({ controls }: ControlPanelsContainerProps) =>
|
||||
checkColumnType(
|
||||
getColumnLabel(controls?.x_axis?.value as QueryFormColumn),
|
||||
controls?.datasource?.datasource,
|
||||
[GenericDataType.Numeric],
|
||||
),
|
||||
},
|
||||
},
|
||||
],
|
||||
|
||||
@@ -17,7 +17,10 @@
|
||||
* under the License.
|
||||
*/
|
||||
import { t } from '@apache-superset/core/translation';
|
||||
import { getColumnLabel, QueryFormColumn } from '@superset-ui/core';
|
||||
import { GenericDataType } from '@apache-superset/core/common';
|
||||
import {
|
||||
checkColumnType,
|
||||
ControlPanelConfig,
|
||||
ControlPanelsContainerProps,
|
||||
ControlSubSectionHeader,
|
||||
@@ -111,6 +114,30 @@ const config: ControlPanelConfig = {
|
||||
...sharedControls.x_axis_time_format,
|
||||
default: 'smart_date',
|
||||
description: `${D3_TIME_FORMAT_DOCS}. ${TIME_SERIES_DESCRIPTION_TEXT}`,
|
||||
visibility: ({ controls }: ControlPanelsContainerProps) =>
|
||||
checkColumnType(
|
||||
getColumnLabel(controls?.x_axis?.value as QueryFormColumn),
|
||||
controls?.datasource?.datasource,
|
||||
[GenericDataType.Temporal],
|
||||
),
|
||||
disableStash: true,
|
||||
resetOnHide: false,
|
||||
},
|
||||
},
|
||||
],
|
||||
[
|
||||
{
|
||||
name: 'x_axis_number_format',
|
||||
config: {
|
||||
...sharedControls.x_axis_number_format,
|
||||
default: '~g',
|
||||
mapStateToProps: undefined,
|
||||
visibility: ({ controls }: ControlPanelsContainerProps) =>
|
||||
checkColumnType(
|
||||
getColumnLabel(controls?.x_axis?.value as QueryFormColumn),
|
||||
controls?.datasource?.datasource,
|
||||
[GenericDataType.Numeric],
|
||||
),
|
||||
},
|
||||
},
|
||||
],
|
||||
|
||||
@@ -17,7 +17,10 @@
|
||||
* under the License.
|
||||
*/
|
||||
import { t } from '@apache-superset/core/translation';
|
||||
import { getColumnLabel, QueryFormColumn } from '@superset-ui/core';
|
||||
import { GenericDataType } from '@apache-superset/core/common';
|
||||
import {
|
||||
checkColumnType,
|
||||
ControlPanelConfig,
|
||||
ControlPanelsContainerProps,
|
||||
ControlSubSectionHeader,
|
||||
@@ -163,6 +166,30 @@ const config: ControlPanelConfig = {
|
||||
...sharedControls.x_axis_time_format,
|
||||
default: 'smart_date',
|
||||
description: `${D3_TIME_FORMAT_DOCS}. ${TIME_SERIES_DESCRIPTION_TEXT}`,
|
||||
visibility: ({ controls }: ControlPanelsContainerProps) =>
|
||||
checkColumnType(
|
||||
getColumnLabel(controls?.x_axis?.value as QueryFormColumn),
|
||||
controls?.datasource?.datasource,
|
||||
[GenericDataType.Temporal],
|
||||
),
|
||||
disableStash: true,
|
||||
resetOnHide: false,
|
||||
},
|
||||
},
|
||||
],
|
||||
[
|
||||
{
|
||||
name: 'x_axis_number_format',
|
||||
config: {
|
||||
...sharedControls.x_axis_number_format,
|
||||
default: '~g',
|
||||
mapStateToProps: undefined,
|
||||
visibility: ({ controls }: ControlPanelsContainerProps) =>
|
||||
checkColumnType(
|
||||
getColumnLabel(controls?.x_axis?.value as QueryFormColumn),
|
||||
controls?.datasource?.datasource,
|
||||
[GenericDataType.Numeric],
|
||||
),
|
||||
},
|
||||
},
|
||||
],
|
||||
|
||||
@@ -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,
|
||||
);
|
||||
});
|
||||
@@ -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,
|
||||
);
|
||||
});
|
||||
|
||||
@@ -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,
|
||||
);
|
||||
});
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
@@ -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,
|
||||
);
|
||||
});
|
||||
@@ -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,
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user