-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(interaction): emit brush axis highlight
- Loading branch information
Showing
22 changed files
with
711 additions
and
18 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
__tests__/integration/api-chart-emit-brush-highlight-axis-cross.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { chartEmitBrushHighlightAxisCross as render } from '../plots/api/chart-emit-brush-highlight-axis-cross'; | ||
import { dblclick, brush } from '../plots/interaction/penguins-point-brush'; | ||
import { AXIS_HOT_AREA_CLASS_NAME } from '../../src/interaction/brushAxisHighlight'; | ||
import { createNodeGCanvas } from './utils/createNodeGCanvas'; | ||
import { createPromise, getElementByClassName } from './utils/event'; | ||
import { sleep } from './utils/sleep'; | ||
import { kebabCase } from './utils/kebabCase'; | ||
import './utils/useCustomFetch'; | ||
import './utils/useSnapshotMatchers'; | ||
|
||
describe('chart.on', () => { | ||
const dir = `${__dirname}/snapshots/api/${kebabCase(render.name)}`; | ||
const canvas = createNodeGCanvas(640, 480); | ||
|
||
it('chart.emit("brushAxis:highlight", callback) should emit events.', async () => { | ||
const { chart, finished } = render({ | ||
canvas, | ||
container: document.createElement('div'), | ||
}); | ||
await finished; | ||
await sleep(20); | ||
|
||
// chart.emit('brushAxis:highlight', options) should trigger slider. | ||
chart.emit('brushAxis:highlight', { | ||
data: { | ||
selection: [ | ||
[40, 50], | ||
[14, 18], | ||
], | ||
}, | ||
}); | ||
await sleep(20); | ||
await expect(canvas).toMatchCanvasSnapshot(dir, 'step0'); | ||
|
||
// chart.emit('brushAxis:remove', options) should reset. | ||
chart.emit('brushAxis:remove', {}); | ||
await sleep(20); | ||
await expect(canvas).toMatchCanvasSnapshot(dir, 'step1'); | ||
|
||
chart.off(); | ||
|
||
const axis = getElementByClassName(canvas, AXIS_HOT_AREA_CLASS_NAME); | ||
|
||
// chart.on("brushAxis:highlight") should receive expected data. | ||
const [highlight, resolveHighlight] = createPromise(); | ||
chart.on('brushAxis:highlight', (event) => { | ||
if (!event.nativeEvent) return; | ||
expect(event.data.selection).toEqual([]); | ||
resolveHighlight(); | ||
}); | ||
brush(axis, -Infinity, 50, Infinity, 400); | ||
await sleep(20); | ||
await highlight; | ||
|
||
// chart.on("brushAxis:remove") should be called. | ||
const [remove, resolveRemove] = createPromise(); | ||
chart.on('brushAxis:remove', (event) => { | ||
if (!event.nativeEvent) return; | ||
resolveRemove(); | ||
}); | ||
dblclick(axis); | ||
await sleep(20); | ||
await remove; | ||
}); | ||
|
||
afterAll(() => { | ||
canvas?.destroy(); | ||
}); | ||
}); |
72 changes: 72 additions & 0 deletions
72
__tests__/integration/api-chart-emit-brush-highlight-axis-horizontal.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { chartEmitBrushHighlightAxisHorizontal as render } from '../plots/api/chart-emit-brush-highlight-axis-horizontal'; | ||
import { dblclick, brush } from '../plots/interaction/penguins-point-brush'; | ||
import { AXIS_HOT_AREA_CLASS_NAME } from '../../src/interaction/brushAxisHighlight'; | ||
import { createNodeGCanvas } from './utils/createNodeGCanvas'; | ||
import { createPromise, getElementByClassName } from './utils/event'; | ||
import { sleep } from './utils/sleep'; | ||
import { kebabCase } from './utils/kebabCase'; | ||
import './utils/useCustomFetch'; | ||
import './utils/useSnapshotMatchers'; | ||
|
||
describe('chart.on', () => { | ||
const dir = `${__dirname}/snapshots/api/${kebabCase(render.name)}`; | ||
const canvas = createNodeGCanvas(640, 800); | ||
|
||
it('chart.emit("brushAxis:highlight", callback) should emit events.', async () => { | ||
const { chart, finished } = render({ | ||
canvas, | ||
container: document.createElement('div'), | ||
}); | ||
await finished; | ||
await sleep(20); | ||
|
||
// chart.emit('brushAxis:highlight', options) should trigger slider. | ||
chart.emit('brushAxis:highlight', { | ||
data: { selection: [[20, 30], undefined, [100, 300]] }, | ||
}); | ||
await sleep(20); | ||
await expect(canvas).toMatchCanvasSnapshot(dir, 'step0'); | ||
|
||
// chart.emit('brushAxis:remove', options) should reset. | ||
chart.emit('brushAxis:remove', {}); | ||
await sleep(20); | ||
await expect(canvas).toMatchCanvasSnapshot(dir, 'step1'); | ||
|
||
chart.off(); | ||
|
||
const axis = getElementByClassName(canvas, AXIS_HOT_AREA_CLASS_NAME); | ||
|
||
// chart.on("brushAxis:highlight") should receive expected data. | ||
const [highlight, resolveHighlight] = createPromise(); | ||
chart.on('brushAxis:highlight', (event) => { | ||
if (!event.nativeEvent) return; | ||
expect(event.data.selection).toEqual([ | ||
[11, 33], | ||
[3, 8], | ||
[68, 455], | ||
[46, 230], | ||
[1613, 5140], | ||
[8, 24.8], | ||
[70, 82], | ||
]); | ||
resolveHighlight(); | ||
}); | ||
brush(axis, 50, -Infinity, 400, Infinity); | ||
await sleep(20); | ||
await highlight; | ||
|
||
// chart.on("brushAxis:remove") should be called. | ||
const [remove, resolveRemove] = createPromise(); | ||
chart.on('brushAxis:remove', (event) => { | ||
if (!event.nativeEvent) return; | ||
resolveRemove(); | ||
}); | ||
dblclick(axis); | ||
await sleep(20); | ||
await remove; | ||
}); | ||
|
||
afterAll(() => { | ||
canvas?.destroy(); | ||
}); | ||
}); |
72 changes: 72 additions & 0 deletions
72
__tests__/integration/api-chart-emit-brush-highlight-axis-vertical.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { chartEmitBrushHighlightAxisVertical as render } from '../plots/api/chart-emit-brush-highlight-axis-vertical'; | ||
import { dblclick, brush } from '../plots/interaction/penguins-point-brush'; | ||
import { AXIS_HOT_AREA_CLASS_NAME } from '../../src/interaction/brushAxisHighlight'; | ||
import { createNodeGCanvas } from './utils/createNodeGCanvas'; | ||
import { createPromise, getElementByClassName } from './utils/event'; | ||
import { sleep } from './utils/sleep'; | ||
import { kebabCase } from './utils/kebabCase'; | ||
import './utils/useCustomFetch'; | ||
import './utils/useSnapshotMatchers'; | ||
|
||
describe('chart.on', () => { | ||
const dir = `${__dirname}/snapshots/api/${kebabCase(render.name)}`; | ||
const canvas = createNodeGCanvas(640, 480); | ||
|
||
it('chart.emit("brushAxis:highlight", callback) should emit events.', async () => { | ||
const { chart, finished } = render({ | ||
canvas, | ||
container: document.createElement('div'), | ||
}); | ||
await finished; | ||
await sleep(20); | ||
|
||
// chart.emit('brushAxis:highlight', options) should trigger slider. | ||
chart.emit('brushAxis:highlight', { | ||
data: { selection: [[20, 30], undefined, [100, 300]] }, | ||
}); | ||
await sleep(20); | ||
await expect(canvas).toMatchCanvasSnapshot(dir, 'step0'); | ||
|
||
// chart.emit('brushAxis:remove', options) should reset. | ||
chart.emit('brushAxis:remove', {}); | ||
await sleep(20); | ||
await expect(canvas).toMatchCanvasSnapshot(dir, 'step1'); | ||
|
||
chart.off(); | ||
|
||
const axis = getElementByClassName(canvas, AXIS_HOT_AREA_CLASS_NAME); | ||
|
||
// chart.on("brushAxis:highlight") should receive expected data. | ||
const [highlight, resolveHighlight] = createPromise(); | ||
chart.on('brushAxis:highlight', (event) => { | ||
if (!event.nativeEvent) return; | ||
expect(event.data.selection).toEqual([ | ||
[14, 44.6], | ||
[3, 8], | ||
[68, 455], | ||
[46, 230], | ||
[1613, 5140], | ||
[8, 24.8], | ||
[70, 82], | ||
]); | ||
resolveHighlight(); | ||
}); | ||
brush(axis, -Infinity, 50, Infinity, 400); | ||
await sleep(20); | ||
await highlight; | ||
|
||
// chart.on("brushAxis:remove") should be called. | ||
const [remove, resolveRemove] = createPromise(); | ||
chart.on('brushAxis:remove', (event) => { | ||
if (!event.nativeEvent) return; | ||
resolveRemove(); | ||
}); | ||
dblclick(axis); | ||
await sleep(20); | ||
await remove; | ||
}); | ||
|
||
afterAll(() => { | ||
canvas?.destroy(); | ||
}); | ||
}); |
Binary file added
BIN
+68 KB
...sts__/integration/snapshots/api/chart-emit-brush-highlight-axis-cross/step0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+67.7 KB
...sts__/integration/snapshots/api/chart-emit-brush-highlight-axis-cross/step1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+708 KB
.../integration/snapshots/api/chart-emit-brush-highlight-axis-horizontal/step0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+769 KB
.../integration/snapshots/api/chart-emit-brush-highlight-axis-horizontal/step1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+481 KB
...__/integration/snapshots/api/chart-emit-brush-highlight-axis-vertical/step0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+510 KB
...__/integration/snapshots/api/chart-emit-brush-highlight-axis-vertical/step1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
__tests__/plots/api/chart-emit-brush-highlight-axis-cross.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { Chart } from '../../../src'; | ||
|
||
export function chartEmitBrushHighlightAxisCross(context) { | ||
const { container, canvas } = context; | ||
|
||
// button | ||
const button1 = document.createElement('button'); | ||
button1.innerText = 'Highlight'; | ||
container.appendChild(button1); | ||
|
||
const button2 = document.createElement('button'); | ||
button2.innerText = 'Reset'; | ||
container.appendChild(button2); | ||
|
||
// wrapperDiv | ||
const wrapperDiv = document.createElement('div'); | ||
container.appendChild(wrapperDiv); | ||
|
||
const chart = new Chart({ | ||
theme: 'classic', | ||
container: wrapperDiv, | ||
canvas, | ||
}); | ||
|
||
chart.options({ | ||
type: 'point', | ||
data: { | ||
type: 'fetch', | ||
value: 'data/penguins.csv', | ||
}, | ||
encode: { | ||
color: 'species', | ||
x: 'culmen_length_mm', | ||
y: 'culmen_depth_mm', | ||
}, | ||
state: { inactive: { stroke: 'gray', opacity: 0.5 } }, | ||
interaction: { | ||
brushAxisHighlight: true, | ||
}, | ||
}); | ||
|
||
const finished = chart.render(); | ||
|
||
chart.on('brushAxis:highlight', (event) => { | ||
const { data, nativeEvent } = event; | ||
if (nativeEvent) console.log('brushAxis:highlight', data); | ||
}); | ||
|
||
chart.on('brushAxis:remove', (event) => { | ||
const { data, nativeEvent } = event; | ||
if (nativeEvent) console.log('brushAxis:remove', data); | ||
}); | ||
|
||
button1.onclick = () => { | ||
chart.emit('brushAxis:highlight', { | ||
data: { | ||
selection: [ | ||
[40, 50], | ||
[14, 18], | ||
], | ||
}, | ||
}); | ||
}; | ||
|
||
button2.onclick = () => { | ||
chart.emit('brushAxis:remove', {}); | ||
}; | ||
|
||
return { chart, finished }; | ||
} |
Oops, something went wrong.