create a script that makes a x y graph. every time that the script runs adds a new (x,y) point to that graph in javascript

You can use Chart.js library to create an x y graph in JavaScript. Here's a script that generates a new point every time the script runs and adds it to the existing chart:

index.tsx
var chartData = {
  datasets: [{
      label: 'My Dataset',
      data: []
  }]
};

var chartOptions = {
  scales: {
      xAxes: [{
          type: 'linear',
          position: 'bottom'
      }]
  }
};

var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
    type: 'scatter',
    data: chartData,
    options: chartOptions
});

// generate new point
var x = Math.random();
var y = Math.random();
var newPoint = { x: x, y: y };

// add new point to chart
myChart.data.datasets[0].data.push(newPoint);
myChart.update();
562 chars
32 lines

Here, we have initialized a chart using Chart.js library, added an empty dataset, and set the options as required. We generated random x,y values and added them to the dataset of the chart. The last line tells the chart to update with the new data.

Note: You need to include the Chart.js script tag in your HTML file.

related categories

gistlibby LogSnag