Skip to content
This repository was archived by the owner on Dec 10, 2021. It is now read-only.

fix(plugin-chart-echarts): default to standard x-axis format #1043

Merged
merged 1 commit into from
Apr 9, 2021
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 @@ -46,7 +46,7 @@ export const D3_TIME_FORMAT_OPTIONS: [string, string][] = [
['%m/%d/%Y', '%m/%d/%Y | 01/14/2019'],
['%Y-%m-%d', '%Y-%m-%d | 2019-01-14'],
['%Y-%m-%d %H:%M:%S', '%Y-%m-%d %H:%M:%S | 2019-01-14 01:32:10'],
['%d-%m-%Y %H:%M:%S', '%Y-%m-%d %H:%M:%S | 14-01-2019 01:32:10'],
['%d-%m-%Y %H:%M:%S', '%d-%m-%Y %H:%M:%S | 14-01-2019 01:32:10'],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bycatch - typo in the description

['%H:%M:%S', '%H:%M:%S | 01:32:10'],
];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import createMultiFormatter from '../factories/createMultiFormatter';

const smartDateDetailedFormatter = createMultiFormatter({
id: 'smart_date_detailed',
label: 'Detailed adaptive formatter',
formats: {
millisecond: '%Y-%m-%d %H:%M:%S.%L',
second: '%Y-%m-%d %H:%M:%S',
minute: '%Y-%m-%d %H:%M',
hour: '%Y-%m-%d %H:%M',
day: '%Y-%m-%d',
week: '%Y-%m-%d',
month: '%Y-%m-%d',
year: '%Y',
},
});

export default smartDateDetailedFormatter;
1 change: 1 addition & 0 deletions packages/superset-ui-core/src/time-format/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export { default as createD3TimeFormatter } from './factories/createD3TimeFormat
export { default as createMultiFormatter } from './factories/createMultiFormatter';

export { default as smartDateFormatter } from './formatters/smartDate';
export { default as smartDateDetailedFormatter } from './formatters/smartDateDetailed';
export { default as smartDateVerboseFormatter } from './formatters/smartDateVerbose';

export * from './types';
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import TimeFormatter from '@superset-ui/core/src/time-format/TimeFormatter';
import smartDateDetailedFormatter from '@superset-ui/core/src/time-format/formatters/smartDateDetailed';

describe('smartDateDetailedFormatter', () => {
const formatter = smartDateDetailedFormatter;

it('is a function', () => {
expect(formatter).toBeInstanceOf(TimeFormatter);
});

it('shows only year when 1st day of the year', () => {
expect(formatter(new Date('2020-01-01 0:00:00'))).toBe('2020');
});

it('shows full date when a regular date', () => {
expect(formatter(new Date('2020-03-01 00:00:00'))).toBe('2020-03-01');
});

it('shows full date including time of day without seconds when hour precision', () => {
expect(formatter(new Date('2020-03-01 13:00:00'))).toBe('2020-03-01 13:00');
});

it('shows full date including time of day when minute precision', () => {
expect(formatter(new Date('2020-03-10 13:10:00'))).toBe('2020-03-10 13:10');
});

it('shows full date including time of day when subsecond precision', () => {
expect(formatter(new Date('2020-03-10 13:10:00.1'))).toBe('2020-03-10 13:10:00.100');
});
});
39 changes: 34 additions & 5 deletions plugins/plugin-chart-echarts/src/Timeseries/controlPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
*/
import React from 'react';
import { legacyValidateInteger, legacyValidateNumber, t } from '@superset-ui/core';
import { ControlPanelConfig, sections } from '@superset-ui/chart-controls';
import {
ControlPanelConfig,
D3_TIME_FORMAT_DOCS,
sections,
sharedControls,
} from '@superset-ui/chart-controls';

