javascript - flot 饼图中的重叠标签

标签 javascript jquery charts flot pie-chart

我使用 jquery flot 制作饼图,但出现重叠问题 饼图 block 非常小时的标签。有没有好的 解决方案?

我的饼图:

series: { 
                pie: { 
                    show: true, 
                    radius: 1, 
                    label: { 
                        show: true, 
                        radius: 5/8, 
                        formatter: function(label, series){ 
                            return '<div style="font-size:12pt;text-  align:center;padding:2px;color:black;margin-left:-80%;margin-  top:-20%;">'+label+'<br/>'+Math.round(series.percent)+'%</div>'; 
                        }, 
                        background: { opacity: 0.5 } 
                    } 
                } 
            }, 
            legend: { 
                show: false 
            }

谢谢,Arshavski Alexander。

最佳答案

Marshall Leggett( link ) 对 Flot 的 Google 代码问题的解决方案:

I've found that it seems common for pie labels to overlap in smaller pie charts making them unreadable, particularly if several slices have small percentage values. This is with the jquery.flot.pie plugin.
Please see attached images. I've worked around this with the addition of an anti-collision routine in the label rendering code. I'm attaching a copy of the revised plugin as well. See lines 472-501, particularly the new functions getPositions() and comparePositions(). This is based in part on Šime Vidas' DOM-element collision detection code. Something like this might be a nice addition to the pie library.

pie labels overlapping pie labels overlapping fixed

长话短说:

  1. 在 jquery.flot.pie.js 和 之后 463 行包含:

    label.css('left', labelLeft);

添加以下代码:

// check to make sure that the label doesn't overlap one of the other labels
var label_pos = getPositions(label);
for(var j=0; j<labels.length; j++)
{
var tmpPos = getPositions(labels[j]);
var horizontalMatch = comparePositions(label_pos[0], tmpPos[0]);
var verticalMatch = comparePositions(label_pos[1], tmpPos[1]);                  
var match = horizontalMatch && verticalMatch;                           
if(match)
{
    var newTop = tmpPos[1][0] - (label.height() +1 );
    label.css('top', newTop);
    labelTop = newTop;
}       
}

function getPositions(box) {
        var $box = $(box);
        var pos = $box.position();
        var width = $box.width();
        var height = $box.height();
        return [ [ pos.left, pos.left + width ], [ pos.top, pos.top + height ] ];
}

function comparePositions(p1, p2) {
        var x1 = p1[0] < p2[0] ? p1 : p2;
        var x2 = p1[0] < p2[0] ? p2 : p1;
        return x1[1] > x2[0] || x1[0] === x2[0] ? true : false;
}
labels.push(label);
  1. 将以下内容添加到 drawLabels() 中,您就完成了:

    var labels = [];

关于javascript - flot 饼图中的重叠标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9496689/

相关文章:

javascript - JQuery 在点击问题上添加类

javascript - 多系列图表——时间(年)区间x轴叠加多年数据

javascript - jQuery 滚动显示隐藏内容

flex3 - 如何在 Flex/Flash ColumnChart 中的 ColumnSeries 之间添加空间?

javascript - 如何在对数刻度上显示等于零的数据?

javascript - 调整窗口大小时 DIV 的相对高度

javascript - 多边形 array.filter() typeError

javascript - 如何使我的 JavaScript 代码在开发者控制台中不可读?

javascript - CSS翻转动画?

javascript - jQuery UI 可拖动和可排序不能像我想要的那样工作