Dynamic Labelled Pie Chart


13% 44% 24% 13% 4%
<Note>Data Setup</Note>
<Map not-started>
  <Key value>12</Key>
  <Key color>#D4D4D4</Key>
<Map in-progress>
  <Key value>40</Key>
  <Key color>#EFBC24</Key>
<Map complete>
  <Key value>22</Key>
  <Key color>#00966F</Key>
<Map other>
  <Key value>12</Key>
  <Key color>red</Key>
<Map another>
  <Key value>4</Key>
  <Key color>blue</Key>
<Note>Loopable list with map names</Note>
<List name=pieData>

<Note>Store calculated percentages in dynamically named math variables for later use</Note>
<Math>total = <Loop list=pieData><Field map="{Field /}" name=value /><If not last> + </If></Loop></Math>
<Loop list=pieData><Math>percentage<Get loop=count /> = <Field map="{Field /}" name=value /> / total * 100</Math> </Loop>

<Note>Create the conic gradient</Note>
<Set stop_start>0</Set><Set stop_end>0</Set>
<Set gradient>conic-gradient(<Loop list=pieData><Set stop_end><Math><Get stop_end /> + percentage<Get loop=count /></Math></Set><Field map="{Field /}" name=color /> <Get stop_start />% <If first><Math>percentage<Get loop=count /></Math>%<Else /><Get stop_end />%</If><If not last>, </If><Set stop_start><Get stop_end /></Set></Loop>);</Set>

<div class="pie-chart">
  <Note>Apply the conic gradient as an inline style</Note>
  <div class="pie-chart__chart" style="
  background: {Get gradient /}"></div>
  <Note>Output the labels, which need to be rotated to half the current pie slice + the already output slices, subtracting 90 degrees to account for the different rotation starting point vs the conic gradient start point. The content within the label is rotated the same amount negative so that the lables aren't askew</Note>
  <Set rotationTotal>0</Set>
  <Loop list=pieData>
    <Set dataDeg><Math>( percentage<Get loop=count /> / 100 * 360 )</Math></Set>
    <Set rotation><Math><Get rotationTotal /> + (<Get dataDeg /> * 0.5 ) - 90</Math></Set>
    <Set rotation_neg><Math><Get rotation /> * -1</Math></Set>
      <span class="pie-chart__label pie-chart__label--{Field /}" style="rotate: {Get rotation /}deg;"><span class="pie-chart__label__inner" style="rotate: {Get rotation_neg /}deg;"><Format number decimals=0><Math>percentage<Get loop=count /></Math></Format>%</span></span>
    <Set rotationTotal><Math><Get rotationTotal /> + <Get dataDeg /></Math></Set>