import {
DEFAULT_FORM_DATA,
Expand Down Expand Up @@ -51,12 +56,14 @@ const {
rowLimit,
seriesType,
stack,
tooltipTimeFormat,
truncateYAxis,
yAxisBounds,
zoomable,
xAxisLabelRotation,
xAxisShowMinLabel,
xAxisShowMaxLabel,
} = DEFAULT_FORM_DATA;

const config: ControlPanelConfig = {
controlPanelSections: [
sections.legacyTimeseriesTime,
Expand Down Expand Up @@ -314,14 +321,25 @@ const config: ControlPanelConfig = {
[legendTypeControl, legendOrientationControl],
[legendMarginControl, noopControl],
[<h1 className="section-header">{t('X Axis')}</h1>],
['x_axis_time_format'],
[
{
name: 'x_axis_time_format',
config: {
...sharedControls.x_axis_time_format,
default: 'smart_date',
description: `${D3_TIME_FORMAT_DOCS}. ${t(
'When using other than adaptive formatting, labels may overlap.',
)}`,
},
},
],
[
{
name: 'xAxisShowMinLabel',
config: {
type: 'CheckboxControl',
label: t('Show Min Label'),
default: true,
default: xAxisShowMinLabel,
renderTrigger: true,
description: t('Show Min Label'),
},
Expand All @@ -333,7 +351,7 @@ const config: ControlPanelConfig = {
config: {
type: 'CheckboxControl',
label: t('Show Max Label'),
default: true,
default: xAxisShowMaxLabel,
renderTrigger: true,
description: t('Show Max Label'),
},
Expand Down Expand Up @@ -371,6 +389,17 @@ const config: ControlPanelConfig = {
},
},
],
[
{
name: 'tooltipTimeFormat',
config: {
...sharedControls.x_axis_time_format,
label: t('Tooltip time format'),
default: tooltipTimeFormat,
clearable: false,
},
},
],
// eslint-disable-next-line react/jsx-key
[<h1 className="section-header">{t('Y Axis')}</h1>],
['y_axis_format'],
Expand Down
21 changes: 15 additions & 6 deletions plugins/plugin-chart-echarts/src/Timeseries/transformProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import {
isIntervalAnnotationLayer,
isTimeseriesAnnotationLayer,
getTimeFormatter,
getTimeFormatterForGranularity,
smartDateFormatter,
smartDateDetailedFormatter,
TimeseriesChartDataResponseResult,
TimeFormatter,
} from '@superset-ui/core';
Expand Down Expand Up @@ -80,7 +80,7 @@ export default function transformProps(chartProps: ChartProps): EchartsProps {
xAxisShowMaxLabel,
xAxisTimeFormat,
yAxisBounds,
timeGrainSqla,
tooltipTimeFormat,
zoomable,
richTooltip,
xAxisLabelRotation,
Expand Down Expand Up @@ -133,9 +133,18 @@ export default function transformProps(chartProps: ChartProps): EchartsProps {
if (max === undefined) max = 1;
}

let xAxisFormatter: TimeFormatter | StringConstructor;
if (xAxisTimeFormat === smartDateFormatter.id) {
xAxisFormatter = getTimeFormatterForGranularity(timeGrainSqla);
let tooltipFormatter: TimeFormatter | StringConstructor;
if (tooltipTimeFormat === smartDateFormatter.id) {
tooltipFormatter = smartDateDetailedFormatter;
} else if (tooltipTimeFormat) {
tooltipFormatter = getTimeFormatter(xAxisTimeFormat);
} else {
tooltipFormatter = String;
}

let xAxisFormatter: TimeFormatter | StringConstructor | undefined;
if (xAxisTimeFormat === smartDateFormatter.id || !xAxisTimeFormat) {
xAxisFormatter = undefined;
} else if (xAxisTimeFormat) {
xAxisFormatter = getTimeFormatter(xAxisTimeFormat);
} else {
Expand Down Expand Up @@ -184,7 +193,7 @@ export default function transformProps(chartProps: ChartProps): EchartsProps {
const value: number = !richTooltip ? params.value : params[0].value[0];
const prophetValue = !richTooltip ? [params] : params;

const rows: Array<string> = [`${xAxisFormatter(value)}`];
const rows: Array<string> = [`${tooltipFormatter(value)}`];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested and found that the String constructor did not work.

image

const prophetValues: Record<string, ProphetValue> = extractProphetValuesFromTooltipParams(
prophetValue,
);
Expand Down
8 changes: 5 additions & 3 deletions plugins/plugin-chart-echarts/src/Timeseries/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export type EchartsTimeseriesFormData = {
rowLimit: number;
seriesType: EchartsTimeseriesSeriesType;
stack: boolean;
tooltipTimeFormat?: string;
truncateYAxis: boolean;
yAxisFormat?: string;
xAxisShowMinLabel?: boolean;
Expand Down Expand Up @@ -85,11 +86,12 @@ export const DEFAULT_FORM_DATA: EchartsTimeseriesFormData = {
rowLimit: 10000,
seriesType: EchartsTimeseriesSeriesType.Line,
stack: false,
tooltipTimeFormat: 'smart_date',
truncateYAxis: true,
yAxisBounds: [null, null],
xAxisShowMinLabel: true,
xAxisShowMaxLabel: true,
xAxisShowMinLabel: false,
xAxisShowMaxLabel: false,
zoomable: false,
richTooltip: true,
xAxisLabelRotation: 45,
xAxisLabelRotation: 0,
};