<?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 Power BI Custom Visuals - Gradient Fill id not getting refreshed in Custom Visuals Development Discussion</title>
    <link>https://community.fabric.microsoft.com/t5/Custom-Visuals-Development/Power-BI-Custom-Visuals-Gradient-Fill-id-not-getting-refreshed/m-p/1232282#M3275</link>
    <description>&lt;P&gt;Hello everyone,&lt;/P&gt;&lt;P&gt;I have just started learning to create Power BI custom visuals and I have created my first custom visual i.e. Circle Card by referring the Microsoft tutorial.&lt;/P&gt;&lt;P&gt;Now, I want to modify this little bit and want to show a percentage based on Actual Value and Target Value and fill the circle according to the percentage i.e. if the percentage is 85% then fill 85% circle with a different color. I have managed to do this using the below sort of code in the Update method of the visual lifecycle.&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;var gradient = this.container.append("defs").append("linearGradient").attr("id", "gradient")
            .attr("x1", "0%").attr("x2", "0%").attr("y1", "100%").attr("y2", "0%");

        gradient.append("stop").attr("offset", + fillPercent + "%").style("stop-color", "red");

        gradient.append("stop").attr("offset", fillPercent + "%").style("stop-color", "white");

        this.circle
            .style("fill", function () {
                return "url(#gradient)";
            })
            .style("fill-opacity", 1)
            .style("stroke", "black")
            .style("stroke-width", this.visualSettings.gauge.circleThickness)
            .attr("r", radius)
            .attr("cx", width / 2)
            .attr("cy", height / 2);&lt;/LI-CODE&gt;&lt;P&gt;The problem is that the gradient fill is not getting refreshed based on the percentage I need to press the reload visual button. (See below)&lt;/P&gt;&lt;P&gt;&lt;img /&gt;I have attached my visual.ts file for the reference.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please let me know what's wrong here?&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://1drv.ms/u/s!AitlNHhhJb42gXS6gNiYlf4sEMCH?e=JUrK9l" target="_blank" rel="noopener"&gt;Please click here to open visual.ts&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 18 Jul 2020 07:24:16 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2020-07-18T07:24:16Z</dc:date>
    <item>
      <title>Power BI Custom Visuals - Gradient Fill id not getting refreshed</title>
      <link>https://community.fabric.microsoft.com/t5/Custom-Visuals-Development/Power-BI-Custom-Visuals-Gradient-Fill-id-not-getting-refreshed/m-p/1232282#M3275</link>
      <description>&lt;P&gt;Hello everyone,&lt;/P&gt;&lt;P&gt;I have just started learning to create Power BI custom visuals and I have created my first custom visual i.e. Circle Card by referring the Microsoft tutorial.&lt;/P&gt;&lt;P&gt;Now, I want to modify this little bit and want to show a percentage based on Actual Value and Target Value and fill the circle according to the percentage i.e. if the percentage is 85% then fill 85% circle with a different color. I have managed to do this using the below sort of code in the Update method of the visual lifecycle.&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;var gradient = this.container.append("defs").append("linearGradient").attr("id", "gradient")
            .attr("x1", "0%").attr("x2", "0%").attr("y1", "100%").attr("y2", "0%");

        gradient.append("stop").attr("offset", + fillPercent + "%").style("stop-color", "red");

        gradient.append("stop").attr("offset", fillPercent + "%").style("stop-color", "white");

        this.circle
            .style("fill", function () {
                return "url(#gradient)";
            })
            .style("fill-opacity", 1)
            .style("stroke", "black")
            .style("stroke-width", this.visualSettings.gauge.circleThickness)
            .attr("r", radius)
            .attr("cx", width / 2)
            .attr("cy", height / 2);&lt;/LI-CODE&gt;&lt;P&gt;The problem is that the gradient fill is not getting refreshed based on the percentage I need to press the reload visual button. (See below)&lt;/P&gt;&lt;P&gt;&lt;img /&gt;I have attached my visual.ts file for the reference.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please let me know what's wrong here?&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://1drv.ms/u/s!AitlNHhhJb42gXS6gNiYlf4sEMCH?e=JUrK9l" target="_blank" rel="noopener"&gt;Please click here to open visual.ts&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jul 2020 07:24:16 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Custom-Visuals-Development/Power-BI-Custom-Visuals-Gradient-Fill-id-not-getting-refreshed/m-p/1232282#M3275</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-07-18T07:24:16Z</dc:date>
    </item>
    <item>
      <title>Re: Power BI Custom Visuals - Gradient Fill id not getting refreshed</title>
      <link>https://community.fabric.microsoft.com/t5/Custom-Visuals-Development/Power-BI-Custom-Visuals-Gradient-Fill-id-not-getting-refreshed/m-p/1233466#M3278</link>
      <description>&lt;P&gt;Hi Anonymous&lt;/LI-USER&gt;,&lt;/P&gt;
