Skip to content

Commit

Permalink
fix: Chart date axis bug (pinterest#1108)
Browse files Browse the repository at this point in the history
  • Loading branch information
czgu authored Dec 22, 2022
1 parent 6e4b9bc commit 8dbce67
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
25 changes: 22 additions & 3 deletions const/dataDocChart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,33 @@ export interface IChartSeriesMeta {
agg_type?: ChartDataAggType;
}

export type ChartScaleType = 'time' | 'category' | 'linear' | 'logarithmic';
export type ChartScaleType =
| 'time'
| 'date'
| 'category'
| 'linear'
| 'logarithmic';
export const ChartScaleOptions: ChartScaleType[] = [
'time',
'date',
'category',
'linear',
'logarithmic',
];

// The hard code for values is a bug on the chartJS side
// We should use the type ScaleType instead once this is fixed
export const chartScaleToChartJSScale: Record<
ChartScaleType,
'time' | 'category' | 'linear' | 'logarithmic'
> = {
category: 'category',
linear: 'linear',
logarithmic: 'logarithmic',
time: 'time',
date: 'time', // This is the only one that is different
};

export enum ChartScaleFormat {
NONE = '',
DOLLAR = '$',
Expand All @@ -88,11 +107,11 @@ export const chartTypeToAllowedAxisType: Partial<
y: ['linear', 'logarithmic'],
},
bar: {
x: ['time', 'category'],
x: ['time', 'date', 'category'],
y: ['linear', 'logarithmic'],
},
histogram: {
x: ['time', 'category'],
x: ['time', 'date', 'category'],
y: ['linear', 'logarithmic'],
},
pie: {
Expand Down
2 changes: 1 addition & 1 deletion lib/chart/chart-data-processing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function processDataPoint(val: any, scale: ChartScaleType) {
// Convert data by Axis type
if (scale === 'category') {
return val;
} else if (scale === 'time' && isNaN(val)) {
} else if ((scale === 'time' || scale === 'date') && isNaN(val)) {
return val;
}

Expand Down
11 changes: 10 additions & 1 deletion lib/chart/chart-meta-processing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
ChartValueSourceType,
IChartAxisMeta,
IChartFormValues,
chartScaleToChartJSScale,
} from 'const/dataDocChart';
import { StatementExecutionDefaultResultSize } from 'const/queryResultLimit';
import type { DeepPartial } from 'lib/typescript';
Expand Down Expand Up @@ -344,7 +345,7 @@ function computeScaleOptions(
};

if (scaleType != null) {
axis.type = scaleType;
axis.type = chartScaleToChartJSScale[scaleType];
}

if (scaleType === 'time') {
Expand All @@ -356,6 +357,14 @@ function computeScaleOptions(
minute: 'h:mm a',
},
};
} else if (scaleType === 'date') {
(axis as DeepPartial<TimeScaleOptions>).time = {
tooltipFormat: 'YYYY-MM-DD',
displayFormats: {
day: 'YYYY-MM-DD',
},
minUnit: 'day',
};
} else if (scaleType === 'linear' || scaleType === 'logarithmic') {
// for empty case, it might be null or ""
if (axisMeta.max != null && typeof axisMeta.max === 'number') {
Expand Down
3 changes: 2 additions & 1 deletion lib/chart/chart-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ export function getValueDataType(value: any): AxesValueType {

export function getDefaultScaleType(value: any): ChartScaleType {
const valueType = getValueDataType(value);

switch (valueType) {
case 'datetime':
return 'time';
case 'number':
return 'linear';
case 'date':
return 'time';
return 'date';
case 'string':
default:
return 'category';
Expand Down

0 comments on commit 8dbce67

Please sign in to comment.