Skip to content

Commit

Permalink
feat: having a bar and error bars with time scale (#65)
Browse files Browse the repository at this point in the history
* 🚧 Having a bar and error bars with time scale

* ♻️ use a function to indicate that a scale is numeric

* 🔥 Remove Timescale as default scale, so TimeScale must be registred before use it in barchart
  • Loading branch information
trambi authored Jul 14, 2023
1 parent 1838ae8 commit 0a9668d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 10 deletions.
50 changes: 48 additions & 2 deletions src/controllers/BarWithErrorBarsController.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { registry, LinearScale, CategoryScale } from 'chart.js';
import { registry, LinearScale, CategoryScale, TimeScale } from 'chart.js';
import createChart from '../__tests__/createChart';
import { BarWithErrorBarsController } from './BarWithErrorBarsController';
import { BarWithErrorBar } from '../elements';
import 'chartjs-adapter-date-fns';

describe('bar', () => {
beforeAll(() => {
registry.addControllers(BarWithErrorBarsController);
registry.addElements(BarWithErrorBar);
registry.addScales(LinearScale, CategoryScale);
registry.addScales(LinearScale, CategoryScale, TimeScale);
});
test('default', () => {
return createChart({
Expand Down Expand Up @@ -43,4 +44,49 @@ describe('bar', () => {
},
}).toMatchImageSnapshot();
});
test('time scale', () => {
return createChart({
type: BarWithErrorBarsController.id,
data: {
labels: ['A', 'B'],
datasets: [
{
data: [
{
x: '2017-12-25',
y: 4,
yMin: 1,
yMax: 6,
},
{
x: '2018-12-25',
y: 2,
yMin: 1,
yMax: 4,
},
{
x: '2019-12-25',
y: 3,
yMin: 2,
yMax: 5,
},
],
},
],
},
options: {
scales: {
x: {
type: 'time',
display: false,
min: '2017-01-01',
max: '2020-01-01',
},
y: {
display: false,
},
},
},
}).toMatchImageSnapshot();
});
});
10 changes: 8 additions & 2 deletions src/controllers/BarWithErrorBarsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
CartesianScaleTypeRegistry,
} from 'chart.js';
import { merge } from 'chart.js/helpers';
import { calculateScale } from './utils';
import { calculateScale, isNumericScale } from './utils';
import type { IErrorBarOptions } from '../elements/render';
import { BarWithErrorBar } from '../elements';
import { generateBarTooltip } from './tooltip';
Expand Down Expand Up @@ -67,7 +67,13 @@ export class BarWithErrorBarsController extends BarController {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
parseErrorNumberData(parsed, meta.vScale!, data, start, count);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
parseErrorLabelData(parsed, meta.iScale!, start, count);
const iScale = meta.iScale as Scale;
const hasNumberIScale = isNumericScale(iScale);
if (hasNumberIScale) {
parseErrorNumberData(parsed, meta.iScale!, data, start, count);
} else {
parseErrorLabelData(parsed, meta.iScale!, start, count);
}
}

/**
Expand Down
8 changes: 3 additions & 5 deletions src/controllers/LineWithErrorBarsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
CartesianScaleTypeRegistry,
} from 'chart.js';
import { merge } from 'chart.js/helpers';
import { calculateScale } from './utils';
import { calculateScale, isNumericScale } from './utils';
import type { IErrorBarOptions } from '../elements/render';
import { PointWithErrorBar } from '../elements';
import { generateBarTooltip } from './tooltip';
Expand All @@ -31,8 +31,6 @@ import {
} from './base';
import patchController from './patchController';

const NUMERIC_SCALE_TYPES = ['linear', 'logarithmic', 'time', 'timeseries'];

export class LineWithErrorBarsController extends LineController {
/**
* @hidden
Expand Down Expand Up @@ -73,7 +71,7 @@ export class LineWithErrorBarsController extends LineController {
parseErrorNumberData(parsed, meta.vScale!, data, start, count);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const iScale = meta.iScale as Scale;
const hasNumberIScale = NUMERIC_SCALE_TYPES.includes(iScale.type);
const hasNumberIScale = isNumericScale(iScale);
if (hasNumberIScale) {
parseErrorNumberData(parsed, meta.iScale!, data, start, count);
} else {
Expand Down Expand Up @@ -111,7 +109,7 @@ export class LineWithErrorBarsController extends LineController {
);

const iScale = this._cachedMeta.iScale as Scale;
const hasNumberIScale = NUMERIC_SCALE_TYPES.includes(iScale.type);
const hasNumberIScale = isNumericScale(iScale);
if (hasNumberIScale) {
calculateScale(
properties,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion src/controllers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { LinearScale, RadialLinearScale } from 'chart.js';
import type { LinearScale, RadialLinearScale, Scale } from 'chart.js';
import type { IErrorBarRDataPoint, IErrorBarXYDataPoint } from './base';

export const allModelKeys = ['xMin', 'xMax', 'yMin', 'yMax'];
Expand Down Expand Up @@ -63,3 +63,9 @@ export function calculatePolarScale(
}
}
}

const NUMERIC_SCALE_TYPES = ['linear', 'logarithmic', 'time', 'timeseries'];

export function isNumericScale(scale: Scale): boolean {
return NUMERIC_SCALE_TYPES.includes(scale.type);
}

0 comments on commit 0a9668d

Please sign in to comment.