Creating Polar Area Chart using Canvas

前端 未结 2 2017
醉酒成梦
醉酒成梦 2021-01-15 01:48

I am trying to create Polar Area Chart using canvas here :

http://jsfiddle.net/wm7pwL2w/2/

Code:

var myColor = [\"#ff0\",          


        
2条回答
  •  说谎
    说谎 (楼主)
    2021-01-15 02:27

    You are not supposed to change the values of the Radius 'myRadius', it must be constant (simple math).

    var myColor = ["#ff0","#00f","#002","#003","#004"];
    var myData = [10,30,20,60,40];
    var myRadius = 120;//[120,80,40,70,40]; <=====Changed here
    function getTotal(){
        var myTotal = 0;
        for (var j = 0; j < myData.length; j++) {
            myTotal += (typeof myData[j] == 'number') ? myData[j] : 0;
        }
        return myTotal;
    }
    
    function plotData() {
        var canvas;
        var ctx;
        var lastend = 0;
        var myTotal = getTotal();
    
        canvas = document.getElementById("canvas");
        ctx = canvas.getContext("2d");
        ctx.clearRect(0, 0, canvas.width, canvas.height);
    
        for (var i = 0; i < myData.length; i++) {
            ctx.fillStyle = myColor[i];
            ctx.beginPath();
            ctx.moveTo(200,150);
            ctx.arc(200,150,myRadius,lastend,lastend+(Math.PI*2*(myData[i]/myTotal)),false);//<=====And Changed here
            console.log(myRadius[i]);
            ctx.lineTo(200,150);
            ctx.fill();
            lastend += Math.PI*2*(myData[i]/myTotal);
        }
    }
    
    plotData();
    

    Check http://jsfiddle.net/sameersemna/xhpot31v/

提交回复
热议问题