├── style └── visual.less ├── .npmignore ├── .gitignore ├── assets └── icon.png ├── screenshot.png ├── src ├── settings.ts └── visual.ts ├── tsconfig.json ├── pbiviz.json ├── package.json ├── LICENSE ├── capabilities.json ├── README.md └── tslint.json /style/visual.less: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | .tmp 4 | dist -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .tmp 3 | .vscode 4 | dist 5 | 6 | webpack.statistics*.html -------------------------------------------------------------------------------- /assets/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/microsoft/vegalite-for-powerbi/HEAD/assets/icon.png -------------------------------------------------------------------------------- /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/microsoft/vegalite-for-powerbi/HEAD/screenshot.png -------------------------------------------------------------------------------- /src/settings.ts: -------------------------------------------------------------------------------- 1 | import { dataViewObjectsParser } from 'powerbi-visuals-utils-dataviewutils'; 2 | import DataViewObjectsParser = dataViewObjectsParser.DataViewObjectsParser; 3 | 4 | export class VisualSettings extends DataViewObjectsParser { 5 | public rendering = new RenderSettings(); 6 | } 7 | 8 | export class RenderSettings { 9 | public svg = false; 10 | } 11 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "allowJs": false, 4 | "emitDecoratorMetadata": true, 5 | "experimentalDecorators": true, 6 | "target": "es6", 7 | "sourceMap": true, 8 | "outDir": "./.tmp/build/", 9 | "moduleResolution": "node", 10 | "declaration": true, 11 | "lib": [ 12 | "es2015", 13 | "dom" 14 | ] 15 | }, 16 | "files": [ 17 | "./src/visual.ts" 18 | ] 19 | } 20 | -------------------------------------------------------------------------------- /pbiviz.json: -------------------------------------------------------------------------------- 1 | { 2 | "visual": { 3 | "name": "bar", 4 | "displayName": "bar", 5 | "guid": "bar247013B6E51446149DBCA247B9C6BBBA", 6 | "visualClassName": "BarChart", 7 | "version": "1.0.0", 8 | "description": "Sample Vega-Lite Wrapper as a PowerBi Custom Visual", 9 | "supportUrl": "https://github.com/Microsoft/vegalite-for-powerbi/issues", 10 | "gitHubUrl": "https://github.com/Microsoft/vegalite-for-powerbi" 11 | }, 12 | "apiVersion": "2.6.0", 13 | "author": { 14 | "name": "Dominik Moritz", 15 | "email": "t-romor@microsoft.com" 16 | }, 17 | "assets": { 18 | "icon": "assets/icon.png" 19 | }, 20 | "externalJS": [], 21 | "style": "style/visual.less", 22 | "capabilities": "capabilities.json", 23 | "dependencies": null, 24 | "stringResources": [] 25 | } 26 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "visual", 3 | "license": "MIT", 4 | "repository": { 5 | "type": "git", 6 | "url": "https://github.com/Microsoft/vegalite-for-powerbi.git" 7 | }, 8 | "dependencies": { 9 | "@babel/runtime": "7.6.0", 10 | "@babel/runtime-corejs2": "7.6.0", 11 | "core-js": "3.2.1", 12 | "powerbi-visuals-api": "^2.6.2", 13 | "powerbi-visuals-utils-dataviewutils": "2.2.1", 14 | "regenerator-runtime": "^0.13.5", 15 | "vega": "^5.17.3", 16 | "vega-lite": "^4.13.1" 17 | }, 18 | "devDependencies": { 19 | "powerbi-visuals-tools": "^3.1.5", 20 | "ts-loader": "6.1.0", 21 | "tslint": "^5.18.0", 22 | "tslint-microsoft-contrib": "^6.2.0", 23 | "typescript": "3.6.3" 24 | }, 25 | "scripts": { 26 | "start": "pbiviz start", 27 | "build": "pbiviz package", 28 | "pbiviz": "pbiviz", 29 | "lint": "tslint tsconfig.json", 30 | "cert": "pbiviz --install-cert" 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Microsoft Corporation. All rights reserved. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE 22 | -------------------------------------------------------------------------------- /capabilities.json: -------------------------------------------------------------------------------- 1 | { 2 | "dataRoles": [ 3 | { 4 | "displayName": "Category Data", 5 | "name": "category", 6 | "kind": "Grouping" 7 | }, 8 | { 9 | "displayName": "Measure Data", 10 | "name": "measure", 11 | "kind": "Measure" 12 | } 13 | ], 14 | "objects": { 15 | "rendering": { 16 | "displayName": "Rendering", 17 | "properties": { 18 | "svg": { 19 | "displayName": "Use SVG", 20 | "type": { 21 | "bool": true 22 | } 23 | } 24 | } 25 | } 26 | }, 27 | "dataViewMappings": [ 28 | { 29 | "conditions": [ 30 | { 31 | "category": { 32 | "max": 1 33 | }, 34 | "measure": { 35 | "max": 1 36 | } 37 | } 38 | ], 39 | "categorical": { 40 | "categories": { 41 | "for": { 42 | "in": "category" 43 | }, 44 | "dataReductionAlgorithm": { 45 | "top": { 46 | "count": 100 47 | } 48 | } 49 | }, 50 | "values": { 51 | "select": [ 52 | { 53 | "bind": { 54 | "to": "measure" 55 | } 56 | } 57 | ] 58 | } 59 | } 60 | } 61 | ] 62 | } 63 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PowerBi Custom Visual in Vega-Lite 2 | 3 | **NOTE: This project is now archived. We recommend https://github.com/deneb-viz/deneb as a well maintained replacement for this project.** 4 | 5 | This project demonstrates how [Vega](vega.github.io/vega) and [Vega-Lite](vega.github.io/vega-lite) can be used in custom visuals in PowerBI. This custom visual is not intended for daily use. Think of it as a hello world for Vega and Vega-Lite in PowerBI custom visuals. 6 | 7 | ![Screenshot](screenshot.png) 8 | 9 | ## Install 10 | 11 | First install necessary dependencies with `npm install`. 12 | Then run `npm run cert` and follow the instructions at https://github.com/Microsoft/PowerBI-visuals/blob/master/tools/CertificateAddOSX.md. 13 | 14 | ## Run 15 | 16 | Run `npm start` in a terminal. 17 | 18 | The open PowerBI with developer mode enabled and create a developer visual. 19 | 20 | ## Contributing 21 | 22 | This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com. 23 | 24 | When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA. 25 | 26 | This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). 27 | For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or 28 | contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. 29 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "lintOptions": { 3 | "typeCheck": true 4 | }, 5 | "rules": { 6 | "class-name": true, 7 | "comment-format": [ 8 | true, 9 | "check-space" 10 | ], 11 | "indent": [ 12 | true, 13 | "spaces" 14 | ], 15 | "no-duplicate-variable": true, 16 | "no-eval": true, 17 | "no-internal-module": false, 18 | "no-trailing-whitespace": true, 19 | "no-unsafe-finally": true, 20 | "no-var-keyword": true, 21 | "one-line": [ 22 | true, 23 | "check-open-brace", 24 | "check-whitespace" 25 | ], 26 | "quotemark": [ 27 | false, 28 | "double" 29 | ], 30 | "semicolon": [ 31 | true, 32 | "always" 33 | ], 34 | "triple-equals": [ 35 | true, 36 | "allow-null-check" 37 | ], 38 | "typedef-whitespace": [ 39 | true, 40 | { 41 | "call-signature": "nospace", 42 | "index-signature": "nospace", 43 | "parameter": "nospace", 44 | "property-declaration": "nospace", 45 | "variable-declaration": "nospace" 46 | } 47 | ], 48 | "variable-name": [ 49 | true, 50 | "ban-keywords" 51 | ], 52 | "whitespace": [ 53 | true, 54 | "check-branch", 55 | "check-decl", 56 | "check-operator", 57 | "check-separator", 58 | "check-type" 59 | ], 60 | "insecure-random": true, 61 | "no-banned-terms": true, 62 | "no-cookies": true, 63 | "no-delete-expression": true, 64 | "no-disable-auto-sanitization": true, 65 | "no-document-domain": true, 66 | "no-document-write": true, 67 | "no-exec-script": true, 68 | "no-function-constructor-with-string-args": true, 69 | "no-http-string": [true, "http://www.example.com/?.*", "http://www.examples.com/?.*"], 70 | "no-inner-html": true, 71 | "no-octal-literal": true, 72 | "no-reserved-keywords": true, 73 | "no-string-based-set-immediate": true, 74 | "no-string-based-set-interval": true, 75 | "no-string-based-set-timeout": true, 76 | "non-literal-require": true, 77 | "possible-timing-attack": true, 78 | "react-anchor-blank-noopener": true, 79 | "react-iframe-missing-sandbox": true, 80 | "react-no-dangerous-html": true, 81 | "object-literal-key-quotes": [true, "as-needed"] 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/visual.ts: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | import "core-js/stable"; 4 | import "regenerator-runtime/runtime"; 5 | import "./../style/visual.less"; 6 | import powerbi from "powerbi-visuals-api"; 7 | import VisualConstructorOptions = powerbi.extensibility.visual.VisualConstructorOptions; 8 | import VisualUpdateOptions = powerbi.extensibility.visual.VisualUpdateOptions 9 | import IVisual = powerbi.extensibility.visual.IVisual; 10 | import EnumerateVisualObjectInstancesOptions = powerbi.EnumerateVisualObjectInstancesOptions; 11 | import VisualObjectInstance = powerbi.VisualObjectInstance; 12 | import VisualObjectInstanceEnumerationObject = powerbi.VisualObjectInstanceEnumerationObject; 13 | import VisualUpdateType = powerbi.VisualUpdateType; 14 | import PrimitiveValue = powerbi.PrimitiveValue; 15 | import DataView = powerbi.DataView; 16 | import * as vega from 'vega'; 17 | import * as vl from 'vega-lite'; 18 | import { VisualSettings } from "./settings"; 19 | import { Dict } from "vega-lite/build/src/util"; 20 | 21 | interface BarChartDataPoint { 22 | value: PrimitiveValue; 23 | category: string; 24 | } 25 | 26 | function visualTransform(options: VisualUpdateOptions) { 27 | let dataViews = options.dataViews; 28 | 29 | let dataPoints: BarChartDataPoint[] = []; 30 | 31 | if (!dataViews 32 | || !dataViews[0] 33 | || !dataViews[0].categorical 34 | || !dataViews[0].categorical.categories 35 | || !dataViews[0].categorical.categories[0].source 36 | || !dataViews[0].categorical.values) 37 | return null; 38 | 39 | let categorical = dataViews[0].categorical; 40 | let category = categorical.categories[0]; 41 | let dataValue = categorical.values[0]; 42 | 43 | let objects = dataViews[0].metadata.objects; 44 | 45 | let categoryTitle = category.source.displayName; 46 | let valueTitle = dataValue.source.displayName; 47 | 48 | for (let i = 0, len = Math.max(category.values.length, dataValue.values.length); i < len; i++) { 49 | dataPoints.push({ 50 | category: category.values[i] + "", 51 | value: dataValue.values[i] 52 | }); 53 | } 54 | 55 | return { dataPoints, categoryTitle, valueTitle }; 56 | } 57 | 58 | export class BarChart implements IVisual { 59 | private target: HTMLElement; 60 | private settings: VisualSettings; 61 | 62 | private view: any; 63 | 64 | constructor(options: VisualConstructorOptions) { 65 | console.log("Visual constructor", options); 66 | 67 | this.target = options.element; 68 | } 69 | 70 | public update(options: VisualUpdateOptions) { 71 | this.settings = BarChart.parseSettings(options && options.dataViews && options.dataViews[0]); 72 | console.log("Visual update", options); 73 | 74 | if (this.view && (options.type & VisualUpdateType.Resize || options.type & VisualUpdateType.ResizeEnd)) { 75 | this.view.width(options.viewport.width).height(options.viewport.height).run(); 76 | return; 77 | } 78 | 79 | const r = visualTransform(options); 80 | 81 | if (!r) { 82 | this.target.innerHTML = "Need category and measure"; 83 | this.view = null; 84 | return; 85 | } 86 | 87 | const { dataPoints, categoryTitle, valueTitle } = r; 88 | const spec: vl.TopLevelSpec = { 89 | $schema: "https://vega.github.io/schema/vega-lite/v4.json", 90 | width: options.viewport.width, 91 | height: options.viewport.height, 92 | padding: 0, 93 | autosize: { 94 | type: "fit", 95 | contains: "content" 96 | }, 97 | data: { 98 | values: dataPoints as Dict 99 | }, 100 | mark: "bar", 101 | encoding: { 102 | x: { field: "category", type: "ordinal", axis: { title: categoryTitle } }, 103 | y: { field: "value", type: "quantitative", axis: { title: valueTitle } } 104 | } 105 | }; 106 | 107 | const vgSpec = vl.compile(spec).spec; 108 | 109 | const runtime = vega.parse(vgSpec); 110 | 111 | this.view = new vega.View(runtime) 112 | .logLevel(vega.Warn) 113 | .initialize(this.target) 114 | .renderer(this.settings.rendering.svg ? "svg" : "canvas") 115 | .run(); 116 | } 117 | 118 | private static parseSettings(dataView: DataView): VisualSettings { 119 | return VisualSettings.parse(dataView) as VisualSettings; 120 | } 121 | 122 | /** 123 | * This function gets called for each of the objects defined in the capabilities files and allows you to select which of the 124 | * objects and properties you want to expose to the users in the property pane. 125 | */ 126 | public enumerateObjectInstances(options: EnumerateVisualObjectInstancesOptions): VisualObjectInstance[] | VisualObjectInstanceEnumerationObject { 127 | return VisualSettings.enumerateObjectInstances(this.settings || VisualSettings.getDefault(), options); 128 | } 129 | } --------------------------------------------------------------------------------