functiondrawViz(data){// obtain the height and width to scale your visualization appropriatelyvarheight=dscc.getHeight();varwidth=dscc.getWidth();// write your visualization code hereconsole.log("I'm the callback and I was passed this data: "+JSON.stringify(data,null,' '));}// call drawViz every time Looker Studio sends a new postMessagedscc.subscribeToData(drawViz,{transform:dscc.objectTransform});
functiondrawViz(data){// what the object transform could look like// [// {'configId1': ['hello'], 'configId2': [1] },// {'configId1': ['world'], 'configId2': [2] }// ]vardsccObjectTransformData=data.tables.DEFAULT;// creating an array of objectsvararrayOfObjects=dscc.ObjectTransformData.rows.map(function(d){return{'configId1':d.configId1[0],'configId2':d.configId2[0]};};}
ユーザーが複数のフィールド(例:
たとえば、サンキーダイアグラムに 2 つのディメンションが定義されている場合、
Looker から返されるデータ形式はユースケースによって異なり、
Studio は次のようになります。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-07-25 UTC。"],[[["\u003cp\u003eLooker Studio uses postMessage to communicate data and styling to embedded community visualizations.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eds-component\u003c/code\u003e library helps manage communication with Looker Studio and get the iframe dimensions.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003edscc.subscribeToData\u003c/code\u003e lets you register a callback to handle data and styling updates from Looker Studio.\u003c/p\u003e\n"],["\u003cp\u003eTwo data transformation options, \u003ccode\u003eobjectTransform\u003c/code\u003e and \u003ccode\u003etableTransform\u003c/code\u003e, help format incoming data for use in visualization libraries.\u003c/p\u003e\n"],["\u003cp\u003eYou can access styling, user interactions, fields, theme, and data through the data object passed to the callback function.\u003c/p\u003e\n"]]],[],null,["# Using the helper library\n\nLooker Studio uses the\n[postMessage](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage)\ninterface to provide data and styling information to the iframe containing the\ncommunity visualization. This guide provides more detail on using the helper\nlibrary.\n\nFor community visualizations,\n[ds-component](/looker-studio/visualization/library) serves two functions.\n\n1. Obtain the dimensions of the iframe\n2. Manage communication with Looker Studio\n\nSubscribing to events\n---------------------\n\nWhen a user interacts with your visualization, such as changing selected fields,\nstyle, or resizing the component, Looker Studio sends events to your\nvisualization.\n\n[`dscc.subscribeToData`](/looker-studio/visualization/library-reference#subscribetodata) registers a callback that will be invoked for each\n`postMessage` event from Looker Studio. The callback is passed a [`data`](/looker-studio/visualization/library-reference#data)\nobject. \n\n function drawViz(data){\n // obtain the height and width to scale your visualization appropriately\n var height = dscc.getHeight();\n var width = dscc.getWidth();\n // write your visualization code here\n console.log(\"I'm the callback and I was passed this data: \" + JSON.stringify(data, null, ' '));\n }\n\n // call drawViz every time Looker Studio sends a new postMessage\n dscc.subscribeToData(drawViz, {transform: dscc.objectTransform});\n\nThe returned data\n-----------------\n\nBoth data transforms return an object with five keys.\n\n| Key | Purpose |\n|----------------|---------------------------------------------|\n| `style` | User-selected and default style information |\n| `fields` | User-selected fields information |\n| `interactions` | User-selected interactions |\n| `theme` | Report theme information |\n| `tables` | Rows of data |\n| `dateRanges` | Default and comparison date ranges |\n\nFormat of the data: \n\n {\n fields: object(fieldsByConfigId),\n style: object(styleById),\n interactions: object(interactionsById),\n theme: object(themeStyle),\n tables: object(tablesById),\n dateRanges: object(dateRangesById),\n }\n\nDifferent visualizations require different data formats. Two common formats are\nan array of arrays or an array of objects. The ds-component library provides two\ntransforms, which are designed to get you straight to these data formats.\n\nThe two transforms are documented in the\n[library-reference](/looker-studio/visualization/library-reference). These\ntransforms map easily to data formats commonly expected by JavaScript\nvisualization libraries. The two supported transforms are: `objectTransform`,\nwhich returns an array of objects, and `tableTransform`, which returns an array\nof arrays.\n\n### `dscc.objectTransform`\n\nSome visualizations expect data as an array of objects.\n\nFor example: \n\n var data = [\n {'colA': 'hello', 'colB', 'world'},\n {'colA': 'hello', 'colB', 'world'}\n ];\n\nThe following code shows how to access an array of objects from the\n`dscc.objectTransform` format. \n\n\n function drawViz(data){\n // what the object transform could look like\n // [\n // {'configId1': ['hello'], 'configId2': [1] },\n // {'configId1': ['world'], 'configId2': [2] }\n // ]\n var dsccObjectTransformData = data.tables.DEFAULT;\n\n // creating an array of objects\n var arrayOfObjects = dscc.ObjectTransformData.rows.map(function(d){\n return {\n 'configId1': d.configId1[0],\n 'configId2': d.configId2[0]\n };\n };\n\n }\n\nIf data sections are defined such that a user can input multiple fields (for\nexample, if there were two dimensions defined for a sankey diagram), then the\ntransform will depend on your use case, as the data format returned by Looker\nStudio will look more like: \n\n\n var dsccObjectTransformData = [\n {'configId1': ['hello', 'there'], 'configId2': [1] },\n {'configId1': ['world', 'globe'], 'configId2': [2] }\n ]\n\n| **Note:** The data transformations needed will depend on your visualization config.\n\n### `dscc.tableTransform`\n\nSome visualization libraries expect an array of arrays.\n\nFor example: \n\n var data = [\n ['hello', 1],\n ['world', 2]\n ];\n\nThe following code shows how to access a row of rows from the\n`dscc.tableTransform` format. \n\n\n function drawViz(data);\n // what the below object looks like\n // {\n // headers: [{\n // \"id\": \"qt_ky8sltutsb\",\n // \"name\": \"dimension\",\n // \"type\": \"TEXT\",\n // \"concept\": \"DIMENSION\",\n // \"configId\": \"configId1\"\n // }, {\n // \"id\": \"qt_m9dtntutsb\",\n // \"name\": \"metric\",\n // \"type\": \"NUMBER\",\n // \"concept\": \"METRIC\",\n // \"configId\": \"configId2\"\n // }],\n // rows: [\n // ['hello', 1],\n // ['world', 2]\n // ];\n // }\n var dsccTableTransformObject = data.tables.DEFAULT;\n\n // accessing the row of rows\n var rowOfRows = dsccTableTransformObject.rows;\n\n // accessing the header row\n var headers = dsccTableTransformObject.headers;\n }\n\n dscc.subscribeToData(drawViz, {transform: tableTransform});"]]