<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Custom Visuals Interactive Legend in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/Custom-Visuals-Interactive-Legend/m-p/1646201#M27686</link>
    <description>&lt;P&gt;Hi Community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to learn to create an Interactive Legend in a Custom Visual. I am referring to the instructions in the following link to be able to do so.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/Microsoft/powerbi-visuals-utils-chartutils/blob/master/docs/api/legend.md#ILegend" target="_blank" rel="noopener"&gt;https://github.com/Microsoft/powerbi-visuals-utils-chartutils/blob/master/docs/api/legend.md#ILegend&lt;/A&gt;&lt;/P&gt;&lt;P&gt;I implementing the steps mentioned, I am not getting the expected results. Right after adding renderLegend() and compiling the visual, I get a blank output instead of the Legend.&lt;/P&gt;&lt;P&gt;I have pasted my code below, Please help me in understanding where I am going wrong.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;"use strict";

import "core-js/stable";
import "./../style/visual.less";
import powerbi from "powerbi-visuals-api";
import IDataViewObject = powerbi.DataViewObject;
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 ISelectionManager = powerbi.extensibility.ISelectionManager;
import DataView = powerbi.DataView;
import IViewport = powerbi.IViewport;
import VisualObjectInstanceEnumeration = powerbi.VisualObjectInstanceEnumeration;
import IVisualHost = powerbi.extensibility.visual.IVisualHost;
import VisualObjectInstanceEnumerationObject = powerbi.VisualObjectInstanceEnumerationObject;
import { legend, legendInterfaces, legendBehavior, legendPosition, legendData, OpacityLegendBehavior } from "powerbi-visuals-utils-chartutils"
import LegendModule = legend;
import ILegend = legendInterfaces.ILegend;
import LegendData = legendInterfaces.LegendData;
import LegendDataPoint = legendInterfaces.LegendDataPoint;
import LegendDataModule = legendData;
import legendProps = legendInterfaces.legendProps;
import createLegend = legend.createLegend;
import LegendPosition = legendInterfaces.LegendPosition;
import { dataViewObjects } from "powerbi-visuals-utils-dataviewutils"
import DataViewObjects = dataViewObjects;
import { ColorHelper } from "powerbi-visuals-utils-colorutils"
import { interactivityUtils, interactivityBaseService } from "powerbi-visuals-utils-interactivityutils"
import IInteractivityService = interactivityBaseService.IInteractivityService;
import { select, scaleBand, scaleLinear, max, min, axisBottom, axisLeft } from "d3";

interface DataPoint {
    category: string;
    value: number;
    color: string;
    identity: powerbi.visuals.ISelectionId;
    highlighted: boolean
};

interface LegDataPoint {
    category: string;
    color: string;
};

interface LegViewModel {
    dataPoints: DataPoint[];
}

interface ViewModel {
    dataPoints: DataPoint[];
    maxValue: number;
    highlights: boolean;
}

export interface LegChartData {
    legendData: LegendData;
}



export class Visual implements IVisual {

    private host: IVisualHost;
    private svg: d3.Selection&amp;lt;SVGElement, any, any, any&amp;gt;;
    private barContainer: d3.Selection&amp;lt;SVGElement, any, any, any&amp;gt;;
    private selectionManager: ISelectionManager;
    private xAxisGroup: d3.Selection&amp;lt;SVGElement, any, any, any&amp;gt;;
    private yAxisGroup: d3.Selection&amp;lt;SVGElement, any, any, any&amp;gt;;
    private legend: ILegend;
    private legendObjectProperties: IDataViewObject;
    private legChartData: LegChartData;
    private viewport: IViewport;



    private settings = {
        axis: {
            x: {
                padding: {
                    default: 50,
                    value: 50
                },
            },
            y: {
                padding: {
                    default: 50,
                    value: 50
                },
            }

        },
        border: {
            top: 10
        }
    }

    constructor(options: VisualConstructorOptions) {

        const element: HTMLElement = options.element;
        this.host = options.host;
        this.svg = select(options.element)
            .append('svg')
            .classed('jorgeBarChart', true);

        this.barContainer = this.svg.append('g')
            .classed('bar-group', true);

        this.xAxisGroup = this.svg.append('g')
            .classed('x-axis', true);

        this.yAxisGroup = this.svg.append('g')
            .classed('y-axis', true);

        this.legend = createLegend(
            element,
            false,
            null,
            true,
            LegendPosition.Top);



        this.selectionManager = this.host.createSelectionManager();
    }

