-
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 legend highlight
- Loading branch information
Showing
8 changed files
with
232 additions
and
14 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
__tests__/integration/api-chart-emit-legend-highlight.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,71 @@ | ||
import { chartEmitLegendHighlight as render } from '../plots/api/chart-emit-legend-highlight'; | ||
import { createNodeGCanvas } from './utils/createNodeGCanvas'; | ||
import { sleep } from './utils/sleep'; | ||
import { kebabCase } from './utils/kebabCase'; | ||
import { createPromise, dispatchFirstShapeEvent } from './utils/event'; | ||
import './utils/useSnapshotMatchers'; | ||
import './utils/useCustomFetch'; | ||
import { | ||
LEGEND_ITEMS_CLASS_NAME, | ||
CATEGORY_LEGEND_CLASS_NAME, | ||
} from '../../src/interaction/legendFilter'; | ||
|
||
describe('chart.emit', () => { | ||
const dir = `${__dirname}/snapshots/api/${kebabCase(render.name)}`; | ||
const canvas = createNodeGCanvas(800, 500); | ||
|
||
it('chart.on("legend:highlight") should receive expected data.', async () => { | ||
const { chart, finished } = render({ | ||
canvas, | ||
container: document.createElement('div'), | ||
}); | ||
await finished; | ||
await sleep(20); | ||
|
||
// chart.emit('legend:highlight', options) should trigger slider. | ||
chart.emit('legend:highlight', { | ||
data: { channel: 'color', value: 'Increase' }, | ||
}); | ||
await sleep(20); | ||
await expect(canvas).toMatchCanvasSnapshot(dir, 'step0'); | ||
|
||
// chart.emit('legend:unhighlight', options) should reset. | ||
chart.emit('legend:unhighlight', {}); | ||
await sleep(20); | ||
await expect(canvas).toMatchCanvasSnapshot(dir, 'step1'); | ||
|
||
chart.off(); | ||
|
||
// chart.on("legend:unhighlight") should be called. | ||
const [unhighlight, resolveUnhighlight] = createPromise(); | ||
chart.on('legend:unhighlight', (event) => { | ||
if (!event.nativeEvent) return; | ||
resolveUnhighlight(); | ||
}); | ||
dispatchFirstShapeEvent( | ||
canvas, | ||
CATEGORY_LEGEND_CLASS_NAME, | ||
'pointerleave', | ||
{ nativeEvent: true }, | ||
); | ||
await sleep(20); | ||
await unhighlight; | ||
|
||
// chart.on("legend:highlight") should receive expected data. | ||
const [highlight, resolveHighlight] = createPromise(); | ||
chart.on('legend:highlight', (event) => { | ||
if (!event.nativeEvent) return; | ||
expect(event.data).toEqual({ channel: 'color', value: 'Increase' }); | ||
resolveHighlight(); | ||
}); | ||
dispatchFirstShapeEvent(canvas, LEGEND_ITEMS_CLASS_NAME, 'pointerover', { | ||
nativeEvent: true, | ||
}); | ||
await sleep(20); | ||
await highlight; | ||
}); | ||
|
||
afterAll(() => { | ||
canvas?.destroy(); | ||
}); | ||
}); |
Binary file added
BIN
+12 KB
__tests__/integration/snapshots/api/chart-emit-legend-highlight/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
+11.5 KB
__tests__/integration/snapshots/api/chart-emit-legend-highlight/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
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 { Chart } from '../../../src'; | ||
import { profit } from '../../data/profit'; | ||
|
||
export function chartEmitLegendHighlight(context) { | ||
const { container, canvas } = context; | ||
|
||
// button | ||
const button = document.createElement('button'); | ||
button.innerText = 'highlight'; | ||
container.appendChild(button); | ||
|
||
const button1 = document.createElement('button'); | ||
button1.innerText = 'reset'; | ||
container.appendChild(button1); | ||
|
||
// wrapperDiv | ||
const wrapperDiv = document.createElement('div'); | ||
container.appendChild(wrapperDiv); | ||
|
||
const chart = new Chart({ | ||
theme: 'classic', | ||
container: wrapperDiv, | ||
canvas, | ||
}); | ||
|
||
chart.options({ | ||
paddingLeft: 60, | ||
type: 'interval', | ||
data: profit, | ||
axis: { y: { labelFormatter: '~s' } }, | ||
encode: { | ||
x: 'month', | ||
y: ['end', 'start'], | ||
color: (d) => | ||
d.month === 'Total' ? 'Total' : d.profit > 0 ? 'Increase' : 'Decrease', | ||
}, | ||
state: { inactive: { opacity: 0.5 } }, | ||
legend: { | ||
color: { state: { inactive: { labelOpacity: 0.5, markerOpacity: 0.5 } } }, | ||
}, | ||
interaction: { | ||
legendHighlight: true, | ||
tooltip: false, | ||
}, | ||
}); | ||
|
||
chart.on('legend:highlight', (e) => { | ||
const { nativeEvent, data } = e; | ||
if (!nativeEvent) return; | ||
console.log(data); | ||
}); | ||
|
||
chart.on('legend:unhighlight', (e) => { | ||
const { nativeEvent } = e; | ||
if (!nativeEvent) return; | ||
console.log('unhighlight'); | ||
}); | ||
|
||
button.onclick = () => { | ||
chart.emit('legend:highlight', { | ||
data: { channel: 'color', value: 'Increase' }, | ||
}); | ||
}; | ||
|
||
button1.onclick = () => { | ||
chart.emit('legend:unhighlight', {}); | ||
}; | ||
|
||
const finished = chart.render(); | ||
|
||
return { chart, finished }; | ||
} |
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
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
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