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

112
Visualizações
Changing an object value using onChange event

I am trying to change a chart data using the id given in a select box, for that I used an onChange event:

<label for="patientId">Choose a patient:</label>
 <select name="patient" id="patient" onchange="myFunction(this.value)">
  {% for item in patientId %}
   <option value="{{item}}">
    {{item}}
   </option>
  {% endfor %}
 </select>

<canvas id="weeklyChart"></canvas>

The chart data is a bug object that looks like that:

var myLineChart = new Chart(ctx, {
  type: 'line',
  data: {
    labels: {{ hourArray|safe}},
    datasets: [{
      label: "Value",
      lineTension: 0.3,
      backgroundColor: "rgba(78, 115, 223, 0.05)",
      borderColor: "rgba(78, 115, 223, 1)",
      pointRadius: 3,
      pointBackgroundColor: "rgba(78, 115, 223, 1)",
      pointBorderColor: "rgba(78, 115, 223, 1)",
      pointHoverRadius: 3,
      pointHoverBackgroundColor: "rgba(78, 115, 223, 1)",
      pointHoverBorderColor: "rgba(78, 115, 223, 1)",
      pointHitRadius: 10,
      pointBorderWidth: 2,
      data: {{ glucoseArray|safe}},
    }],
  },
}

In the onChange function I have the following logic:

function myFunction(val) {
  let hourArray = [];
  let glucoseArray = [];
  for (let i = 0; i < deviceList.length; i++) {
    if (deviceList[i].patientId == val) {
      hourArray.push(deviceList[i].hour);
      glucoseArray.push(deviceList[i].glucoseValue);
    }
  }
  myLineChart.data.datasets[0].data = glucoseArray;
  myLineChart.data.labels = hourArray;
  console.log(myLineChart.data.datasets[0].data);
  console.log(myLineChart.data.labels);
}

In the console.log() I have exactly the data I need, but nothing happens on the page. What can I do, so my function will render the right data for the chart.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You have to update the data you add. For that use myLineChart.update()

For change data without animation you can use none as mode

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