-
Notifications
You must be signed in to change notification settings - Fork 605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(event): add plot events bind #1412
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
import { Chart } from '@antv/g2'; | ||
import { Chart, Event } from '@antv/g2'; | ||
import { deepMix } from '@antv/util'; | ||
import EE from '@antv/event-emitter'; | ||
import { bind } from 'size-sensor'; | ||
import { Adaptor } from './adaptor'; | ||
import { ChartOptions, Data } from '../types'; | ||
|
||
/** | ||
* 所有 plot 的基类 | ||
*/ | ||
export abstract class Plot<O extends ChartOptions> { | ||
export abstract class Plot<O extends ChartOptions> extends EE { | ||
/** plot 类型名称 */ | ||
public abstract readonly type: string = 'base'; | ||
/** plot 的 schema 配置 */ | ||
|
@@ -20,11 +21,14 @@ export abstract class Plot<O extends ChartOptions> { | |
private unbind: () => void; | ||
|
||
constructor(container: string | HTMLElement, options: O) { | ||
super(); | ||
this.container = typeof container === 'string' ? document.getElementById(container) : container; | ||
const defaultOptions = this.getDefaultOptions(); | ||
this.options = deepMix({}, defaultOptions, options); | ||
|
||
this.options = deepMix({}, this.getDefaultOptions(), options); | ||
|
||
this.createG2(); | ||
|
||
this.bindEvents(); | ||
} | ||
|
||
/** | ||
|
@@ -46,6 +50,19 @@ export abstract class Plot<O extends ChartOptions> { | |
}); | ||
} | ||
|
||
/** | ||
* 绑定代理所有 G2 的事件 | ||
*/ | ||
private bindEvents() { | ||
if (this.chart) { | ||
this.chart.on('*', (e: Event) => { | ||
if (e?.type) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if (e?.type && contains(keys(this.getEvents()), e?.type)) ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 就是每个图表支持哪些 event 有图表决定吗?这样是在 G2 的事件能力上做了限制了~ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 直接透传就行了 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 是的,取决于图表注册了哪些事件,例如 chart.on('line:click',(e)=> console.log(e)); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 然后 line:click 映射到 G2 element:click,但看这一个图表是容易理解了,但是从开发者来看:
比如 G2 中 lable:click,如果在 G2Plot 2.0.0 没有映射掉,如果后面做映射成 pie-label:click 那么就会产生 breakchange,需要发大版本(3.0.0),或者两种命名都支持,上百个事件都存在这类困扰,维护起来也是麻烦。 |
||
this.emit(e.type, e); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* 获取默认的 options 配置项 | ||
* 每个组件都可以复写 | ||
|
@@ -116,6 +133,8 @@ export abstract class Plot<O extends ChartOptions> { | |
this.unbindSizeSensor(); | ||
// G2 的销毁 | ||
this.chart.destroy(); | ||
// 清空已经绑定的事件 | ||
this.off(); | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
element:click 做个映射?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
映射是改事件名称吗?如果 elemnt:click 改成 line:click 可能但这个图来看更语意了,但是就在 G2 的基础增加新概念了。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
感觉 element:click 不够明确。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
考虑用户学习成本,同时学习 G2、G2Plot 概念太多,对于他们后续想要做定制也有理解成本。element 其实很明确了,就是图形元素,这些都可以用一个基础概念教程介绍下就行了