<?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 Legend Development in Custom Visuals Development Discussion</title>
    <link>https://community.fabric.microsoft.com/t5/Custom-Visuals-Development/Custom-Legend-Development/m-p/3484666#M8125</link>
    <description>&lt;P&gt;Hi Developers!&lt;BR /&gt;&lt;BR /&gt;I have written the code for developing a legend for my custom visual. There are many legend items&amp;nbsp; which overflow the chartArea and some are invisible.&lt;BR /&gt;How can i add horizontal scrolling or buttons like other power bi visual?&lt;BR /&gt;Can any one help me on this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;const fontsSize=this.visualSettings.legendFormat.fontSizeLegend.value + "px";
const fontFamily=this.visualSettings.legendFormat.fontFamilyLegend.value;
const fontColor=this.visualSettings.legendFormat.colorLegend.value.value;
const title=datas.categorical.categories[2].source.displayName
createLegend(adjustedChartWidth,title,fontColor,fontFamily,fontsSize,colorScale,this.svg,chartHeight)


function createLegend(adjustedChartWidth: any, title: any, fontColor: any, fontFamily: string, fontsSize: any, colorScale: d3.ScaleOrdinal&amp;lt;string, string&amp;gt;, container: d3.Selection&amp;lt;SVGGraphicsElement, any, any, any&amp;gt;, chartHeight: number) {
  container.select(".legend-scrollable").remove();

  const legendData = colorScale.domain();



  const legendGroup = container
  .append("g")
  .attr("class", "legend-scrollable")
  

  const legendItems = legendGroup
    .selectAll(".legend-item")
    .data(legendData)
    .enter()
    .append("g")
    .attr("class", "legend-item")
    .attr("transform", (_d, i) =&amp;gt; `translate(0, ${i * 25})`)

  const circleRadius = 7;

  legendItems.append("circle")
    .attr("r", circleRadius - 1)
    .attr("cx", circleRadius)
    .attr("cy", circleRadius)
    .attr("fill", colorScale);

  legendItems.append("text")
    .attr("x", circleRadius * 2 + 10)
    .attr("y", circleRadius + 2)
    .attr("dy", "0.08em")
    .style("font-size", fontsSize)
    .style("font-family", fontFamily)
    .style("fill", fontColor)
    .style('text-anchor', "start")
    
    .text((d) =&amp;gt; {
      const availableWidth = Visual.margins.right - adjustedChartWidth + 850; // Adjust as needed

      const tempText = d3.select('body')
        .append('svg')
        .append('text')
        .style('font-size', fontsSize)
        .style('font-family', fontFamily)
        .style('visibility', 'hidden')
        .text(d);

      const textWidth = tempText.node().getBBox().width + 5;
      tempText.remove();

      if (textWidth &amp;gt; availableWidth) {
        const avgCharWidth = textWidth / d.length;
        const maxChars = Math.floor(availableWidth / avgCharWidth);
        return d.substring(0, maxChars - 4) + '...';
      }

      return d;
    })
    .append('title')
    .text(function (d: string) {
      return d;
    });
    

   

    
  //for positioning of the legend container after the last x axis label
    const lastXTick = xScale.range()[xScale.range().length - 1] + Visual.margins.left + 70;
    const legendX = lastXTick + chartWidth - adjustedChartWidth - 80;
    legendGroup.attr("transform", `translate(${legendX}, 0)`);
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Thank You !&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 19 Oct 2023 05:49:09 GMT</pubDate>
    <dc:creator>AakashMarasini</dc:creator>
    <dc:date>2023-10-19T05:49:09Z</dc:date>
    <item>
      <title>Custom Legend Development</title>
      <link>https://community.fabric.microsoft.com/t5/Custom-Visuals-Development/Custom-Legend-Development/m-p/3484666#M8125</link>
      <description>&lt;P&gt;Hi Developers!&lt;BR /&gt;&lt;BR /&gt;I have written the code for developing a legend for my custom visual. There are many legend items&amp;nbsp; which overflow the chartArea and some are invisible.&lt;BR /&gt;How can i add horizontal scrolling or buttons like other power bi visual?&lt;BR /&gt;Can any one help me on this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;const fontsSize=this.visualSettings.legendFormat.fontSizeLegend.value + "px";
const fontFamily=this.visualSettings.legendFormat.fontFamilyLegend.value;
const fontColor=this.visualSettings.legendFormat.colorLegend.value.value;
const title=datas.categorical.categories[2].source.displayName
createLegend(adjustedChartWidth,title,fontColor,fontFamily,fontsSize,colorScale,this.svg,chartHeight)


function createLegend(adjustedChartWidth: any, title: any, fontColor: any, fontFamily: string, fontsSize: any, colorScale: d3.ScaleOrdinal&amp;lt;string, string&amp;gt;, container: d3.Selection&amp;lt;SVGGraphicsElement, any, any, any&amp;gt;, chartHeight: number) {
  container.select(".legend-scrollable").remove();

  const legendData = colorScale.domain();



  const legendGroup = container
  .append("g")
  .attr("class", "legend-scrollable")
  

  const legendItems = legendGroup
    .selectAll(".legend-item")
    .data(legendData)
    .enter()
    .append("g")
    .attr("class", "legend-item")
    .attr("transform", (_d, i) =&amp;gt; `translate(0, ${i * 25})`)

  const circleRadius = 7;

  legendItems.append("circle")
    .attr("r", circleRadius - 1)
    .attr("cx", circleRadius)
    .attr("cy", circleRadius)
    .attr("fill", colorScale);

  legendItems.append("text")
    .attr("x", circleRadius * 2 + 10)
    .attr("y", circleRadius + 2)
    .attr("dy", "0.08em")
    .style("font-size", fontsSize)
    .style("font-family", fontFamily)
    .style("fill", fontColor)
    .style('text-anchor', "start")
    
    .text((d) =&amp;gt; {
      const availableWidth = Visual.margins.right - adjustedChartWidth + 850; // Adjust as needed

      const tempText = d3.select('body')
        .append('svg')
        .append('text')
        .style('font-size', fontsSize)
        .style('font-family', fontFamily)
        .style('visibility', 'hidden')
        .text(d);

      const textWidth = tempText.node().getBBox().width + 5;
      tempText.remove();

      if (textWidth &amp;gt; availableWidth) {
        const avgCharWidth = textWidth / d.length;
        const maxChars = Math.floor(availableWidth / avgCharWidth);
        return d.substring(0, maxChars - 4) + '...';
      }

      return d;
    })
    .append('title')
    .text(function (d: string) {
      return d;
    });
    

   

    
  //for positioning of the legend container after the last x axis label
    const lastXTick = xScale.range()[xScale.range().length - 1] + Visual.margins.left + 70;
    const legendX = lastXTick + chartWidth - adjustedChartWidth - 80;
    legendGroup.attr("transform", `translate(${legendX}, 0)`);
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Thank You !&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2023 05:49:09 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Custom-Visuals-Development/Custom-Legend-Development/m-p/3484666#M8125</guid>
      <dc:creator>AakashMarasini</dc:creator>
      <dc:date>2023-10-19T05:49:09Z</dc:date>
    </item>
  </channel>
</rss>