    public update(options: VisualUpdateOptions) {


        let viewModel = this.getViewModel(options);
        let width = options.viewport.width;
        let height = options.viewport.height;

        //this.parseLegendProperties(dataView);
        this.renderLegend();

        this.svg.attr('width', width).attr('height', height);

        let yScale = scaleLinear()
            .domain([0, viewModel.maxValue])
            .range([height - this.settings.axis.x.padding.value, 0 + this.settings.border.top]);

        let yAxis = axisLeft(yScale).tickSize(1)

        this.yAxisGroup.attr('transform', 'translate(' + this.settings.axis.y.padding.value + ',0)').call(yAxis)

        let xScale = scaleBand()
            .domain(viewModel.dataPoints.map(d =&amp;gt; d.category))
            .rangeRound([this.settings.axis.y.padding.value, width])
            .padding(0.1);

        let xAxis = axisBottom(xScale).tickSize(1);

        this.xAxisGroup.attr('transform', 'translate(0, ' + (height - this.settings.axis.x.padding.value) + ')').call(xAxis);

        let bars = this.barContainer
            .selectAll('.bar')
            .data(viewModel.dataPoints);

        bars.enter()
            .append('rect')
            .classed('bar', true)
            .attr('width', xScale.bandwidth())
            .attr('height', d =&amp;gt; height - yScale(d.value) - this.settings.axis.x.padding.value)
            .attr('y', d =&amp;gt; yScale(d.value))
            .attr('x', d =&amp;gt; xScale(d.category))
            .attr('fill', d =&amp;gt; d.color)
            .style('fill-opacity', d =&amp;gt; viewModel.highlights ? d.highlighted ? 1.0 : 0.5 : 1.0)
            .on('click', (d) =&amp;gt; {
                this.selectionManager.select(d.identity, true)
                    .then(ids =&amp;gt; {
                        bars.style(
                            'fill-opacity', ids.length &amp;gt; 0 ? 0.5 : 1.0
                        );
                    });
            });

        bars.exit().remove();

        //this.legend.reset();
        //this.legend.drawLegend({ dataPoints: [] }, (this.viewport));

    }

    public converter(dataView: DataView, options: VisualUpdateOptions): LegChartData {

        let legendData: LegendData = {
            dataPoints: []
        }

        let dv = options.dataViews;

        let view = dv[0].categorical;
        let categories = view.categories[0];
        let values = view.values[0];
        let highlights = values.highlights;

        for (let i = 0, len = Math.max(categories.values.length, values.values.length); i &amp;lt; len; i++) {
            legendData.dataPoints.push(&amp;lt;LegendDataPoint&amp;gt;{
                label: &amp;lt;string&amp;gt;categories.values[i],
                color: this.host.colorPalette.getColor(&amp;lt;string&amp;gt;categories.values[i]).value
            });
        }





        return {
            legendData: legendData
        }
    }

    private getViewModel(options: VisualUpdateOptions): ViewModel {

        let dv = options.dataViews;

        let viewModel: ViewModel = {
            dataPoints: [],
            maxValue: 0,
            highlights: false
        };

        if (!dv
            || !dv[0]
            || !dv[0].categorical
            || !dv[0].categorical.categories
            || !dv[0].categorical.categories[0].source
            || !dv[0].categorical.values)
            return viewModel;

        let view = dv[0].categorical;
        let categories = view.categories[0];
        let values = view.values[0];
        let highlights = values.highlights;

        for (let i = 0, len = Math.max(categories.values.length, values.values.length); i &amp;lt; len; i++) {
            viewModel.dataPoints.push({
                category: &amp;lt;string&amp;gt;categories.values[i],
                value: &amp;lt;number&amp;gt;values.values[i],
                color: this.host.colorPalette.getColor(&amp;lt;string&amp;gt;categories.values[i]).value,
                identity: this.host.createSelectionIdBuilder()
                    .withCategory(categories, i)
                    .createSelectionId(),
                highlighted: highlights ? highlights[i] ? true : false : false
            });
        }

        viewModel.maxValue = max(viewModel.dataPoints, d =&amp;gt; d.value);
        viewModel.highlights = viewModel.dataPoints.filter(d =&amp;gt; d.highlighted).length &amp;gt; 0;

        return viewModel;
    }

