How can I hide dataset labels in Chart.js v2?
Asked Answered
L

11

192

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?

Legitimatize answered 13/5, 2016 at 7:52 Comment(0)
T
343

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/

Toilworn answered 13/5, 2016 at 9:4 Comment(8)
works like charm, thanks. by the way, how to change the gradient color of the line chart?Legitimatize
You mean, how to use a gradient as the borderColor / backgroundColor. Just create one on the context and use that when initializing - see jsfiddle.net/g9h6gtvxToilworn
What if I want to use it on one dataset but not on the other oneCinemascope
It works! just one question, where did you find all these options?Beanpole
Unfortunately, this no longer works in the latest v2.9.3. jsfiddle.net/hwtysf64Nobleminded
This works for me in 3.4.1, by setting options.plugins.tooltip.callbacks.label to the function.Callida
this didn't work for me . adding the code within plugins works not in optionsYearning
Note that in Chart JS 3 it's tooltip, not tooltips. This tripped me up for a minute.Fiedling
C
215

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

Coonhound answered 12/4, 2021 at 9:29 Comment(1)
js options: { legend: { display: false } } Angrist
I
54

add:

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

in the starting of your script code;

Inelegance answered 21/4, 2017 at 10:59 Comment(3)
Simple and works perfectly. I noticed the accepted answer was breaking some things.Ogre
In React, I did this import { Chart } from "react-chartjs-2"; Chart.defaults.global.legend.display = false; Uprear
Doesn't this disable it for all charts, in all pages where Chart is being used?Hinterland
S
35

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
   }
  }
 }
Spectacled answered 22/4, 2021 at 18:10 Comment(1)
This also work in v4.2.1Claudianus
C
19

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>
Chee answered 7/9, 2021 at 13:8 Comment(2)
This does not remove the axis labels but the entire axes togehter with the gridHeedful
Oh dang, you're right @LeeLenalee. I should probably rephrase. Thanks for the comment :)Chee
S
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

Skein answered 1/1, 2018 at 23:39 Comment(0)
S
12

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;``
Scion answered 24/5, 2019 at 13:37 Comment(2)
options: { legend: { display: false, }} It helps to write which block to put it in (this is basically the problem with the documentation)Peary
For v3.0.0+: options: { plugins: { legend: { display: false } } }Candelaria
A
9
new Chart('idName', {
      type: 'typeChar',
      data: data,
      options: {
        legend: {
          display: false
        }
      }
    });
Adamski answered 4/10, 2019 at 17:54 Comment(1)
Hi, welcome to SO. consider adding a brief explanation next to the codeRibbon
P
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
            }
        }
Petrel answered 18/1, 2021 at 11:58 Comment(0)
C
1

⬆ v3.0.0 and after

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

⬇ Before v3.0.0

...
options: {
  legend: {
    display: false
  }
}
Candelaria answered 13/5, 2016 at 7:52 Comment(0)
B
0

for me also needed to remove x-axis and y-axis labels. for that use:

options: {
  scales: {
    x: {
      display: false, // Hide x-axis label
    },
    y: {
      display: false, // Hide y-axis label
    }
  }
}
Bipropellant answered 31/1 at 14:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.