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

216
Visualizações
Add dynamically function to Chart.js

I'm trying to add some JavaScript function to Chart.js. I want to add them outside the Options because I want to find a way to add them dynamically when I need them in my Blazor project.

So, I have this JavaScript code where I create a graph.

var ctx = document.getElementById("myChart").getContext("2d");
var myChart = new Chart(ctx, {
  type: "line",
  data: {
    labels: [
      "Sunday",
      "Monday",
      "Tuesday",
      "Wednesday",
      "Thursday",
      "Friday",
      "Saturday"
    ],
    datasets: [
      {
        data: [86, 114, 106, 106, 107, 111, 133],
        label: "Total",
        borderColor: "rgb(62,149,205)",
        backgroundColor: "rgb(62,149,205,0.1)"
      },
      {
        data: [70, 90, 44, 60, 83, 90, 100],
        label: "Accepted",
        borderColor: "rgb(60,186,159)",
        backgroundColor: "rgb(60,186,159,0.1)"
      },
      {
        data: [10, 21, 60, 44, 17, 21, 17],
        label: "Pending",
        borderColor: "rgb(255,165,0)",
        backgroundColor: "rgb(255,165,0,0.1)"
      },
      {
        data: [6, 3, 2, 2, 7, 0, 16],
        label: "Rejected",
        borderColor: "rgb(196,88,80)",
        backgroundColor: "rgb(196,88,80,0.1)"
      }
    ]
  }
});

myChart.data.labels.push('June');

myChart.options.plugins.legend.onClick = function (e, legendItem, legend) {
  console.log('legend click');
  console.log(legendItem);
};

myChart.options.elements.line.backgroundColor = function (ctx) {
  console.log('line backgroundColor');
  return '#000000';
};

myChart.update();
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.6.2/chart.min.js"></script>
<canvas id="myChart" width="500" height="150"></canvas>

At the end of the script, I can add a new label with push, add the onClick for the lagend... but I can't add a function for a specific element for example adding a gradient to a line

const data = {
  labels: labels,
  datasets: [
    {
      label: 'Dataset 1',
      data: myData,
      borderColor: function(context) {
        const chart = context.chart;
        const {ctx, chartArea} = chart;

        if (!chartArea) {
          return;
        }
        return getGradient(ctx, chartArea);
      },
    },
  ]
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The function does work but you are missing config, in V3 by default linecharts dont fill anymore so you will need to set fill to true, also the dataset background property overrides the one set in the elements part so you will need to rename backgroundColor to pointBackgroundColor if you want the points to have that color, then it works fine:

var ctx = document.getElementById("myChart").getContext("2d");
var myChart = new Chart(ctx, {
  type: "line",
  data: {
    labels: [
      "Sunday",
      "Monday",
      "Tuesday",
      "Wednesday",
      "Thursday",
      "Friday",
      "Saturday"
    ],
    datasets: [{
        data: [86, 114, 106, 106, 107, 111, 133],
        label: "Total",
        borderColor: "rgb(62,149,205)",
        pointBackgroundColor: "rgb(62,149,205,0.1)",
        fill: true
      },
      {
        data: [70, 90, 44, 60, 83, 90, 100],
        label: "Accepted",
        borderColor: "rgb(60,186,159)",
        pointBackgroundColor: "rgb(60,186,159,0.1)",
        fill: true
      },
      {
        data: [10, 21, 60, 44, 17, 21, 17],
        label: "Pending",
        borderColor: "rgb(255,165,0)",
        pointBackgroundColor: "rgb(255,165,0,0.1)",
        fill: true
      },
      {
        data: [6, 3, 2, 2, 7, 0, 16],
        label: "Rejected",
        borderColor: "rgb(196,88,80)",
        pointBackgroundColor: "rgb(196,88,80,0.1)",
        fill: true
      }
    ]
  },
});

myChart.data.labels.push('June');

myChart.options.plugins.legend.onClick = function(e, legendItem, legend) {
  console.log('legend click');
  console.log(legendItem);
};

myChart.options.elements.line.backgroundColor = function(ctx) {
  return '#000000';
};

myChart.update();
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.6.2/chart.min.js"></script>
<canvas id="myChart" width="500" height="150"></canvas>

about 4 years ago · Juan Pablo Isaza 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