    private renderLegend(): void {
        let legChartData: LegChartData = this.legChartData;

        if (!legChartData.legendData) {
            return;
        }



        const { height, width } = this.viewport,
            legendData: LegendData = legChartData.legendData;

        if (this.legendObjectProperties) {
            LegendDataModule.update(legendData, this.legendObjectProperties);

            let position: string = this.legendObjectProperties[legendProps.position] as string;

            if (position) {
                this.legend.changeOrientation(LegendPosition[position]);
            }
            this.legend.drawLegend(legendData, (this.viewport))
        } else {
            this.legend.changeOrientation(LegendPosition.Top);
            this.legend.drawLegend({ dataPoints: [] }, (this.viewport))
        }

        
        //LegendModule.positionChartArea(this.svg, this.legend);
    }







}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 04 Feb 2021 20:15:48 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2021-02-04T20:15:48Z</dc:date>
    <item>
      <title>Custom Visuals Interactive Legend</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Custom-Visuals-Interactive-Legend/m-p/1646201#M27686</link>
      <description>&lt;P&gt;Hi Community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to learn to create an Interactive Legend in a Custom Visual. I am referring to the instructions in the following link to be able to do so.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/Microsoft/powerbi-visuals-utils-chartutils/blob/master/docs/api/legend.md#ILegend" target="_blank" rel="noopener"&gt;https://github.com/Microsoft/powerbi-visuals-utils-chartutils/blob/master/docs/api/legend.md#ILegend&lt;/A&gt;&lt;/P&gt;&lt;P&gt;I implementing the steps mentioned, I am not getting the expected results. Right after adding renderLegend() and compiling the visual, I get a blank output instead of the Legend.&lt;/P&gt;&lt;P&gt;I have pasted my code below, Please help me in understanding where I am going wrong.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;"use strict";

import "core-js/stable";
import "./../style/visual.less";
import powerbi from "powerbi-visuals-api";
import IDataViewObject = powerbi.DataViewObject;
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 ISelectionManager = powerbi.extensibility.ISelectionManager;
import DataView = powerbi.DataView;
import IViewport = powerbi.IViewport;
import VisualObjectInstanceEnumeration = powerbi.VisualObjectInstanceEnumeration;
import IVisualHost = powerbi.extensibility.visual.IVisualHost;
import VisualObjectInstanceEnumerationObject = powerbi.VisualObjectInstanceEnumerationObject;
import { legend, legendInterfaces, legendBehavior, legendPosition, legendData, OpacityLegendBehavior } from "powerbi-visuals-utils-chartutils"
import LegendModule = legend;
import ILegend = legendInterfaces.ILegend;
import LegendData = legendInterfaces.LegendData;
import LegendDataPoint = legendInterfaces.LegendDataPoint;
import LegendDataModule = legendData;
import legendProps = legendInterfaces.legendProps;
import createLegend = legend.createLegend;
import LegendPosition = legendInterfaces.LegendPosition;
import { dataViewObjects } from "powerbi-visuals-utils-dataviewutils"
import DataViewObjects = dataViewObjects;
import { ColorHelper } from "powerbi-visuals-utils-colorutils"
import { interactivityUtils, interactivityBaseService } from "powerbi-visuals-utils-interactivityutils"
import IInteractivityService = interactivityBaseService.IInteractivityService;
import { select, scaleBand, scaleLinear, max, min, axisBottom, axisLeft } from "d3";

interface DataPoint {
    category: string;
    value: number;
    color: string;
    identity: powerbi.visuals.ISelectionId;
    highlighted: boolean
};

interface LegDataPoint {
    category: string;
    color: string;
};

interface LegViewModel {
    dataPoints: DataPoint[];
}

interface ViewModel {
    dataPoints: DataPoint[];
    maxValue: number;
    highlights: boolean;
}

export interface LegChartData {
    legendData: LegendData;
}



export class Visual implements IVisual {

