Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

110
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!