在 Amazon 中在运行时添加嵌入式回调操作 QuickSight - 亚马逊 QuickSight
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

重要:我们已经重新设计了 Amazon QuickSight 分析工作空间。您可能会遇到无法反映 QuickSight 控制台新外观的屏幕截图或程序化文本。我们正在更新屏幕截图和过程文本。

要查找特征或项目,请使用快速搜索栏

有关新外观 QuickSight的更多信息,请参阅在 Amazon 上引入全新的分析体验 QuickSight

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

在 Amazon 中在运行时添加嵌入式回调操作 QuickSight

使用嵌入式数据点回调操作在您的软件即服务 (SaaS) 应用程序与 Amazon QuickSight 嵌入式控制面板和视觉对象之间建立更紧密的集成。开发人员可以使用 QuickSight 嵌入式 SDK 注册要回调的数据点。当您为视觉对象注册回调操作时,读者可以在视觉对象上选择一个数据点,用来接收会针对所选数据点提供数据的回调。此信息可用于标记关键记录、编译针对数据点的原始数据、捕获记录以及为后端进程编译数据。

自定义视觉内容、文本框或见解不支持嵌入式回调。

在开始注册回调数据点之前,请将嵌入开发工具包更新到版本 2.3.0。有关使用 Embedding QuickSight SDK 的更多信息,请参阅amazon-quicksight-embedding-sdk上的 GitHub。

可以在运行时通过 SDK 将数据点回调注册到一个或多个视觉对象。 QuickSight 您还可以向 VisualCustomActionAPI 结构支持的任何交互注册 dataPoint 回调。当用户在视觉对象上选择数据点或从数据点上下文菜单中选择数据点时,此操作允许启动数据点回调。以下示例注册了一个数据点回调,读者在视觉对象上选择数据点时会启动该回调。

