Here is the second implementation on which the lines are not added, but they change:
<html> <head> <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <script type="text/javascript"> google.charts.load('current', {'packages':['corechart']}); google.charts.setOnLoadCallback(drawChart); let chart; let data; let options; function drawChart() { data = google.visualization.arrayToDataTable([ ['Time', 'First', 'Second'], ['00', 4, 4], ['01', 7, 7], ['02', 6, 6], ['03', 5, 6] ]); options = { title: 'Chart online', hAxis: {title: 'Время', titleTextStyle: {color: '#333'}}, vAxis: {minValue: 0} }; chart = new google.visualization.AreaChart(document.getElementById('chart_div')); chart.draw(data, options); } function drawChartRow(){ let row_id = parseInt(document.querySelector("input[name='Time']").value); let row_Time = document.querySelector("input[name='Time']").value; let row_First = parseInt(document.querySelector("input[name='First']").value); let row_Second = parseInt(document.querySelector("input[name='Second']").value); if(row_id >= data.qg.length){ data.addRows([ [row_Time, row_First, row_Second] ]); } else{ data.removeRow(row_id); data.insertRows(row_id, [[row_Time, row_First, row_Second]]); } chart = new google.visualization.AreaChart(document.getElementById('chart_div')); chart.draw(data, options); return false; } </script> </head> <body> <div id="chart_div"></div> <form name="in"> <input name="Time" value="00" /> <input name="First" value="10" /> <input name="Second" value="8" /> <input type="button" value="ok" onclick="drawChartRow()"> </form> </body> </html>
The removeRow insertRows methods are used removeRow . That is, we delete a line and in place of it we add a new one with new values.
The key of the code is that if in the input form you write an existing number, then the row will be updated and if the number is greater than what is on the chart, it will add a new row using the addRows method.
And for an empty graphic, initialize google charts api like this with zero values:
data = google.visualization.arrayToDataTable([ ['Time', 'First', 'Second'], ['00', 0, 0], ['01', 0, 0], ['02', 0, 0], ['03', 0, 0] ]);