Adding min and max values to gauge chart in d3 v4

非 Y 不嫁゛ 提交于 2020-01-07 09:23:47

问题


I am new to d3 v4, want to build a gauge chart where i can give min, max and actual needle value, lets say i want the needle to be shown at 60 with min as 50 and max as 80 instead of 0 to 100, thanks for this SO question where almost my requirement met except with min and max values. I've tried changing current and previous values including data, doesn't work. Any help is much appreciated. my plunker link


回答1:


I have updated the plunker.

Link: https://plnkr.co/edit/xa2d3YTxnxU5kBIOenLR?p=preview

By default, the range value is 0, 180 degrees. So to add custom range you need to create a linear scale and pass the values to the scale. Since your domain is 50, 80 this is how the scale would look like.

var scale = d3.scaleLinear().domain([50, 80]).range([0,180]);

And your update function would look like this

svg.selectAll( ".needle" ).data( [60] )
    .transition()
    .ease( d3.easeElasticOut )
    .duration( 2000 )
    .attr( "transform", function( d ) {
      console.log(d, scale(d));
        return "translate(200,200) rotate(" + scale(d) + ")"
    });


来源:https://stackoverflow.com/questions/46966889/adding-min-and-max-values-to-gauge-chart-in-d3-v4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!