触发事件
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
可视化图表可以触发托管网页可以注册接收的事件。
事件可以由用户操作触发(例如用户点击图表),也可以是内部事件(例如每 10 秒触发一个事件)。您可以注册一个 JavaScript 方法,以便在触发特定事件(可能使用特定于该事件的数据)时调用该方法。
每个可视化图表都会定义自己的事件,并且该可视化图表的文档应描述每个事件的触发时间、事件含义及其向事件处理脚本发送的信息(例如,请参阅组织图表可视化图表)。本页面介绍了可视化图表创建者如何触发事件。
如需了解客户端如何注册以接收事件,请参阅处理事件页面。
任何可选择的可视化图表都应触发一个事件:选择事件。不过,此事件的行为和含义由每个可视化图表定义。
如果在 draw
方法将控制权交还给用户后,某可视化图表尚未准备好进行互动,则应触发该可视化图表: ready 事件。
就绪事件部分定义了此事件的确切行为和含义。
请务必注意,Visualization API 事件是相互独立的,与标准 DOM 事件不同。
目录
触发事件
如需从可视化图表触发事件,请调用 google.visualization.events.trigger()
函数。该函数需要以下参数:
- 来源可视化(通常是
this
值)。
- 事件名称(字符串)。
- 事件详细信息(对象)。特定事件详细信息的可选映射(名称/值)。
下面的示例展示了可视化图表如何引发 select 事件:
MyVisualization.prototype.onclick = function(rowIndex) {
this.highlightRow(this.selectedRow, false); // Clear previous selection
this.highlightRow(rowIndex, true); // Highlight new selection
// Save the selected row index in case getSelection is called.
this.selectedRow = rowIndex;
// Fire a select event.
google.visualization.events.trigger(this, 'select', {});
};
托管网页可以通过调用 google.visualization.events.addListener()
或 google.visualization.events.addOneTimeListener()
进行注册以接收您的事件。请务必全面记录您触发的所有事件。
选择事件
“选择”事件是许多可视化图表为响应用户鼠标点击而抛出的标准事件。如果您选择触发事件来响应鼠标点击,则应以下述标准方式实现“select”事件:
- 当用户选择可视化图表中的某些数据时,触发名为“select”的事件。该事件不会向监听函数发送任何参数。
- 按照链接的文档部分中的说明公开
getSelection()
方法。此方法应返回用户选择的 data 元素的索引。
- 按照参考文档部分中的说明公开
setSelection()
方法。另请参阅处理事件页面,了解如何处理事件。
就绪事件
任何可视化图表都应触发一个以标准方式工作的“ready”事件,以告知开发者该可视化图表何时可以处理(称为方法)。(不过,没有绝对要求可视化图表会以这种方式运作;请查看有关可视化图表的文档。)
一般来说,公开“ready”事件的可视化图表采用以下规范:
- ready 事件不会向该处理程序传递任何属性(您的函数处理程序不应期望向其传递任何参数)。
- 可视化图表应该 在准备好进行互动后触发 ready 事件。
如果可视化图表的绘制是异步进行的,则务必要在可以实际调用互动方法时(而不仅仅是在
draw
方法结束时)触发事件。
- 应该在调用
draw
方法之前向此事件添加监听器,否则可能会在设置监听器之前触发该事件,您无法捕获到它。
- 如果在 ready 事件触发之前调用互动方法,就会面临这些方法无法正常运行的风险。
按照惯例,未触发“ready”事件的可视化图表会在 draw
方法结束后立即进行互动,并将控制权交还给用户。
更多信息
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2024-07-10。
[null,null,["最后更新时间 (UTC):2024-07-10。"],[[["\u003cp\u003eVisualization creators can fire custom events or standard events like 'select' and 'ready' using \u003ccode\u003egoogle.visualization.events.trigger()\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe 'select' event is fired when a user interacts with data, requiring \u003ccode\u003egetSelection()\u003c/code\u003e and \u003ccode\u003esetSelection()\u003c/code\u003e methods for data retrieval and manipulation.\u003c/p\u003e\n"],["\u003cp\u003eThe 'ready' event signals that a visualization is ready for user interaction, typically fired after the \u003ccode\u003edraw()\u003c/code\u003e method completes; any interaction before this event might not work as expected.\u003c/p\u003e\n"],["\u003cp\u003eHost pages can listen for these events using \u003ccode\u003egoogle.visualization.events.addListener()\u003c/code\u003e or \u003ccode\u003egoogle.visualization.events.addOneTimeListener()\u003c/code\u003e to respond accordingly.\u003c/p\u003e\n"],["\u003cp\u003eWhile each visualization defines its own events, the 'select' and 'ready' events follow specific guidelines for consistency and usability.\u003c/p\u003e\n"]]],[],null,["# Firing Events\n\nYour visualization can fire events that a host page can register to receive.\nEvents can be fired by user actions: for example a user clicks on a chart,\nor can be internal: for example, firing an event every 10 seconds. You can register\na Javascript method to be called whenever certain events are fired, possibly with\ndata specific to that event.\n\nEvery visualization defines its own events, and the documentation for that visualization\nshould describe when each event is fired, what it means, and what information it\nsends to your event handler (for example, see the [orgchart\nvisualization](/chart/interactive/docs/gallery/orgchart)). This page describes how a visualization creator can fire events.\nTo learn how clients can register to receive events, see the [Handling\nEvents](/chart/interactive/docs/events) page.\n\nThere is one event that any selectable visualization should fire: the select event.\nHowever, the behavior and meaning of this event is defined by each visualization.\n\nIf a visualization is not ready for interaction immediately after the `draw` method returns control to the user, the visualization should fire: the ready event.\nThe exact behavior and meaning of this event is defined in [The Ready Event](#The_Ready_Event) section.\n\nIt is important to note that the Visualization API events are separate and distinct\nfrom the standard DOM events.\n\nContents\n--------\n\n1. [Firing an Event](#Triggering_an_Event)\n2. [The Select Event](#The_Select_Event)\n3. [The Ready Event](#The_Ready_Event)\n4. [More Information](#moreinfo)\n\nFiring an Event\n---------------\n\nTo fire an event from your visualization, call the [google.visualization.events.trigger()](/chart/interactive/docs/reference#trigger) function.\nThe function expects the following parameters:\n\n1. Source visualization (typically this is the `this` value).\n2. Event name (string).\n3. Event details (Object). An optional map (name/value) of specific event details.\n\n\nThe following example shows how a visualization throws the select event: \n\n```carbon\nMyVisualization.prototype.onclick = function(rowIndex) {\n this.highlightRow(this.selectedRow, false); // Clear previous selection\n this.highlightRow(rowIndex, true); // Highlight new selection\n\n // Save the selected row index in case getSelection is called.\n this.selectedRow = rowIndex;\n\n // Fire a select event.\n google.visualization.events.trigger(this, 'select', {});\n};\n```\n\nHosting pages can register to receive your events by calling [google.visualization.events.addListener()](/chart/interactive/docs/reference#addlistener) or [google.visualization.events.addOneTimeListener()](/chart/interactive/docs/reference#addonetimelistener).\nBe sure to document thoroughly any events that you fire.\n\nThe Select Event\n----------------\n\nThe \"select\" event is a standard event thrown by many visualizations\nin response to a user mouse click. If you choose to fire an event in response to\nmouse clicks, you should implement the \"select\" event in the standard way described\nhere:\n\n1. Fire an event with the name 'select' when the user selects some data within the visualization. The event does not send any arguments to the listening functions.\n2. Expose the [getSelection()](/chart/interactive/docs/reference#visgetselection) method as described in the linked document section. This method should return the indexes of **data** elements that the user selected. \n3. Expose a [setSelection()](/chart/interactive/docs/reference#vissetselection) method as described in the [reference](/chart/interactive/docs/reference#vissetselection) section. Also see the [handling events](/chart/interactive/docs/events) page to learn how to handle events.\n\n\u003cbr /\u003e\n\nThe Ready Event\n---------------\n\nAny visualization should fire a \"ready\" event that works in a standard way to let the developer know when the visualization\nis ready to process called methods. (However, there is no absolute *requirement* that\na visualization behave this way; check the documentation for your visualization).\n\nIn general, visualizations that expose the \"ready\" event are designed with the\nfollowing specifications:\n\n- The ready event does not pass any properties to the handler (your function handler should not expect any parameters to be passed to it).\n- The visualization *should* fire the ready event after the visualization is ready for interaction. If the drawing of the visualization is asynchronous, it is important that the event is fired when interaction methods can actually be called, and not just when the `draw` method ends.\n- Adding a listener to this event should be done before calling the `draw` method, because otherwise the event might be fired before the listener is set up and you will not catch it.\n- By calling interaction methods before the ready event is fired, you take a risk that these methods will not work properly.\n\n\nThe convention is that visualizations which do not fire a \"ready\" event are ready for interaction immediately after the `draw` method ends and returns control to the user.\n\nMore Information\n----------------\n\n- [Controls and Dashboards](/chart/interactive/docs/gallery/controls)\n- [getSelection()](/chart/interactive/docs/reference#visgetselection)\n- [`google.visualization.events.addListener()`](/chart/interactive/docs/reference)"]]