/const MY_GET_EMBED_URL_ENDPOINT = "https://my.api.endpoint.domain/MyGetEmbedUrlApi"; // Sample URL // The dashboard id to embed const MY_DASHBOARD_ID = "my-dashboard"; // Sample ID // The container element in your page that will have the embedded dashboard const MY_DASHBOARD_CONTAINER = "#experience-container"; // Sample ID // SOME HELPERS const ActionTrigger = { DATA_POINT_CLICK: "DATA_POINT_CLICK", DATA_POINT_MENU: "DATA_POINT_MENU", }; const ActionStatus = { ENABLED: "ENABLED", DISABLED: "DISABLED", }; // This function makes a request to your endpoint to obtain an embed url for a given dashboard id // The example implementation below assumes the endpoint takes dashboardId as request data // and returns an object with EmbedUrl property const myGetEmbedUrl = async (dashboardId) => { const apiOptions = { dashboardId, }; const apiUrl = new URL(MY_GET_EMBED_URL_ENDPOINT); apiUrl.search = new URLSearchParams(apiOptions).toString(); const apiResponse = await fetch(apiUrl.toString()); const apiResponseData = await apiResponse.json(); return apiResponseData.EmbedUrl; }; // This function constructs a custom action object const myConstructCustomActionModel = ( customActionId, actionName, actionTrigger, actionStatus ) => { return { Name: actionName, CustomActionId: customActionId, Status: actionStatus, Trigger: actionTrigger, ActionOperations: [ { CallbackOperation: { EmbeddingMessage: {}, }, }, ], }; }; // This function adds a custom action on the first visual of first sheet of the embedded dashboard const myAddVisualActionOnFirstVisualOfFirstSheet = async ( embeddedDashboard ) => { // 1. List the sheets on the dashboard const { SheetId } = (await embeddedDashboard.getSheets())[0]; // If you'd like to add action on the current sheet instead, you can use getSelectedSheetId method // const SheetId = await embeddedDashboard.getSelectedSheetId(); // 2. List the visuals on the specified sheet const { VisualId } = (await embeddedDashboard.getSheetVisuals(SheetId))[0]; // 3. Add the custom action to the visual try { const customActionId = "custom_action_id"; // Sample ID const actionName = "Flag record"; // Sample name const actionTrigger = ActionTrigger.DATA_POINT_CLICK; // or ActionTrigger.DATA_POINT_MENU const actionStatus = ActionStatus.ENABLED; const myCustomAction = myConstructCustomActionModel( customActionId, actionName, actionTrigger, actionStatus ); const response = await embeddedDashboard.addVisualActions( SheetId, VisualId, [myCustomAction] ); if (!response.success) { console.log("Adding visual action failed", response.errorCode); } } catch (error) { console.log("Adding visual action failed", error); } }; const parseDatapoint = (visualId, datapoint) => { datapoint.Columns.forEach((Column, index) => { // FIELD | METRIC const columnType = Object.keys(Column)[0]; // STRING | DATE | INTEGER | DECIMAL const valueType = Object.keys(Column[columnType])[0]; const { Column: columnMetadata } = Column[columnType][valueType]; const value = datapoint.RawValues[index][valueType]; const formattedValue = datapoint.FormattedValues[index]; console.log( `Column: ${columnMetadata.ColumnName} has a raw value of ${value} and formatted value of ${formattedValue.Value} for visual: ${visualId}` ); }); }; // This function is used to start a custom workflow after the end user selects a datapoint const myCustomDatapointCallbackWorkflow = (callbackData) => { const { VisualId, Datapoints } = callbackData; parseDatapoint(VisualId, Datapoints); }; // EMBEDDING THE DASHBOARD const main = async () => { // 1. Get embed url let url; try { url = await myGetEmbedUrl(MY_DASHBOARD_ID); } catch (error) { console.log("Obtaining an embed url failed"); } if (!url) { return; } // 2. Create embedding context const embeddingContext = await createEmbeddingContext(); // 3. Embed the dashboard const embeddedDashboard = await embeddingContext.embedDashboard( { url, container: MY_DASHBOARD_CONTAINER, width: "1200px", height: "300px", resizeHeightOnSizeChangedEvent: true, }, { onMessage: async (messageEvent) => { const { eventName, message } = messageEvent; switch (eventName) { case "CONTENT_LOADED": { await myAddVisualActionOnFirstVisualOfFirstSheet(embeddedDashboard); break; } case "CALLBACK_OPERATION_INVOKED": { myCustomDatapointCallbackWorkflow(message); break; } } }, } ); }; main().catch(console.error);

您也可以将前面的示例配置为在用户打开上下文菜单时启动数据点回调。要在前面的示例中执行此操作,请将 actionTrigger 的值设置为 ActionTrigger.DATA_POINT_MENU

注册数据点回调后,就会应用到指定视觉对象上的大多数数据点。回调不适用于视觉对象的总计或小计。当读者与数据点交互时,会向嵌入式 SDK 发出一条CALLBACK_OPERATION_INVOKED消息。 QuickSight 此消息由 onMessage 处理程序捕获。此消息包含与所选数据点关联的整行数据的原始值和显示值。其中还包含内含数据点的视觉对象中所有列的列元数据。以下是 CALLBACK_OPERATION_INVOKED 消息的示例。

{ CustomActionId: "custom_action_id", DashboardId: "dashboard_id", SheetId: "sheet_id", VisualId: "visual_id", DataPoints: [ { RawValues: [ { String: "Texas" // 1st raw value in row }, { Integer: 1000 // 2nd raw value in row } ], FormattedValues: [ {Value: "Texas"}, // 1st formatted value in row {Value: "1,000"} // 2nd formatted value in row ], Columns: [ { // 1st column metadata Dimension: { String: { Column: { ColumnName: "State", DatsetIdentifier: "..." } } } }, { // 2nd column metadata Measure: { Integer: { Column: { ColumnName: "Cancelled", DatsetIdentifier: "..." }, AggregationFunction: { SimpleNumericalAggregation: "SUM" } } } } ] } ] }