how to calculate the angle between the mid of a clicked donut element and the negative y-axis

后端 未结 1 1630
梦谈多话
梦谈多话 2021-01-26 17:18

Consider the following codesample donut chart using jquery-flot , now as i have added the \'image\' class on click of the donut, i want to dynamically add the degree in the \'im

相关标签:
1条回答
  • 2021-01-26 17:54

    Here you can find my solution to your problem if I got you right.

    $("#pie-placeholder").bind("plotclick", function(event, pos, obj) {
    if (obj) {
        var percentInRads = 0.02;
        var currSegmentInRads = percentInRads * obj.datapoint[0]
        var currSegmentOffset = currSegmentInRads / 2;
        var currSegmentStart = currSegmentOffset >= 0.5 ? -0.5 + currSegmentOffset : 0.5 - currSegmentOffset;
        var total = 0;
        var beforeTotal = 0;
    
        for (var idx = 0; idx < data.length; idx++) {
          var segment = data[idx];
    
          if (idx < obj.seriesIndex) {
            beforeTotal += segment.data;
          }
    
          total += segment.data;
        }
    
        var beforePart = (beforeTotal / total * 100) * percentInRads;
        var chartStartAngle = currSegmentStart - beforePart;
    
        options.series.pie.startAngle = chartStartAngle;
        $.plot($("#pie-placeholder"), data, options);
        console.log(obj.series);
    }
    
    });
    
    0 讨论(0)
提交回复
热议问题