0

I want to show the labels by default when the page loads. But default chart.js will not show any labels until we do mouseover. I am writing code like below.

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

This is my data.

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

What is the property to show the labels by default? I am not able to find under the documentation of chart.js

Mihir
  • 8,696
  • 18
  • 56
  • 87

2 Answers2

2

Actually it is quite easy, you only have to override 2 options properties :

// 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: []

First you force to draw the tooltips when animation is completed, and then you block the mouse events to override the default behaviour as you don't need it anymore (this behaviour is to show only the active tooltip and hide the others)

See my JSFiddle here

bviale
  • 5,245
  • 3
  • 28
  • 48
0

You add it in pieData ex:

var pieData = [
                {
                    value : 30,
                    color : "#F38630",
                    label : 'Sleep',
                    labelColor : 'white',
                    labelFontSize : '16'
                },
            ...
        ];
Margus
  • 19,694
  • 14
  • 55
  • 103