Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Chart date axis bug #1108

Merged
merged 2 commits into from
Dec 22, 2022
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "querybook",
"version": "3.15.2",
"version": "3.15.3",
"description": "A Big Data Webapp",
"private": true,
"scripts": {
Expand Down
25 changes: 22 additions & 3 deletions querybook/webapp/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 querybook/webapp/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 querybook/webapp/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 querybook/webapp/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