fabric.js旋转组合路径

时间:2014-04-23 12:53:44

标签: javascript charts rotation

我已经搜索了几天,并尝试了很多事情来做到这一点。

我想将饼图用作进度饼。我创建了两个结构路径,它绘制饼图,它可以正常工作。 现在我想在中心点旋转路径,但它不起作用。它实际上是一个简单的旋转。主要问题是,旋转点取决于图表的比例。我有多个图表,如果我更改一个,所有其他图表也会发生变化。

我将两个路径合并为一个组,因此每个饼图都是一个包含两个路径的组。

这是我的两张饼图。可选择为true以查看所选内容。

http://i.imgur.com/Q4NLsNf.png

http://i.imgur.com/N8AldM0.png

我希望可选择的矩形在整个圆上均匀分布,以便旋转点正好在中心。我不明白为什么可选区域总是饼图的较小部分。

有人可以帮帮我吗?

这就是我如何计算饼图

for(var i = 0; i < sectorAngleArr.length; i++)
{
    startAngle = endAngle;  
    endAngle = startAngle + sectorAngleArr[i];

    x1 = parseInt(left - (mainProgRad) * Math.sin(Math.PI*startAngle / 180)); 
    y1 = parseInt(top - (mainProgRad) * Math.cos(Math.PI*startAngle / 180)); 

    x2 = parseInt(left - (mainProgRad) * Math.sin(Math.PI * endAngle / 180));
    y2 = parseInt(top - (mainProgRad)  * Math.cos(Math.PI * endAngle / 180));

这就是我画的方式

if(i == 0 && sectorAngleArr[0] <= 180)
    {
        pathString = "M " +  (left) + "," + (top) + " L " + (x1) + "," + (y1) + " A " + mainProgRad + "," + mainProgRad + " 0 0,0 " + (x2) + "," + (y2) + " z";     
        var path0 = new fabric.Path(pathString);

        path0.set(
        {
            fill:" rgba(80, 80, 220, 0.4)",
            stroke:"#0000cc",
            strokeWidth:"1",

        });
    }
    else if(i == 0 && sectorAngleArr[0] > 180)
    {   
        pathString = "M " +  (left) + "," + (top) + " L " + (x1) + "," + (y1) + " A " + mainProgRad + "," + mainProgRad + " 0 1,0 " + (x2) + "," + (y2) + " z";
        var path0 = new fabric.Path(pathString);

        path0.set(
        {
            fill:" rgba(80, 80, 220, 0.4)",
            stroke:"#0000cc",
            strokeWidth:"1",

            });

    }
    else if(i == 1 && sectorAngleArr[1] <= 180)
    {
        pathString = "M " +  (left) + "," + (top) + " L " + (x1) + "," + (y1) + " A " + mainProgRad + "," + mainProgRad + " 0 0,0 " + (x2) + "," + (y2) + " z";
        var path1 = new fabric.Path(pathString);

        path1.set(
        {

            fill:" rgba(220, 80, 80, 0.4)",
            stroke:"#cc00cc",
            strokeWidth:"1",
        });
    }
    else
    {
        pathString = "M " +  (left) + "," + (top) + " L " + (x1) + "," + (y1) + " A " + mainProgRad + "," + mainProgRad + " 0 1,0 " + (x2) + "," + (y2) + " z";
        var path1 = new fabric.Path(pathString);

        path1.set(
        {
            fill:" rgba(220, 80, 80, 0.4)",
            stroke:"#cc00cc",
            strokeWidth:"1",
        });
    }
}   

var progressGroup = new fabric.Group([path0, path1],
        {
            left: left,
            top: top,
            originX: "center",
            originY: "center",
            scaleX: -1,
            selectable:true
        });
        all.add(progressGroup);

我希望你能帮助我!

编辑:向前迈出的一步是使用fabric.Pathgroup而不是fabric.Group ......它会按预期反应更多。但它仍然不起作用:))

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决方法。问题是,电弧被忽略,可选区域位于绘制的2条线附近。所以我创造了更多的馅饼,以获得围绕着圆形饼图和中心的可选区域,现在是中心

你可以在这里看到

http://i.imgur.com/nT7Es3O.png

编辑:在得到一些奇怪的碎片问题后,我把它变得很简单,并在饼图后面画了一个矩形,让它看不见。矩形必须具有绝对位置。现在everthing工作正常! :)