啟動事件
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
您的視覺呈現可以觸發代管頁面可註冊接收的事件。事件可以透過使用者動作觸發。舉例來說,使用者點選圖表時也可以設為內部觸發事件,例如每 10 秒觸發一個事件。您可以註冊要在每次觸發特定事件時呼叫的 JavaScript 方法,方法可能是使用該事件的專屬資料。
每個視覺呈現都會定義自己的事件,而該視覺化呈現的說明文件也應說明每個事件的觸發時間、相關意義,以及事件處理常式會傳送哪些資訊 (例如「圖表視覺化」)。本頁將說明視覺化內容建立者如何觸發事件。如要瞭解用戶端如何註冊以接收事件,請參閱「處理事件」頁面。
任何可選取的圖表都應觸發一個事件:選取事件。不過,這個事件的行為和意義是由各種視覺呈現定義。
在 draw
方法傳回控制項給使用者後,如果視覺呈現無法立即與使用者互動,表示該視覺化作業應觸發:就緒事件。如要瞭解此事件的確切行為和意義,請參閱「準備事件」一節。
請注意,Visualization API 事件與標準 DOM 事件各自獨立,也不同。
目錄
觸發事件
如要從圖表觸發事件,請呼叫 google.visualization.events.trigger()
函式。這個函式預期的參數如下:
- 來源視覺呈現 (通常是
this
值)。
- 事件名稱 (字串)。
- 事件詳細資料 (物件)。可選用事件詳細資料的對應 (名稱/值)。
以下範例顯示圖表如何擲回選取事件:
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」的事件。此事件不會傳送任何引數至監聽函式。
- 按照連結文件一節中的說明公開
getSelection()
方法。這個方法應會傳回使用者所選 data 元素的索引。
- 按照參考資料一節的說明公開
setSelection()
方法。您也可以參閱「處理事件」頁面,瞭解如何處理事件。
「就緒」事件
任何圖表都應觸發「就緒」的事件,運作方式符合標準,讓開發人員知道視覺化內容已準備好處理稱為「方法」的方法。(不過,視覺化資料並沒有絕對「要求」,請查閱說明文件以取得視覺化結果)。
一般來說,顯示「已就緒」事件的圖表設計時會採用以下規格:
- 就緒事件不會傳遞任何屬性至處理常式 (您的函式處理常式不應預期任何參數傳遞至處理常式)。
- 當圖表可供互動時,視覺呈現應 觸發就緒事件。
如果視覺化的繪圖為非同步,請務必在實際可以呼叫互動方法時觸發事件,而不是只在
draw
方法結束時觸發。
- 請在呼叫
draw
方法之前,先將事件監聽器新增至此事件,否則可能會在設定事件監聽器之前觸發事件,而您也不會擷取到該事件。
- 在啟用完備的事件之前呼叫互動方法,有可能導致這些方法無法正常運作的風險。
慣例是,如果圖表未觸發「已就緒」事件,就可在 draw
方法結束後立即進行互動,並傳回控制項給使用者。
更多資訊
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2024-07-10 (世界標準時間)。
[null,null,["上次更新時間: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)"]]