Skip to content

Commit 79c6637

Browse files
authored
test(storybook): chart with decimation story (#813)
1 parent f239b37 commit 79c6637

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@
7070
"@typescript-eslint/parser": "^5.0.0",
7171
"browserslist": "^4.17.3",
7272
"chart.js": "^3.1.0",
73+
"chartjs-adapter-date-fns": "^2.0.0",
7374
"clean-publish": "^3.4.1",
7475
"commitizen": "^4.2.4",
7576
"cz-conventional-changelog": "3.3.0",
77+
"date-fns": "^2.25.0",
7678
"eslint": "^7.32.0",
7779
"eslint-config-prettier": "^8.3.0",
7880
"eslint-config-standard": "^16.0.3",

stories/Chart.data.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,53 @@ export const sameData2 = {
153153
},
154154
],
155155
};
156+
157+
export const decimationOptions = {
158+
// Turn off animations and data parsing for performance
159+
animation: false,
160+
parsing: false,
161+
162+
interaction: {
163+
mode: 'nearest',
164+
axis: 'x',
165+
intersect: false,
166+
},
167+
plugins: {
168+
decimation: {
169+
enabled: true,
170+
algorithm: 'lttb',
171+
samples: 500,
172+
},
173+
},
174+
scales: {
175+
x: {
176+
type: 'time',
177+
ticks: {
178+
source: 'auto',
179+
// Disabled rotation for performance
180+
maxRotation: 0,
181+
autoSkip: true,
182+
},
183+
},
184+
},
185+
};
186+
187+
export const getDecimationData = () => {
188+
const start = Date.now();
189+
const data = Array.from({ length: 100000 }, (_, i) => ({
190+
x: start + i * 30000,
191+
y: faker.datatype.number({ min: 0, max: Math.random() < 0.001 ? 100 : 20 }),
192+
}));
193+
194+
return {
195+
datasets: [
196+
{
197+
borderColor: colorRed,
198+
borderWidth: 1,
199+
data,
200+
label: 'Large Dataset',
201+
radius: 0,
202+
},
203+
],
204+
};
205+
};

stories/Chart.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { useState, useEffect, useReducer } from 'react';
22
import { InteractionItem } from 'chart.js';
3+
import 'chartjs-adapter-date-fns';
34
import Chart from '../src';
45
import * as data from './Chart.data';
56

@@ -114,3 +115,17 @@ export const SameDataToggle = args => {
114115
SameDataToggle.args = {
115116
type: 'bar',
116117
};
118+
119+
export const Decimation = args => {
120+
const [currentData, toggleData] = useReducer(
121+
data.getDecimationData,
122+
data.getDecimationData()
123+
);
124+
125+
return <Chart {...args} data={currentData} onClick={toggleData} />;
126+
};
127+
128+
Decimation.args = {
129+
type: 'line',
130+
options: data.decimationOptions,
131+
};

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4699,6 +4699,11 @@ chart.js@^3.1.0:
46994699
resolved "https://registry.yarnpkg.com/chart.js/-/chart.js-3.6.0.tgz#a87fce8431d4e7c5523d721f487f53aada1e42fe"
47004700
integrity sha512-iOzzDKePL+bj+ccIsVAgWQehCXv8xOKGbaU2fO/myivH736zcx535PGJzQGanvcSGVOqX6yuLZsN3ygcQ35UgQ==
47014701

4702+
chartjs-adapter-date-fns@^2.0.0:
4703+
version "2.0.0"
4704+
resolved "https://registry.yarnpkg.com/chartjs-adapter-date-fns/-/chartjs-adapter-date-fns-2.0.0.tgz#5e53b2f660b993698f936f509c86dddf9ed44c6b"
4705+
integrity sha512-rmZINGLe+9IiiEB0kb57vH3UugAtYw33anRiw5kS2Tu87agpetDDoouquycWc9pRsKtQo5j+vLsYHyr8etAvFw==
4706+
47024707
chokidar@^2.1.8:
47034708
version "2.1.8"
47044709
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-2.1.8.tgz#804b3a7b6a99358c3c5c61e71d8728f041cff917"
@@ -5700,6 +5705,11 @@ data-urls@^2.0.0:
57005705
whatwg-mimetype "^2.3.0"
57015706
whatwg-url "^8.0.0"
57025707

5708+
date-fns@^2.25.0:
5709+
version "2.25.0"
5710+
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-2.25.0.tgz#8c5c8f1d958be3809a9a03f4b742eba894fc5680"
5711+
integrity sha512-ovYRFnTrbGPD4nqaEqescPEv1mNwvt+UTqI3Ay9SzNtey9NZnYu6E2qCcBBgJ6/2VF1zGGygpyTDITqpQQ5e+w==
5712+
57035713
dateformat@^3.0.0:
57045714
version "3.0.3"
57055715
resolved "https://registry.yarnpkg.com/dateformat/-/dateformat-3.0.3.tgz#a6e37499a4d9a9cf85ef5872044d62901c9889ae"

0 commit comments

Comments
 (0)