185

I have the following code to create a graph using Chart.js v2.1.3:

var ctx = $('#gold_chart');
var goldChart = new Chart(ctx, {
    type: 'line',
    data: {
        labels: dates,
        datasets: [{
            label: 'I want to remove this Label',
            data: prices,
            pointRadius: 0,
            borderWidth: 1
        }]
    }
});

The code looks simple, but I cannot remove the label from the graph. I tried a lot of solutions I found online, but most of them use Chart.js v1.x.

How can I remove the dataset labels?

isherwood
  • 58,414
  • 16
  • 114
  • 157
Raptor
  • 53,206
  • 45
  • 230
  • 366

9 Answers9

340

Just set the label and tooltip options like so

...
options: {
    legend: {
        display: false
    },
    tooltips: {
        callbacks: {
           label: function(tooltipItem) {
                  return tooltipItem.yLabel;
           }
        }
    }
}

Fiddle - http://jsfiddle.net/g19220r6/

potatopeelings
  • 40,709
  • 7
  • 95
  • 119
192

As of 2021, the namespace has changed from options.legend to options.plugins.legend. This simple code worked for me -

options: {
  plugins: {
    legend: {
      display: false
    }
  }
}

Documentation reference

Zakariya Qureshi
  • 2,084
  • 2
  • 8
  • 9
51

add:

Chart.defaults.global.legend.display = false;

in the starting of your script code;

Rochan
  • 1,412
  • 1
  • 14
  • 17
29

New Solution ChartJS v3.1.1

The above solution is correct for previous versions of chart js prior to v3.1 for v3.1.1 use the following

 ...
 options: {
  plugins:{
   legend: {
    display: false
   }
  }
 }
Rex. A
  • 379
  • 4
  • 7
16

For those who want to remove the actual axis labels and not just the legend in 2021 (Chart.js v.3.5.1). Note: this also removes the axes.

const chartWrap = document.querySelector('.chart-wrap')
const canvas = document.createElement('canvas')

chartWrap.appendChild(canvas)
const ctx = canvas.getContext('2d')

const myChart = new Chart(ctx, {
    type: 'line',
    data: {
        labels: ['Your', 'axis', 'labels'],
        datasets: [
            {
                label: 'Your legend label',
                data: [1, 3, 2],
                backgroundColor: '#54ACEF'
            }
        ]
    },
    options: {
        maintainAspectRatio: false,
        plugins: {
            legend: false // Hide legend
        },
        scales: {
            y: {
                display: false // Hide Y axis labels
            },
            x: {
                display: false // Hide X axis labels
            }
        }   
    }
})
<div class="chart-wrap" style="width: 200px; height: 100px;"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.1/chart.min.js" integrity="sha512-Wt1bJGtlnMtGP0dqNFH1xlkLBNpEodaiQ8ZN5JLA5wpc1sUlk/O5uuOMNgvzddzkpvZ9GLyYNa8w2s7rqiTk5Q==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
Ludolfyn
  • 1,806
  • 14
  • 20
13

You can also put the tooltip onto one line by removing the "title":

this.chart = new Chart(ctx, {
    type: this.props.horizontal ? 'horizontalBar' : 'bar',
    options: {
        legend: {
            display: false,
        },
        tooltips: {
            callbacks: {
                label: tooltipItem => `${tooltipItem.yLabel}: ${tooltipItem.xLabel}`, 
                title: () => null,
            }
        },
    },
});

enter image description here

mpen
  • 272,448
  • 266
  • 850
  • 1,236
13

It's just as simple as adding this:

legend: {
    display: false,
}

Or if you want you could use this other option which should also work:

Chart.defaults.global.legend.display = false;``
Jasper
  • 11,590
  • 6
  • 38
  • 55
  • 2
    options: { legend: { display: false, }} It helps to write which block to put it in (this is basically the problem with the documentation) – Normajean Jul 31 '20 at 20:08
8
new Chart('idName', {
      type: 'typeChar',
      data: data,
      options: {
        legend: {
          display: false
        }
      }
    });
jurcola
  • 359
  • 4
  • 7
3

Replace options with this snippet, will fix for Vanilla JavaScript Developers

options: {
            title: {
                text: 'Hello',
                display: true
            },
            scales: {
                xAxes: [{
                    ticks: {
                        display: false
                    }
                }]
            },
            legend: {
                display: false
            }
        }
Sajeel Hassan
  • 314
  • 3
  • 8