Char.js - 如何在饼图中默认显示标签

时间:2015-08-03 09:14:57

标签: javascript chart.js

我想在页面加载时默认显示标签。但是在我们进行鼠标悬停之前,默认的chart.js不会显示任何标签。我正在编写如下代码。

var ctx = document.getElementById("chart-area").getContext("2d");
                    window.myPie = new Chart(ctx).Pie(pieData,{                        
                    animationSteps: 100,
                    animationEasing: 'easeInOutQuart',
                    scaleShowLabels : true                  
                    });

这是我的数据。

var pieData = [
                {
                    value: 100,
                    color:"#F7464A",
                    highlight: "#FF5A5E",
                    label: "New",
                },
                {
                    value: 220,
                    color: "#46BFBD",
                    highlight: "#5AD3D1",
                    label: "In Progress"
                }];

默认显示标签的属性是什么?我无法在chart.js

的文档中找到

2 个答案:

答案 0 :(得分:2)

实际上这很简单,你只需要覆盖2个选项属性:

// Draw the tooltips when the animation is completed
onAnimationComplete: function() {
    this.showTooltip(this.segments);
},

// Block the mouse tooltip events so the tooltips
// won't disapear on mouse events
tooltipEvents: []

首先强制在动画完成时绘制工具提示,然后阻止鼠标事件覆盖默认行为,因为您不再需要它(此行为仅显示活动工具提示并隐藏其他工具提示)

查看我的JSFiddle here

答案 1 :(得分:0)

您可以在pieData ex:

中添加它
var pieData = [
                {
                    value : 30,
                    color : "#F38630",
                    label : 'Sleep',
                    labelColor : 'white',
                    labelFontSize : '16'
                },
            ...
        ];
相关问题