score:1

Accepted answer

after you change your data you should update your chart with chartvariable.update().

i made a jsbin which explains you how to use it.

the important function for you is the last in the code, adddatabutton() which gets triggered by a button click. in this function i add new data and update my chart after that.

instead of chartvariable and chart you should use linechart2 in your case.

complete code:

let numberofdatacounter = 0 // current data counter
const numberofdataatbeginning = 4 // data number to start with
const weekdays = ["su", "mo", "du", "we", "th", "fr", "sa"]

function randomnumber(){
  let randomnumber = math.floor(math.random()*100)
  return randomnumber
}

let chartdata = {
  label: [],
  data: []
}

function adddata (){
  chartdata.label.push(weekdays[numberofdatacounter % 7])
  chartdata.data.push(randomnumber())

  numberofdatacounter++
}

// fill chart with data at beginning
while (numberofdataatbeginning>numberofdatacounter) {
  adddata()
}

let data = {
  labels: chartdata.label,
  datasets: [{
    label: "label",
    data: chartdata.data
  }]
}

let chart = new chart(document.getelementbyid("chart"), {
  type: 'line',
  data: data,  
  options: {
    scales: {
      yaxes: [{
        ticks: {
          min: 0,
          max: 100
        }
      }]
    }
  }
});

function adddatabutton(){
  adddata()
  chart.update()
}

Related Query

More Query from same tag