1

I have been looking to create a moving gauge and struggled to find an off the shelf solution. I then stumbled across the gauge posted in the below link. Currently it runs random numbers in the chart. I would like it to change value based on an array of numerical values (not %ages) that I have, over a timeframe that I specify. The numbers are currently a simple Excel column.

So the gauge would go through the thousand or so numbers I have across, say, a minute.

I'll be frank, I'm a novice at coding and have limited experience in NetLogo and R. Hence why I'm here asking for pointers.

Any advice would be greatly appreciated. Thank you.

https://codepen.io/leomarquine/pen/xGzMjZ

var size = 150,
    thickness = 60;

var color = d3.scale.linear()
    .domain([0, 50, 100])
    .range(['#db2828', '#fbbd08', '#21ba45']);
// .domain([0, 17, 33, 50, 67, 83, 100])
// .range(['#db4639', '#db7f29', '#d1bf1f', '#92c51b', '#48ba17', '#12ab24', '#0f9f59']);

var arc = d3.svg.arc()
    .innerRadius(size - thickness)
    .outerRadius(size)
    .startAngle(-Math.PI / 2);

var svg = d3.select('#chart').append('svg')
    .attr('width', size * 2)
    .attr('height', size + 20)
    .attr('class', 'gauge');

var chart = svg.append('g')
    .attr('transform', 'translate(' + size + ',' + size + ')')

var background = chart.append('path')
    .datum({
        endAngle: Math.PI / 2
    })
    .attr('class', 'background')
    .attr('d', arc);

var foreground = chart.append('path')
    .datum({
        endAngle: -Math.PI / 2
    })
    .style('fill', '#db2828')
    .attr('d', arc);

var value = svg.append('g')
    .attr('transform', 'translate(' + size + ',' + (size * .9) + ')')
    .append('text')
    .text(0)
    .attr('text-anchor', 'middle')
    .attr('class', 'value');

var scale = svg.append('g')
    .attr('transform', 'translate(' + size + ',' + (size + 15) + ')')
    .attr('class', 'scale');

scale.append('text')
    .text(100)
    .attr('text-anchor', 'middle')
    .attr('x', (size - thickness / 2));

scale.append('text')
    .text(0)
    .attr('text-anchor', 'middle')
    .attr('x', -(size - thickness / 2));

setInterval(function() {
    update(Math.random() * 100);
}, 1500);

function update(v) {
    v = d3.format('.1f')(v);
    foreground.transition()
        .duration(750)
        .style('fill', function() {
            return color(v);
        })
        .call(arcTween, v);

    value.transition()
        .duration(750)
        .call(textTween, v);
}

function arcTween(transition, v) {
    var newAngle = v / 100 * Math.PI - Math.PI / 2;
    transition.attrTween('d', function(d) {
        var interpolate = d3.interpolate(d.endAngle, newAngle);
        return function(t) {
            d.endAngle = interpolate(t);
            return arc(d);
        };
    });
}

function textTween(transition, v) {
    transition.tween('text', function() {
        var interpolate = d3.interpolate(this.innerHTML, v),
            split = (v + '').split('.'),
            round = (split.length > 1) ? Math.pow(10, split[1].length) : 1;
        return function(t) {
            this.innerHTML = d3.format('.1f')(Math.round(interpolate(t) * round) / round) + '<tspan>%</tspan>';
        };
    });
}
  • I'm sympathetic to your plight, but I don't think you're quite ready to ask a question yet. You should try something, fail at it, then come back with what you tried and why it didn't work. That'll give us a better sense of where you're at and what, exactly, you're trying to accomplish. You might even surprise yourself and get it to work! – JDB Mar 27 '19 at 14:53
  • Normally, that's what I do. But in this instance I have utterly no idea where to start. To the point that it would unequivocally rule out the idea. Finding a fair few gauges (i.e. https://www.jqueryscript.net/chart-graph/Customizable-Animated-jQuery-HTML5-Gauge-Meter-Plugin.html). But yeah.. I appreciate the forum isn't for me to get people to just do stuff for me, so fair enough. – Andrew Cooke Mar 27 '19 at 14:58
  • Your code is already using `setInterval` and calling `update()` to change the gauge. You said you have an array ([MDN: Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)). So all that is missing is looping over your array with a built in time delay: https://stackoverflow.com/questions/3583724/how-do-i-add-a-delay-in-a-javascript-loop – JDB Mar 27 '19 at 15:03
  • Thanks - working on it. Managed to edit parts of the chart to remove % and change values. Just got to figure out how to get it to go through the array! – Andrew Cooke Mar 27 '19 at 15:41

0 Answers0