    private host: IVisualHost;
    private svg: d3.Selection&amp;lt;SVGElement, any, any, any&amp;gt;;
    private barContainer: d3.Selection&amp;lt;SVGElement, any, any, any&amp;gt;;
    private selectionManager: ISelectionManager;
    private xAxisGroup: d3.Selection&amp;lt;SVGElement, any, any, any&amp;gt;;
    private yAxisGroup: d3.Selection&amp;lt;SVGElement, any, any, any&amp;gt;;
    private legend: ILegend;
    private legendObjectProperties: IDataViewObject;
    private legChartData: LegChartData;
    private viewport: IViewport;



    private settings = {
        axis: {
            x: {
                padding: {
                    default: 50,
                    value: 50
                },
            },
            y: {
                padding: {
                    default: 50,
                    value: 50
                },
            }

        },
        border: {
            top: 10
        }
    }

    constructor(options: VisualConstructorOptions) {

        const element: HTMLElement = options.element;
        this.host = options.host;
        this.svg = select(options.element)
            .append('svg')
            .classed('jorgeBarChart', true);

        this.barContainer = this.svg.append('g')
            .classed('bar-group', true);

        this.xAxisGroup = this.svg.append('g')
            .classed('x-axis', true);

        this.yAxisGroup = this.svg.append('g')
            .classed('y-axis', true);

        this.legend = createLegend(
            element,
            false,
            null,
            true,
            LegendPosition.Top);



        this.selectionManager = this.host.createSelectionManager();
    }

    public update(options: VisualUpdateOptions) {


        let viewModel = this.getViewModel(options);
        let width = options.viewport.width;
        let height = options.viewport.height;

        //this.parseLegendProperties(dataView);
        this.renderLegend();

        this.svg.attr('width', width).attr('height', height);

        let yScale = scaleLinear()
            .domain([0, viewModel.maxValue])
            .range([height - this.settings.axis.x.padding.value, 0 + this.settings.border.top]);

        let yAxis = axisLeft(yScale).tickSize(1)

        this.yAxisGroup.attr('transform', 'translate(' + this.settings.axis.y.padding.value + ',0)').call(yAxis)

        let xScale = scaleBand()
            .domain(viewModel.dataPoints.map(d =&amp;gt; d.category))
            .rangeRound([this.settings.axis.y.padding.value, width])
            .padding(0.1);

        let xAxis = axisBottom(xScale).tickSize(1);

        this.xAxisGroup.attr('transform', 'translate(0, ' + (height - this.settings.axis.x.padding.value) + ')').call(xAxis);

        let bars = this.barContainer
            .selectAll('.bar')
            .data(viewModel.dataPoints);

        bars.enter()
            .append('rect')
            .classed('bar', true)
            .attr('width', xScale.bandwidth())
            .attr('height', d =&amp;gt; height - yScale(d.value) - this.settings.axis.x.padding.value)
            .attr('y', d =&amp;gt; yScale(d.value))
            .attr('x', d =&amp;gt; xScale(d.category))
            .attr('fill', d =&amp;gt; d.color)
            .style('fill-opacity', d =&amp;gt; viewModel.highlights ? d.highlighted ? 1.0 : 0.5 : 1.0)
            .on('click', (d) =&amp;gt; {
                this.selectionManager.select(d.identity, true)
                    .then(ids =&amp;gt; {
                        bars.style(
                            'fill-opacity', ids.length &amp;gt; 0 ? 0.5 : 1.0
                        );
                    });
            });

        bars.exit().remove();

        //this.legend.reset();
        //this.legend.drawLegend({ dataPoints: [] }, (this.viewport));

    }

    public converter(dataView: DataView, options: VisualUpdateOptions): LegChartData {

        let legendData: LegendData = {
            dataPoints: []
        }

        let dv = options.dataViews;

        let view = dv[0].categorical;
        let categories = view.categories[0];
        let values = view.values[0];
        let highlights = values.highlights;

        for (let i = 0, len = Math.max(categories.values.length, values.values.length); i &amp;lt; len; i++) {
            legendData.dataPoints.push(&amp;lt;LegendDataPoint&amp;gt;{
                label: &amp;lt;string&amp;gt;categories.values[i],
                color: this.host.colorPalette.getColor(&amp;lt;string&amp;gt;categories.values[i]).value
            });
        }





        return {
            legendData: legendData
        }
    }

