Check your eligibility for this 50% exam voucher offer and join us for free live learning sessions to get prepared for Exam DP-700.
Get StartedDon't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.
I'm writing a custom visual with some KPIs and a bar chart. I have created the bar chart by following the below article.
https://tsmatz.wordpress.com/2016/09/27/power-bi-custom-visuals-programming/
The visual is working fine. Now I want a display a tooltip when mouse hovered on the bar chart. I followed the below documentation (couldn't understand most of it) and implemented the same.
https://github.com/Microsoft/PowerBI-visuals/blob/master/Visual/Tooltips.md
My visual doesn't show anything. When I cross-checked the culprit was this line in the constructor.
Solved! Go to Solution.
Thank you for sharing. We're going to look into the issue.
Ignat Vilesov,
Software Engineer
Microsoft Power BI Custom Visuals
@v-viig Thanks for sharing the solution. Below were the changes done in my code.
Did you implement createTooltipServiceWrapper?
Can you share the whole source code of your custom visual?
Ignat Vilesov,
Software Engineer
Microsoft Power BI Custom Visuals
@v-viig I have posted my code. Can you please help me with this? I have a tight deadline to deliver this custom visual to my client.
Copied tooltipServiceWrapper.ts file from https://github.com/Microsoft/PowerBI-visuals-sampleBarChart and pasted in my src folder.
Added "src/tooltipServiceWrapper.ts" to tsconfig.json files part.
Below is my visual.ts. Please let me know if I'm missing anything.
Now, the visual doesn't show anything. If I comment the first line in the constructor the bar graph shows without tooltips.
module powerbi.extensibility.visual { "use strict"; import ValueFormatter = powerbi.extensibility.utils.formatting.valueFormatter; export interface BarData { Indicator: number; Category: string; } export class Visual implements IVisual { private svg: d3.Selection<SVGElement>; private g: d3.Selection<SVGElement>; private host: IVisualHost; private tooltipServiceWrapper: ITooltipServiceWrapper; private bColorPos: string; private bColorNeg: string; constructor(options: VisualConstructorOptions) { this.tooltipServiceWrapper = createTooltipServiceWrapper(this.host.tooltipService, options.element); this.svg = d3.select(options.element).append('svg'); this.g = this.svg.append('g'); this.bColorPos = "#32CD32"; this.bColorNeg = "#FD625E"; } public update(options: VisualUpdateOptions) { this.setObjectProperties(options); this.generateBarChart(options); } public setObjectProperties(options: VisualUpdateOptions) { if (options.dataViews[0].metadata.objects) { if (options.dataViews[0].metadata.objects["bar"] && options.dataViews[0].metadata.objects["bar"]["bColorPos"]) {this.bColorPos = options.dataViews[0].metadata.objects["bar"]["bColorPos"]["solid"]["color"].toString();} if (options.dataViews[0].metadata.objects["bar"] && options.dataViews[0].metadata.objects["bar"]["bColorNeg"]) {this.bColorNeg = options.dataViews[0].metadata.objects["bar"]["bColorNeg"]["solid"]["color"].toString();} } } public generateBarChart(options: VisualUpdateOptions){ var _this = this; var barColor1 = this.bColorPos; var barColor2 = this.bColorNeg; _this.svg.attr({ height: options.viewport.height, width: options.viewport.width }); var gHeight = options.viewport.height * 0.5; var gWidth = options.viewport.width ; _this.g.attr({ height: gHeight, width: gWidth }); _this.g.attr('transform','translate(0,0)'); // convert data format var data = Visual.dataForBarGraph(options); // setup d3 scale var xScale = d3.scale.ordinal() .domain(data.map(function (d) { return d.Category; })) .rangeRoundBands([0, gWidth], 0.1); var yMax = d3.max(data, function (d) { return d.Indicator }); var yScale = d3.scale.linear() .domain([0, yMax]) .range([gHeight, 0]) // remove exsisting axis and bar _this.svg.selectAll('.bar').remove(); // draw bar var shapes = _this.g .append('g') .selectAll('.bar') .data(data); shapes.enter() .append('rect') .attr('class', function (d) { return d.Indicator < 0 ? "bar negative" : "bar positive"; }) .attr('fill', function (d) { return d.Indicator < 0 ? barColor2 : barColor1; }) .attr('x', function (d) { return xScale(d.Category); }) .attr('width', xScale.rangeBand()) .attr('y', function (d) { return d.Indicator < 0 ? yScale(0) : yScale(d.Indicator); }) .attr('height', function (d) { return d.Indicator < 0 ? yScale(d.Indicator) - yScale(0) : gHeight - yScale(d.Indicator); }) this.tooltipServiceWrapper.addTooltip(this.svg.selectAll('.bar'), (tooltipEvent: TooltipEventArgs<number>) => Visual.getTooltipData(tooltipEvent.data), (tooltipEvent: TooltipEventArgs<number>) => null); shapes.exit().remove(); } public static dataForBarGraph(options: VisualUpdateOptions): BarData[] { var resultData: BarData[] = []; var rows = options.dataViews[0].table.rows; for (var i = 0;i < rows.length;i++) { var row = rows[i]; resultData.push({ Indicator: +row[0].toString(), Category: row[2].toString() }); } return resultData; } public enumerateObjectInstances(options: EnumerateVisualObjectInstancesOptions): VisualObjectInstanceEnumeration { let objectName = options.objectName; let objectEnumeration: VisualObjectInstance[] = []; switch (objectName) { case 'bar': objectEnumeration.push({ objectName: objectName, properties: { bColorPos: this.bColorPos, bColorNeg: this.bColorNeg, }, selector: null }); break; }; return objectEnumeration; } private static getTooltipData(value: any): VisualTooltipDataItem[] { return [{ displayName: value.category, value: value.value.toString(), color: value.color }]; } public destroy(): void { //TODO: Perform any cleanup tasks here } } }
this.host is undefined in the constructor. Please take host object form options at contrcutor.
Ignat Vilesov,
Software Engineer
Microsoft Power BI Custom Visuals
This is what I was missing when the visual would not show up.
Thanks @v-viig. Added the line this.host = options.host; in the constructor. The error is gone now but the tool tip is still not working 😞 What else am I missing? I can send you the full code files if needed.
@satishr yes, please share the full source code.
You might share the code here or send it to pbicvsupport@microsoft.com
Ignat Vilesov,
Software Engineer
Microsoft Power BI Custom Visuals
Thanks, @v-viig. I have sent a mail with subject "Need help making tool-tip work".
Thank you for sharing. We're going to look into the issue.
Ignat Vilesov,
Software Engineer
Microsoft Power BI Custom Visuals
@v-viig Thanks for sharing the solution. Below were the changes done in my code.
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.
Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.
User | Count |
---|---|
5 | |
1 | |
1 | |
1 | |
1 |
User | Count |
---|---|
9 | |
3 | |
3 | |
2 | |
2 |