使用輔助程式庫

Looker Studio 會使用 postMessage 提供資料和樣式資訊給包含 社群視覺呈現本指南將詳細說明如何使用輔助程式 資源庫。

以社群視覺呈現 ds-component 提供兩個函式。

  1. 取得 iframe 的尺寸
  2. 管理與 Looker Studio 的通訊

訂閱活動

當使用者與您的圖表互動 (例如變更所選欄位) 時, 或調整元件大小,Looker Studio 會將事件傳送至 圖表

dscc.subscribeToData 會註冊回呼,系統會在每個 postMessage 事件。系統會將回呼傳遞 data 物件。

function drawViz(data){
 
// obtain the height and width to scale your visualization appropriately
 
var height = dscc.getHeight();
 
var width = dscc.getWidth();
 
// write your visualization code here
  console
.log("I'm the callback and I was passed this data: " + JSON.stringify(data, null, '  '));
}

// call drawViz every time Looker Studio sends a new postMessage
dscc
.subscribeToData(drawViz, {transform: dscc.objectTransform});

傳回的資料

兩個資料轉換都會傳回具有五個鍵的物件。

目的
style 使用者選取和預設樣式資訊
fields 使用者選取的欄位資訊
interactions 使用者選取的互動
theme 檢舉主題資訊
tables 資料列
dateRanges 預設和比較日期範圍

資料格式:

{
  fields
: object(fieldsByConfigId),
  style
: object(styleById),
  interactions
: object(interactionsById),
  theme
: object(themeStyle),
  tables
: object(tablesById),
  dateRanges
: object(dateRangesById),
}

不同的圖表需要採用不同的資料格式。常見的兩種格式 陣列或物件陣列。ds-component 程式庫提供兩種 轉換功能,方便您直接使用這些資料格式。

這兩種轉換記錄在 library-reference:這些 可輕鬆映射到 JavaScript 常見的資料格式 視覺化程式庫系統支援的兩種轉換如下:objectTransform、 ,而傳回物件的陣列;以及傳回陣列的 tableTransform 陣列。

dscc.objectTransform

有些視覺化效果預期資料是物件陣列。

例如:

var data = [
 
{'colA': 'hello', 'colB', 'world'},
 
{'colA': 'hello', 'colB', 'world'}
];

以下程式碼顯示如何從 dscc.objectTransform 格式。


function drawViz(data){
 
// what the object transform could look like
 
// [
 
//  {'configId1': ['hello'], 'configId2': [1] },
 
//  {'configId1': ['world'], 'configId2': [2] }
 
// ]
 
var dsccObjectTransformData = data.tables.DEFAULT;

 
// creating an array of objects
 
var arrayOfObjects = dscc.ObjectTransformData.rows.map(function(d){
   
return {
     
'configId1': d.configId1[0],
     
'configId2': d.configId2[0]
   
};
 
};

}

如果定義資料區段後,使用者將可輸入多個欄位 (例如 舉例來說,如果為桑基圖定義了兩個維度,則 轉換會因用途而異,因為 Looker 傳回的資料格式 Studio 看起來會更像是:


var dsccObjectTransformData = [
{'configId1': ['hello', 'there'], 'configId2': [1] },
{'configId1': ['world', 'globe'], 'configId2': [2] }
]

dscc.tableTransform

有些視覺化程式庫需要陣列陣列。

例如:

var data = [
 
['hello', 1],
 
['world', 2]
];

下列程式碼顯示如何從 dscc.tableTransform 格式。


function drawViz(data);
 
// what the below object looks like
 
// {
 
//    headers: [{
 
//      "id": "qt_ky8sltutsb",
 
//      "name": "dimension",
 
//      "type": "TEXT",
 
//      "concept": "DIMENSION",
 
//      "configId": "configId1"
 
//    }, {
 
//      "id": "qt_m9dtntutsb",
 
//      "name": "metric",
 
//      "type": "NUMBER",
 
//      "concept": "METRIC",
 
//      "configId": "configId2"
 
//    }],
 
//  rows: [
 
//    ['hello', 1],
 
//    ['world', 2]
 
//  ];
 
// }
 
var dsccTableTransformObject = data.tables.DEFAULT;

 
// accessing the row of rows
 
var rowOfRows = dsccTableTransformObject.rows;

 
// accessing the header row
 
var headers = dsccTableTransformObject.headers;
}

dscc
.subscribeToData(drawViz, {transform: tableTransform});