In this example, we created a dynamic graphic that represents the results of an election. Pressing the Simulate button generates a new set of random results and re-renders the graphical representation of the election.
To do this, we clone a button, a progress bar and a label template once, then on every simulation we calculate the percentage each party received, sort the parties by their result, and reposition and resize the cloned elements to match.
Info
Control class names (Button, Label, Slider, ...) are available in a UI Builder script both as types (as used in the IParty interface below) and as constructors (new Button({ ... })).
cloneElement
cloneElement(source, props?) duplicates an existing control together with its children and adds the copy to the root of the UI. Pass an object to set properties on the copy, or a string to name the new control. Cloned controls exist only for the lifetime of the script run; they are not saved with the UI.
/* Define the IParty interface with optional properties for button, progress, and label elements */interfaceIParty{color:string;letter:string;button?:Button;progress?:Label;label?:Label;}// Initialize an array of parties with specific colors and lettersconstparties:IParty[]=[{color:'#9e0142',letter:'A'},{color:'#d53e4f',letter:'B'},{color:'#f46d43',letter:'C'},{color:'#fdae61',letter:'D'},{color:'#fee08b',letter:'E'},{color:'#e6f598',letter:'F'},{color:'#abdda4',letter:'G'},{color:'#66c2a5',letter:'H'},{color:'#3288bd',letter:'I'},{color:'#5e4fa2',letter:'J'},];// Define a constant for the margin between elementsconstMARGIN=5;// Variable to keep track of initialization stateletinitialized=false;// Function to initialize party elementsfunctioninitialize(){// Return if already initializedif(initialized)return;// Set initialized to trueinitialized=true;/* Loop through each party and create button, progress, and label elements */for(leti=0;i<parties.length;i++){constparty=parties[i];// Clone button template and set propertiesconstbutton=cloneElement(partyTemplate,{left:MARGIN,text:party.letter});button.style.backgroundColor=party.color;party.button=button;// Clone progress template and set propertiesconstprogress=cloneElement(progressTemplate);progress.width=0;progress.style.backgroundColor=party.color;party.progress=progress;// Clone label templateconstlabel=cloneElement(labelTemplate);party.label=label;}}// Function to handle the simulation onClick eventsimulate.onClick=()=>{// Initialize party elements if not already initializedinitialize();// Variables to store total value and individual party valuesletsum=0;constvalues:Record<string,number>={};// Generate random values for each party and calculate total sumfor(constpartyofparties){constvalue=Math.random()*100;sum+=value;values[party.letter]=value;}// Find the maximum value among the partiesconstmax=Math.max(...Object.values(values));// Sort parties based on their values in descending orderconstsorted=_.orderBy(parties,party=>values[party.letter],'desc');// Variable to track the top position for each party elementlettop=MARGIN;// Loop through each sorted party and update their elementsfor(constpartyofsorted){constvalue=values[party.letter];constprogress=value/max;constpercent=100*value/sum;// Update button positionparty.button.top=top;// Update progress bar position and widthparty.progress.top=top+progressTemplate.top-partyTemplate.top;party.progress.left=MARGIN+progressTemplate.left-partyTemplate.left;party.progress.width=progress*progressTemplate.width;// Update label position and textparty.label.top=top+labelTemplate.top-partyTemplate.top;party.label.left=MARGIN+labelTemplate.left-partyTemplate.left;party.label.text=`${percent.toFixed(1)} %`;// Increment top position for the next party elementtop+=party.button.height+MARGIN;}}
Tip
The script refers to the controls simulate, partyTemplate, progressTemplate and labelTemplate by name. A UI Builder script is evaluated as a whole, so if any of those controls is missing the script stops at the first unresolved name and none of the handlers below it are registered. Download the ezUI file above, or create controls with exactly those ids, before pasting the script.