Creating a custom visual for PowerBI in NodeJS - \

2020-07-21 05:07发布

问题:

I'm trying to follow this tutorial on creating a custom visual for Power BI : https://docs.microsoft.com/en-us/power-bi/developer/custom-visual-develop-tutorial

The test with the default code works properly when I connect to Power BI Cloud, as shown in the part"Testing the custom visual" step 8, of the tutorial.

The problem is when I try to add the class-level properties in the visual.ts file (after I deleted the code as indicated in the part "Developing the visual elements" step 2 of the tutorial ), I get the error "Cannot find name 'IVisualHost'".

"use strict";

import "core-js/stable";
import "./../style/visual.less";
import powerbi from "powerbi-visuals-api";
import VisualConstructorOptions = powerbi.extensibility.visual.VisualConstructorOptions;
import VisualUpdateOptions = powerbi.extensibility.visual.VisualUpdateOptions;
import IVisual = powerbi.extensibility.visual.IVisual;
import EnumerateVisualObjectInstancesOptions = powerbi.EnumerateVisualObjectInstancesOptions;
import VisualObjectInstance = powerbi.VisualObjectInstance;
import DataView = powerbi.DataView;
import VisualObjectInstanceEnumerationObject = powerbi.VisualObjectInstanceEnumerationObject;

import { VisualSettings } from "./settings";
export class Visual implements IVisual {
    private host: IVisualHost; ------------ the first error is here
    private svg: d3.Selection<SVGElement>;
    private container: d3.Selection<SVGElement>;
    private circle: d3.Selection<SVGElement>;
    private textValue: d3.Selection<SVGElement>;
    private textLabel: d3.Selection<SVGElement>;

    constructor(options: VisualConstructorOptions) {
        this.svg = d3.select(options.element) ----------- the second error is here
            .append('svg')
            .classed('circleCard', true);
        this.container = this.svg.append("g")
            .classed('container', true);
        this.circle = this.container.append("circle")
            .classed('circle', true);
        this.textValue = this.container.append("text")
            .classed("textValue", true);
        this.textLabel = this.container.append("text")
            .classed("textLabel", true);
    }

I also have this other error in the same visual.ts file : 'd3' refers to a UMD global, but the current file is a module. Consider adding an import instead. I imported the library with the commands : "npm i d3@3.5.5 --save" and "npm i @types/d3@3.5"

This is my pbiviz.json file :

{
    "visual": {
        "name": "visual9basic",
        "displayName": "visual9basic",
        "guid": "visual9basic252E75AF09794C8F8CE14414674FBC3E",
        "visualClassName": "Visual",
        "version": "1.0.0",
        "description": "",
        "supportUrl": "",
        "gitHubUrl": ""
    },
    "apiVersion": "2.6.0",
    "author": {
        "name": "",
        "email": ""
    },
    "assets": {
        "icon": "assets/icon.png"
    },
    "externalJS": [
        "node_modules/powerbi-visuals-utils-dataviewutils/lib/index.js",
        "node_modules/d3/d3.min.js"
    ],
    "style": "style/visual.less",
    "capabilities": "capabilities.json",
    "dependencies": null,
    "stringResources": []
}

Here is my tsconfig.json :

{
    "compilerOptions": {
        "allowJs": false,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "target": "es6",
        "sourceMap": true,
        "outDir": "./.tmp/build/",
        "moduleResolution": "node",
        "declaration": true,
        "lib": [
            "es2015",
            "dom"
        ]
    },
    "files": [
        "node_modules/powerbi-visuals-utils-dataviewutils/lib/index.js",
        "./src/visual.ts",
        "./src/settings.ts",
    ]
}

回答1:

In Visual.ts, try adding the following import:

import IVisualHost = powerbi.extensibility.IVisualHost;

It should take care of the missing interface reference. For the d3 reference you can try

import * as d3 from "d3";

Hope this helps. I haven't worked through the tutorial myself yet.



回答2:

What I found out is that after a certain patch of npm, the ".api" folder isn't created any more. Instead, those files are stored under "node_modules/powerbi-visuals-api/". Also, the file "index.d.ts" replaces the former "PowerBI-visuals.d.ts".

But, I still have the 2 errors I described.