Search code examples
javascriptchartshighchartskendo-datavizfusion

JS Gauge with growing arc


Does anyone know of a JavaScript charting library that is capable of drawing a a gauge like this:

Gauge chart

I've already looked at Highcharts, Kendo UI and FusionCharts, but I couldn't find any samples with a non-constant width of the arc...but that could also be because I don't even know what to search for exactly.

I found this post which seems to go in the right direction, but I'd rather not have to draw SVG myself if there's an out of the box solution.


Solution

  • In case anyone else ever needs something like that, I ended up building it myself using D3. Full animated sample is available at http://jsfiddle.net/0288wscf/11/

    var domain = [1, 100];
    
    var angleScale = d3.scale.linear().domain(domain).range([minAngle, maxAngle]);
    var radiusScale = d3.scale.linear().domain(domain).range([radius - minWidth, radius - maxWidth]);
    var colorScale = d3.scale.linear().domain(domain).range([minColor, maxColor]);
    
    var svg = d3.select("body").append("svg")
        .attr("width", 2 * radius)
        .attr("height", 2 * radius);
    
    var gauge = svg.append("g")
        .attr("transform", "translate(" + radius + "," + radius + ")")
    
    var arc = d3.svg.arc()
        .innerRadius(radiusScale)
        .outerRadius(radius)
        .startAngle(angleScale)
        .endAngle(angleScale);
    
    function update(n) {    
        var ticks = gauge.selectAll(".tick").data(d3.range(1, n), function(d) { return d; });
    
        ticks.enter()
             .append("path")
             .attr("class", "tick")
             .attr("stroke", colorScale)            
             .attr("d", arc)
             .attr("stroke-width", tickThickness)
             .attr("opacity", 0)
             .transition()
             .delay(enterDuration)
             .attr("opacity", 1);
    
        ticks.exit()
             .transition()
             .delay(exitDuration)
             .remove();
    }