Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

131
Visualizações
how can I programmatically change the label of the Y axis in Chart.JS

In Chart.JS (v3) - how can I programmatically change the label of the Y axis?

myChart.scales.y.text=yaxislabel;

and

myChart.options.scales[0].title.text=yaxislabel;

...doesn't change it - and I get errors such as:

Uncaught TypeError: myChart.options.scales[0] is undefined

...although all of the rest of the datasets and labels update ok with my code below:


My chart is setup with:

    // define chart options
    const options = {
      plugins: { // 'legend' now within object 'plugins {}'
        legend: {
          labels: {
            color: "white",
            // fontSize: 18  // not 'fontSize:' anymore
          }
        }
      },
      maintainAspectRatio: false,
      scales: {
        x: {
          title: {
            display: true,
            text: 'Days',
            color: 'white'
          },
          grid: {
            color: 'rgb(239,96,36,0.5)',
            borderColor: '#ef6024'
          },
          ticks: {
            color: '#ffffff',
            stepSize: 50
          }
        },
        y: {
          title: {
            display: true,
            text: 'Weight (kg)',
            color: '#ffffff'
          },
          grid: {
            color: 'rgb(239,96,36,0.5)',
            borderColor: '#ef6024'
          },
          ticks: {
            color: '#ffffff',
            //stepSize: 50,
            beginAtZero: true
          }
        }
      }
    };
    const config = {
      type: 'line',
      data: data,
      options: options,
    };
    const myChart = new Chart(
      document.getElementById('myChart'),
      config
    );

$yaxislabel = 'kg';
myChart.data.datasets[0].data = Object.values(varsdata);
  myChart.data.labels= Object.values(varslabels);
  myChart.data.datasets[0].label = chartdatalabel;
  myChart.scales.y.text=yaxislabel;               <-- doesn't work
  myChart.options.scales[0].title.text=yaxislabel <-- doesn't work
  myChart.update();


Thanks, Mark
almost 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

This is because you need to put it in the title namespace. So your statement must be: myChart.options.scales.y.title.text=yaxislabel;. For it to show up you also need to make sure it is enabled by calling: myChart.options.scales.y.title.display = true;

almost 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda