-
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(element): handle click event (close: #4665)
- Loading branch information
Showing
10 changed files
with
266 additions
and
5 deletions.
There are no files selected for viewing
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,38 @@ | ||
import { CustomEvent } from '@antv/g'; | ||
import { chartOnItemElement as render } from '../plots/api/chart-on-item-element'; | ||
import { createDOMGCanvas } from './utils/createDOMGCanvas'; | ||
import { sleep } from './utils/sleep'; | ||
import './utils/useSnapshotMatchers'; | ||
|
||
describe('chart.on', () => { | ||
const canvas = createDOMGCanvas(640, 480); | ||
|
||
it('chart.on("element:click", callback) should provide datum for item element', async () => { | ||
const { finished, chart } = render({ canvas }); | ||
await finished; | ||
await sleep(20); | ||
|
||
await new Promise<void>((resolve) => { | ||
let count = 0; | ||
const click = (event) => { | ||
count++; | ||
expect(event.dataset.data).toEqual({ | ||
genre: 'Sports', | ||
sold: 275, | ||
}); | ||
if (count >= 2) resolve(); | ||
}; | ||
|
||
chart.off(); | ||
chart.on('element:click', click); | ||
chart.on('interval:click', click); | ||
|
||
const [element] = canvas.document.getElementsByClassName('element'); | ||
element.dispatchEvent(new CustomEvent('click')); | ||
}); | ||
}); | ||
|
||
afterAll(() => { | ||
canvas?.destroy(); | ||
}); | ||
}); |
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,98 @@ | ||
import { CustomEvent } from '@antv/g'; | ||
import { chartOnSeriesElement as render } from '../plots/api/chart-on-series-element'; | ||
import { createDOMGCanvas } from './utils/createDOMGCanvas'; | ||
import { sleep } from './utils/sleep'; | ||
import './utils/useSnapshotMatchers'; | ||
|
||
const data = [ | ||
{ | ||
month: 'Jan', | ||
city: 'Tokyo', | ||
temperature: 7, | ||
}, | ||
{ | ||
month: 'Feb', | ||
city: 'Tokyo', | ||
temperature: 6.9, | ||
}, | ||
{ | ||
month: 'Mar', | ||
city: 'Tokyo', | ||
temperature: 9.5, | ||
}, | ||
{ | ||
month: 'Apr', | ||
city: 'Tokyo', | ||
temperature: 14.5, | ||
}, | ||
{ | ||
month: 'May', | ||
city: 'Tokyo', | ||
temperature: 18.4, | ||
}, | ||
{ | ||
month: 'Jun', | ||
city: 'Tokyo', | ||
temperature: 21.5, | ||
}, | ||
{ | ||
month: 'Jul', | ||
city: 'Tokyo', | ||
temperature: 25.2, | ||
}, | ||
{ | ||
month: 'Aug', | ||
city: 'Tokyo', | ||
temperature: 26.5, | ||
}, | ||
{ | ||
month: 'Sep', | ||
city: 'Tokyo', | ||
temperature: 23.3, | ||
}, | ||
{ | ||
month: 'Oct', | ||
city: 'Tokyo', | ||
temperature: 18.3, | ||
}, | ||
{ | ||
month: 'Nov', | ||
city: 'Tokyo', | ||
temperature: 13.9, | ||
}, | ||
{ | ||
month: 'Dec', | ||
city: 'Tokyo', | ||
temperature: 9.6, | ||
}, | ||
]; | ||
|
||
describe('chart.on', () => { | ||
const canvas = createDOMGCanvas(640, 480); | ||
|
||
it('chart.on("element:click", callback) should provide data for series element', async () => { | ||
const { finished, chart } = render({ canvas }); | ||
await finished; | ||
await sleep(20); | ||
|
||
await new Promise<void>((resolve) => { | ||
let count = 0; | ||
const click = (event) => { | ||
count++; | ||
expect(event.dataset.data).toEqual(data); | ||
if (count >= 2) resolve(); | ||
}; | ||
|
||
chart.off(); | ||
chart.on('element:click', click); | ||
chart.on('line:click', click); | ||
|
||
const [element] = canvas.document.getElementsByClassName('element'); | ||
element.dispatchEvent(new CustomEvent('click')); | ||
}); | ||
}); | ||
|
||
afterAll(() => { | ||
canvas?.destroy(); | ||
}); | ||
}); |
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,37 @@ | ||
import { Chart } from '../../../src'; | ||
|
||
export function chartOnItemElement(context) { | ||
const { container, canvas } = context; | ||
|
||
const chart = new Chart({ | ||
container, | ||
canvas, | ||
}); | ||
|
||
chart.data([ | ||
{ genre: 'Sports', sold: 275 }, | ||
{ genre: 'Strategy', sold: 115 }, | ||
{ genre: 'Action', sold: 120 }, | ||
{ genre: 'Shooter', sold: 350 }, | ||
{ genre: 'Other', sold: 150 }, | ||
]); | ||
|
||
chart | ||
.interval() | ||
.encode('x', 'genre') | ||
.encode('y', 'sold') | ||
.encode('color', 'genre') | ||
.axis({ x: { animate: false }, y: { animate: false } }); | ||
|
||
chart.on('interval:click', (e) => { | ||
console.log(e.dataset.data); | ||
}); | ||
|
||
chart.on('element:click', (e) => { | ||
console.log(e.dataset.data); | ||
}); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Chart } from '../../../src'; | ||
import { temperatures } from '../../data/temperatures'; | ||
|
||
export function chartOnSeriesElement(context) { | ||
const { container, canvas } = context; | ||
|
||
const chart = new Chart({ | ||
container, | ||
canvas, | ||
}); | ||
|
||
chart.data(temperatures); | ||
|
||
chart | ||
.line() | ||
.encode('x', 'month') | ||
.encode('y', 'temperature') | ||
.encode('color', 'city') | ||
.style('strokeWidth', 10); | ||
|
||
chart.on('line:click', (e) => { | ||
console.log(e.dataset.data); | ||
}); | ||
|
||
chart.on('element:click', (e) => { | ||
console.log(e.dataset.data); | ||
}); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
function dataOf(element, view) { | ||
const { __data__: datum } = element; | ||
const { markKey, index, seriesIndex } = datum; | ||
const { markState } = view; | ||
const selectedMark: any = Array.from(markState.keys()).find( | ||
(mark) => (mark as any).key === markKey, | ||
); | ||
if (!selectedMark) return; | ||
if (seriesIndex) { | ||
return seriesIndex.map((i) => selectedMark.data[i]); | ||
} | ||
return selectedMark.data[index]; | ||
} | ||
|
||
function clickEvent(view, emitter) { | ||
return (e) => { | ||
const { target } = e; | ||
const { classList } = target; | ||
const [elementType, markType] = classList; | ||
if (elementType === 'element') { | ||
const data = dataOf(target, view); | ||
const { dataset = {} } = e; | ||
dataset.data = data; | ||
e.dataset = dataset; | ||
emitter.emit('element:click', e); | ||
emitter.emit(`${markType}:click`, e); | ||
return; | ||
} | ||
// @todo Handle click axis and legend. | ||
emitter.emit(`${elementType}:click`); | ||
}; | ||
} | ||
|
||
// @todo More events such as mouseenter, mouseleave. | ||
// @todo Provide more info for event.dataset. | ||
export function Event() { | ||
return (context, _, emitter) => { | ||
const { container, view } = context; | ||
const click = clickEvent(view, emitter); | ||
container.addEventListener('click', click); | ||
return () => { | ||
container.removeEventListener('click', click); | ||
}; | ||
}; | ||
} |
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