Skip to content

Commit

Permalink
fix: data数量小于rate (#1880)
Browse files Browse the repository at this point in the history
Co-authored-by: xuying.xu <xuying.xu@alibaba-inc.com>
  • Loading branch information
tangying1027 and xuying.xu committed Nov 2, 2023
1 parent 7394a0f commit 64de0a7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/f2-algorithm/src/lttbDownSample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export default function lttbDownSample(data, options?: OptionsProps) {
const { rate = 5, dimension = 'value' } = options;
const len = data.length;
const targetCount = len / rate;
if (targetCount >= len || targetCount === 0) {

if (rate >= len || targetCount === 0) {
return data;
}

Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions packages/f2-algorithm/test/sample.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,41 @@ describe('sample', () => {
expect(context).toMatchImageSnapshot();
});

it('rate 小于 data个数', async () => {
const res = await fetch(url);
const result = [
{ value: { value: '0.516' }, updateTime: '2023-10-26T16:00:00.000Z', extInfo: null },
{ value: { value: '0.504' }, updateTime: '2023-10-27T16:00:00.000Z', extInfo: null },
{ value: { value: '0.488' }, updateTime: '2023-10-28T16:00:00.000Z', extInfo: null },
{ value: { value: '0.516' }, updateTime: '2023-10-29T16:00:00.000Z', extInfo: null },
{ value: { value: '0.472' }, updateTime: '2023-10-30T16:00:00.000Z', extInfo: null },
{ value: { value: '0.448' }, updateTime: '2023-10-31T16:00:00.000Z', extInfo: null },
];
const data = result.map((d) => {
return {
date: d.updateTime,
value: Number(d.value.value),
};
});
const context = createContext('lttb', { width: '300px', height: '200px' });
const sampleData = lttbDownSample(data, {
rate: 7,
dimension: 'value',
});
const { props } = (
<Canvas context={context} pixelRatio={1}>
<Chart data={sampleData}>
<Line x="date" y="value" />
</Chart>
</Canvas>
);

const canvas = new Canvas(props);
await canvas.render();

await delay(1000);
expect(context).toMatchImageSnapshot();
});
it('nearest sample', async () => {
const res = await fetch(url);
const result = await res.json();
Expand Down

0 comments on commit 64de0a7

Please sign in to comment.