如何移动标签'在Chart.js饼上的位置

时间:2016-03-24 20:02:50

标签: javascript css label chart.js labels

我正在使用Chart.js,我试图将饼图区域外的饼图移动到饼图区域外(参见红色X' s):

enter image description here

我的代码现在:

<div class="container" id="pieContainer">
    <h4 class="title">Title</h4>
    <center><canvas id="pie"></canvas></center>
</div>

<script>                                  
    var pieData = [
    {
        value: 39,
        color:"#335478",
        label: "Blue"
    },
    {
        value : 4,
        color : "#7f7f7f",
        label: "Grey"
    },
    {
        value : 57,
        color : "#99cb55",
        label: "Green"
    }
    ];

    var optionsPie = {
        responsive : true,
        tooltipEvents: [],
        showTooltips: true,
        onAnimationComplete: function() {
             this.showTooltip(this.segments, true);
        },
        tooltipTemplate: "<%= label %> - <%= value %>%"
    };
    new Chart(document.getElementById("pie").getContext("2d")).Pie(pieData, optionsPie);
</script>

我不想使用传说,我无法找到移动标签的内置方法。有没有办法在不改变chart.js的情况下做到这一点?什么是实现目标的最佳方式?

4 个答案:

答案 0 :(得分:3)

只需扩展图表即可。如果您的标签是静态的,则更改tooltipPosition方法可能更简单。

预览

enter image description here

<强>脚本

Chart.types.Pie.extend({
    name: "PieAlt",
    initialize: function(data){
        Chart.types.Pie.prototype.initialize.apply(this, arguments);

        var requiredSpace = 0;
        for (var i = 0; i < data.length; i++)
            requiredSpace = Math.max(ctx.measureText(Chart.helpers.template(this.options.tooltipTemplate, data[i])).width, requiredSpace);
        this.outerRadius -= (requiredSpace + 20);
    },
    draw: function(data){
        Chart.types.Pie.prototype.draw.apply(this, arguments);

        var self = this;
        ctx.save();
        ctx.font = Chart.helpers.fontString(self.options.scaleFontSize, self.options.scaleFontStyle, self.options.scaleFontFamily);
                ctx.textBaseline = "middle";
        self.segments.forEach(function (segment) {
            var outerEdge = Chart.Arc.prototype.tooltipPosition.apply({
                x: this.chart.width / 2,
                y: this.chart.height / 2,
                startAngle: segment.startAngle,
                endAngle: segment.endAngle,
                outerRadius: segment.outerRadius * 2 + 20,
                innerRadius: 0
            })

            var normalizedAngle = (segment.startAngle + segment.endAngle) / 2;
            while (normalizedAngle > 2 * Math.PI) {
                normalizedAngle -= (2 * Math.PI)
            }

            if (normalizedAngle < (Math.PI * 0.4) || (normalizedAngle > Math.PI * 1.5))
                ctx.textAlign = "start";
            else if (normalizedAngle > (Math.PI * 0.4) && (normalizedAngle < Math.PI * 0.6)) {
                outerEdge.y += 5;
                ctx.textAlign = "center";
            }
            else if (normalizedAngle > (Math.PI * 1.4) && (normalizedAngle < Math.PI * 1.6)) {
                outerEdge.y - 5;
                ctx.textAlign = "center";
            }
            else
                ctx.textAlign = "end";

            ctx.fillText(Chart.helpers.template(self.options.tooltipTemplate, segment), outerEdge.x, outerEdge.y);
        });

        ctx.restore();
    }
});

然后

new Chart(ctx).PieAlt(data, {
    showTooltips: false
});

小提琴 - http://jsfiddle.net/h8rggkhp/

答案 1 :(得分:1)

看起来您可以控制工具提示的x和y位置:

var myPieChart = new Chart(ctx).Pie(data, {
    customTooltips: function(tooltip) {

        // tooltip will be false if tooltip is not visible or should be hidden
        if (!tooltip) {
            return;
        }

        // Otherwise, tooltip will be an object with all tooltip properties like:

        // tooltip.caretHeight
        // tooltip.caretPadding
        // tooltip.chart
        // tooltip.cornerRadius
        // tooltip.fillColor
        // tooltip.font...
        // tooltip.text
        // tooltip.x
        // tooltip.y
        // etc...

    };
});

看看http://www.chartjs.org/docs/#doughnut-pie-chart-chart-options

答案 2 :(得分:1)

我最近遇到了同样的问题,chartsjs-plugin-label s为我解决了这个问题。

示例:

res = []
lst = [set(i) for i in lst]

for i in lst:
    tmp = []
    for j in i:
        if not any(j in i for i in res):
            tmp.append(j)
    if tmp:
        res.append(sorted(tmp))

print(res)
# [[125.25, 128, 129], [124.25, 127, 130, 131], [124, 125, 126]]

答案 3 :(得分:0)

如果您想要带有这样的标签外的尖线

enter image description here

然后使用chartjs-plugin-piechart-outlabels npm link