我们可以自定义canvasjs圆环图吗?

时间:2014-05-22 09:47:36

标签: javascript canvas html5-canvas canvasjs

如何在第一张图片中将此canvasjs甜圈自定义为第二张图片中的另一张?

enter image description here

enter image description here

这是第一张图表的代码:

window.onload = function() {
    chart = new CanvasJS.Chart("chartContainer2", {
        title: {
            text: "Title",
            verticalAlign: "top",
            horizontalAlign: "left"
        },
        data: [{
            type: "doughnut",
            startAngle: 20,
            dataPoints: [{
                y: 30,
                label: "one"
            }, {
                y: 30,
                label: "tow"
            }, {
                y: 50,
                label: " three "
            }]
        }]
    }), chart.render()
};

1 个答案:

答案 0 :(得分:1)

您必须自定义源代码才能获得所需的图表。

CanvasJS拥有Creative Commons + Attribution License,用于非商业用途。但是此许可证不允许对源代码进行任何修改。所以你必须购买商业许可证。我没有他们的商业许可证,所以我无法帮助您进行所需的修改。

由于您无论如何都必须自定义修改CanvasJS,因此创建自己的自定义代码可能更容易; - )

这里有一些示例启动代码,用于计算和绘制圆环图的弧线。添加文本标签和一些lineTo连接器应该很容易。

演示:http://jsfiddle.net/m1erickson/UFPQz/

enter image description here

<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<style>
    body{ background-color: ivory; }
    canvas{border:1px solid red;}
</style>
<script>
$(function(){

    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");

    function DonutChart(cx,cy,radius,arcWidth,values,colors){

        // save incoming chart definition
        this.cx=cx;
        this.cy=cy,
        this.radius=radius;
        this.arcWidth=arcWidth
        this.colors=colors;
        this.arcRadians=[];

        // put spacing between each arc in the chart
        this.leadPadding=Math.PI/45; // 3 degrees lead padding on arcs

        // parallel arrays holding starting/ending angles of each arc
        this.startRadians=[];
        this.endRadians=[];

        // calc sum of all values
        var total=0;
        for(var i=0;i<values.length;i++){ total+=values[i]; }

        // calc starting & ending radian angles for each arc
        var PI2=Math.PI*2;
        var accumRadians=0;
        var starting=0;
        var ending=0;
        for(var i=0;i<values.length;i++){
            starting=ending+this.leadPadding;
            ending=starting+PI2*values[i]/total-this.leadPadding;
            this.startRadians.push(starting);
            this.endRadians.push(ending);
        }


    };
    // draw all the arcs for this chart
    DonutChart.prototype.drawArcs=function(){
        ctx.save();
        for(var i=0;i<this.startRadians.length;i++){
            ctx.beginPath();
            ctx.arc(this.cx,this.cy,this.radius,this.startRadians[i],this.endRadians[i]);
            ctx.lineWidth=this.arcWidth;
            ctx.strokeStyle=this.colors[i];
            ctx.stroke();
        }
        ctx.restore();
    }


    // TEST: draw the arcs for an example donut chart

    var arcs=new DonutChart(150,150,100,10,[250,360,280,250],['red','green','blue','gold']);

    arcs.drawArcs();


}); // end $(function(){});
</script>
</head>
<body>
    <h4>Example arcs for a donut chart</h4>
    <canvas id="canvas" width=300 height=300></canvas>
</body>
</html>