Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,31 @@ const x_axis_time_format: SharedControlConfig<
option.label.includes(search) || option.value.includes(search),
};

const x_axis_number_format: SharedControlConfig<
'SelectControl',
SelectDefaultOption
> = {
type: 'SelectControl',
freeForm: true,
label: t('X Axis Number Format'),
renderTrigger: true,
default: DEFAULT_NUMBER_FORMAT,
choices: D3_FORMAT_OPTIONS,
description: D3_FORMAT_DOCS,
tokenSeparators: ['\n', '\t', ';'],
filterOption: ({ data: option }, search) =>
option.label.includes(search) || option.value.includes(search),
mapStateToProps: state => {
const isPercentage =
state.controls?.comparison_type?.value === ComparisonType.Percentage;
return {
choices: isPercentage
? D3_FORMAT_OPTIONS.filter(option => option[0].includes('%'))
: D3_FORMAT_OPTIONS,
};
},
};

const color_scheme: SharedControlConfig<'ColorSchemeControl'> = {
type: 'ColorSchemeControl',
label: t('Color Scheme'),
Expand Down Expand Up @@ -456,6 +481,7 @@ const sharedControls: Record<string, SharedControlConfig<any>> = {
size: dndSizeControl,
y_axis_format,
x_axis_time_format,
x_axis_number_format,
adhoc_filters: dndAdhocFilterControl,
color_scheme,
time_shift_color,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,53 @@ 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')
);
},
},
},
{
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),
);
},
},
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export const DEFAULT_FORM_DATA: EchartsTimeseriesFormData = {
stack: false,
tooltipTimeFormat: 'smart_date',
xAxisTimeFormat: 'smart_date',
xAxisNumberFormat: 'SMART_NUMBER',
truncateXAxis: true,
truncateYAxis: false,
yAxisBounds: [null, null],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ export default function transformProps(
xAxisSort,
xAxisSortAsc,
xAxisTimeFormat,
xAxisNumberFormat,
xAxisTitle,
xAxisTitleMargin,
yAxisBounds,
Expand Down Expand Up @@ -485,7 +486,9 @@ export default function transformProps(
const xAxisFormatter =
xAxisDataType === GenericDataType.Temporal
? getXAxisFormatter(xAxisTimeFormat)
: String;
: xAxisDataType === GenericDataType.Numeric
? getNumberFormatter(xAxisNumberFormat)
: String;

const {
setDataMask = () => {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export type EchartsTimeseriesFormData = QueryFormData & {
yAxisFormat?: string;
xAxisForceCategorical?: boolean;
xAxisTimeFormat?: string;
xAxisNumberFormat?: string;
timeGrainSqla?: TimeGranularity;
forceMaxInterval?: boolean;
xAxisBounds: [number | undefined | null, number | undefined | null];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/**
* 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 controlPanel from '../../../src/Timeseries/Regular/Scatter/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,
xAxisType: string | null,
): ControlPanelsContainerProps => {
const options = xAxisType
? [{ column_name: xAxisColumn, type: xAxisType }]
: [];

return {
controls: {
// @ts-ignore
x_axis: {
value: xAxisColumn,
options: options,
},
},
};
};

// tests for x_axis_time_format control
const timeFormatControl: any = getControl('x_axis_time_format');

test('scatter chart control panel should include x_axis_time_format control in the panel', () => {
expect(timeFormatControl).toBeDefined();
});

test('scatter chart control panel should have correct default value for x_axis_time_format', () => {
expect(timeFormatControl).toBeDefined();
expect(timeFormatControl.config).toBeDefined();
expect(timeFormatControl.config.default).toBe('smart_date');
});

test('scatter chart control panel should have visibility function for x_axis_time_format', () => {
expect(timeFormatControl).toBeDefined();
expect(timeFormatControl.config.visibility).toBeDefined();
expect(typeof timeFormatControl.config.visibility).toBe('function');

// The visibility function exists - the exact logic is tested implicitly through UI behavior
// The important part is that the control has proper visibility configuration
});

const isTimeVisible = (
xAxisColumn: string | null,
xAxisType: string | 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 hidden for data types that do NOT include TIME', () => {
expect(isTimeVisible('null', 'null')).toBe(false);
expect(isTimeVisible(null, null)).toBe(false);
expect(isTimeVisible('float_column', 'FLOAT')).toBe(false);
});

// tests for x_axis_number_format control
const numberFormatControl: any = getControl('x_axis_number_format');

test('scatter chart control panel should include x_axis_number_format control in the panel', () => {
expect(numberFormatControl).toBeDefined();
});

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');
});

test('scatter chart control panel should have visibility function for x_axis_number_format', () => {
expect(numberFormatControl).toBeDefined();
expect(numberFormatControl.config.visibility).toBeDefined();
expect(typeof numberFormatControl.config.visibility).toBe('function');

// The visibility function exists - the exact logic is tested implicitly through UI behavior
// The important part is that the control has proper visibility configuration
});

const isNumberVisible = (
xAxisColumn: string | null,
xAxisType: string | 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 hidden for any non-floating-point data types', () => {
expect(isNumberVisible('string_column', 'VARCHAR')).toBe(false);
expect(isNumberVisible('null', 'null')).toBe(false);
expect(isNumberVisible(null, null)).toBe(false);
expect(isNumberVisible('time_column', 'TIMESTAMP WITHOUT TIME ZONE')).toBe(
false,
);
});
Loading
Loading