    private getViewModel(options: VisualUpdateOptions): ViewModel {

        let dv = options.dataViews;

        let viewModel: ViewModel = {
            dataPoints: [],
            maxValue: 0,
            highlights: false
        };

        if (!dv
            || !dv[0]
            || !dv[0].categorical
            || !dv[0].categorical.categories
            || !dv[0].categorical.categories[0].source
            || !dv[0].categorical.values)
            return viewModel;

        let view = dv[0].categorical;
        let categories = view.categories[0];
        let values = view.values[0];
        let highlights = values.highlights;

        for (let i = 0, len = Math.max(categories.values.length, values.values.length); i &amp;lt; len; i++) {
            viewModel.dataPoints.push({
                category: &amp;lt;string&amp;gt;categories.values[i],
                value: &amp;lt;number&amp;gt;values.values[i],
                color: this.host.colorPalette.getColor(&amp;lt;string&amp;gt;categories.values[i]).value,
                identity: this.host.createSelectionIdBuilder()
                    .withCategory(categories, i)
                    .createSelectionId(),
                highlighted: highlights ? highlights[i] ? true : false : false
            });
        }

        viewModel.maxValue = max(viewModel.dataPoints, d =&amp;gt; d.value);
        viewModel.highlights = viewModel.dataPoints.filter(d =&amp;gt; d.highlighted).length &amp;gt; 0;

        return viewModel;
    }

    private renderLegend(): void {
        let legChartData: LegChartData = this.legChartData;

        if (!legChartData.legendData) {
            return;
        }



        const { height, width } = this.viewport,
            legendData: LegendData = legChartData.legendData;

        if (this.legendObjectProperties) {
            LegendDataModule.update(legendData, this.legendObjectProperties);

            let position: string = this.legendObjectProperties[legendProps.position] as string;

            if (position) {
                this.legend.changeOrientation(LegendPosition[position]);
            }
            this.legend.drawLegend(legendData, (this.viewport))
        } else {
            this.legend.changeOrientation(LegendPosition.Top);
            this.legend.drawLegend({ dataPoints: [] }, (this.viewport))
        }

        
        //LegendModule.positionChartArea(this.svg, this.legend);
    }







}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Feb 2021 20:15:48 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Custom-Visuals-Interactive-Legend/m-p/1646201#M27686</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2021-02-04T20:15:48Z</dc:date>
    </item>
    <item>
      <title>Re: Custom Visuals Interactive Legend</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Custom-Visuals-Interactive-Legend/m-p/1646254#M27687</link>
      <description>&lt;P&gt;Hi @Anonymous&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;It's a bit challenging to reproduce locally without the capabilities and the data your adding to the visual. Ideally, if you're able to share the project in a GitHub repo or similar, and some sample data w/steps to reproduce, that would be ideal.&lt;/P&gt;
&lt;P&gt;If you're not able to share these, have you tried using a &lt;FONT face="courier new,courier"&gt;&lt;A href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/debugger" target="_self"&gt;debugger&lt;/A&gt;&lt;/FONT&gt; statement in the &lt;FONT face="courier new,courier"&gt;renderLegend&lt;/FONT&gt; method? This will pause execution in your browser and allow you to step through each line and inspect variable values and may help you find something that isn't quite set up as needed. It could also be worth surrounding your code with a &lt;FONT face="courier new,courier"&gt;&lt;A href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch" target="_self"&gt;try...catch&lt;/A&gt;&lt;/FONT&gt; block and log the error output to the console so you can get more details as to why the visual is erroring out.&lt;/P&gt;
&lt;P&gt;Any further context you can provide here will help us to get to the root cause a bit more quickly, if nothing comes to light from your end.&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;Daniel&lt;/P&gt;</description>
      <pubDate>Thu, 04 Feb 2021 20:34:50 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Custom-Visuals-Interactive-Legend/m-p/1646254#M27687</guid>
      <dc:creator>dm-p</dc:creator>
      <dc:date>2021-02-04T20:34:50Z</dc:date>
    </item>
  </channel>
</rss>