&lt;P&gt;As we only have your &lt;FONT face="courier new,courier"&gt;visual.ts&lt;/FONT&gt; to go on, I can't reproduce fully locally to confirm, the first line in your posted snippet is the suspect:&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;var gradient = this.container.append("defs").append("linearGradient").attr("id", "gradient")
            .attr("x1", "0%").attr("x2", "0%").attr("y1", "100%").attr("y2", "0%");&lt;/LI-CODE&gt;
&lt;P&gt;How this is currently written, will append a &lt;FONT face="courier new,courier"&gt;defs&lt;/FONT&gt; element to your &lt;FONT face="courier new,courier"&gt;container&lt;/FONT&gt; element each time the &lt;FONT face="courier new,courier"&gt;update&lt;/FONT&gt; method runs, rather than replacing it. This is because your &lt;FONT face="courier new,courier"&gt;container&lt;/FONT&gt; property (and it's children) is persisted across the lifecycle of the visual.&lt;/P&gt;
&lt;P&gt;So, when you start your visual and the &lt;FONT face="courier new,courier"&gt;update&lt;/FONT&gt; method runs for the first time the gradient is applied as you expect (akin to&amp;nbsp; your scenario of refreshing the visual to see it in your post). When you update your visual, the original will still be there in the &lt;FONT face="courier new,courier"&gt;g&lt;/FONT&gt; element represented by &lt;FONT face="courier new,courier"&gt;this.container&lt;/FONT&gt;.&lt;/P&gt;
&lt;P&gt;So, here's what it might look like on the first run:&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;Now, if I update the visual in a similar way to you, e.g. changing a slicer value, this happens to your DOM when the &lt;FONT face="courier new,courier"&gt;update&lt;/FONT&gt; method runs:&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;And if we expand this, it looks as follows:&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;Hopefully the above should help illustrate that there are 5 &lt;FONT face="courier new,courier"&gt;defs&lt;/FONT&gt; now attached, and SVG will just be using the first one that was originally added as it's not been cleared down.&lt;/P&gt;
&lt;P&gt;There are more elegant ways to solve this, but for minimal changes to your code, add the following line prior to the &lt;FONT face="courier new,courier"&gt;var gradient...&lt;/FONT&gt; declaration, e.g.:&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;...

this.container.select('defs').remove();
var gradient = this.container.append("defs").append("linearGradient").attr("id", "gradient")
            .attr("x1", "0%").attr("x2", "0%").attr("y1", "100%").attr("y2", "0%");

...&lt;/LI-CODE&gt;
&lt;P&gt;This checks for the presence of a &lt;FONT face="courier new,courier"&gt;defs&lt;/FONT&gt; element and removes it prior to adding the new one, and hopefully this is close to what you're after:&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;Daniel&lt;/P&gt;</description>
      <pubDate>Sun, 19 Jul 2020 20:13:58 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Custom-Visuals-Development/Power-BI-Custom-Visuals-Gradient-Fill-id-not-getting-refreshed/m-p/1233466#M3278</guid>
      <dc:creator>dm-p</dc:creator>
      <dc:date>2020-07-19T20:13:58Z</dc:date>
    </item>
    <item>
      <title>Re: Power BI Custom Visuals - Gradient Fill id not getting refreshed</title>
      <link>https://community.fabric.microsoft.com/t5/Custom-Visuals-Development/Power-BI-Custom-Visuals-Gradient-Fill-id-not-getting-refreshed/m-p/1235219#M3281</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="397" data-lia-user-login="dm-p" class="lia-mention lia-mention-user"&gt;dm-p&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;Thank you so much for your detailed explanation. It resolved my issue.&lt;/P&gt;&lt;P&gt;Thank you once again.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jul 2020 13:23:24 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Custom-Visuals-Development/Power-BI-Custom-Visuals-Gradient-Fill-id-not-getting-refreshed/m-p/1235219#M3281</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-07-20T13:23:24Z</dc:date>
    </item>
    <item>
      <title>Re: Power BI Custom Visuals - Gradient Fill id not getting refreshed</title>
      <link>https://community.fabric.microsoft.com/t5/Custom-Visuals-Development/Power-BI-Custom-Visuals-Gradient-Fill-id-not-getting-refreshed/m-p/1236385#M3282</link>
      <description>&lt;P&gt;No worries! Glad it's working for you &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jul 2020 20:33:33 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Custom-Visuals-Development/Power-BI-Custom-Visuals-Gradient-Fill-id-not-getting-refreshed/m-p/1236385#M3282</guid>
      <dc:creator>dm-p</dc:creator>
      <dc:date>2020-07-20T20:33:33Z</dc:date>
    </item>
  </channel>
</rss>

