Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

128
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda