I'm trying to create multiple gauges but seem to have trouble. If I don't create a var for each gauge then they don't render. How do I create multiple dynamic variables?
I'm able to create the elements but am struggling to set the val/render the gauges.
for (idx = 0; idx < chartdata.length; idx += 1) {
var opts = {
angle: 0.15, // The span of the gauge arc
lineWidth: 0.44, // The line thickness
radiusScale: 1, // Relative radius
pointer: {
length: 0.6, // // Relative to gauge radius
strokeWidth: 0.035, // The thickness
color: '#000000' // Fill color
},
limitMax: false, // If false, max value increases automatically if value > maxValue
limitMin: false, // If true, the min value of the gauge will be fixed
colorStart: '#6FADCF', // Colors
colorStop: '#8FC0DA', // just experiment with them
strokeColor: '#E0E0E0'
};
var divId = "_gauge" + idx;
var div = document.getElementById('js_chart');
div.innerHTML += '<canvas id="' + divId + '" style="float:left"></canvas>';
}
but if I try methods of creating the varibles in an array or window["gauage" +idx ] they do not.
When adding the below to the For Loop these method only result in the final gauge showing.
window["guage" + idx] = new Gauge(document.getElementById(divId)).setOptions(opts);
window["guage" + idx].set(1) ;
or
var colletion =[]
collection["guage" + idx] = new Gauge(document.getElementById(divId)).setOptions(opts);
collection[idx].set(1)
If I run a function after the loop of :
function DrawStuff(opts){
var target = document.getElementById("_gauge1"); // your canvas element
var gauge = new Gauge(target).setOptions(opts); // create sexy gauge!
gauge.maxValue = 3000; // set max gauge value
gauge.animationSpeed = 32; // set animation speed (32 is default value)
gauge.set(1244); // set actual value
var target2 = document.getElementById("_gauge2"); // your canvas element
var gauge2 = new Gauge(target2).setOptions(opts); // create sexy gauge!
gauge2.maxValue = 3000; // set max gauge value
gauge2.animationSpeed = 32; // set animation speed (32 is default value)
gauge2.set(124); // set actual value
}
The gauges do render. How can I get the same affect of DrawStuff() but with creating the var dynamically as the number of gauges can change?
*I'm usng https://bernii.github.io/gauge.js/