plot two functions in the same graph in javascript

index.tsx
// Define the functions to plot
function f1(x) {
    return Math.sin(x);
}

function f2(x) {
    return Math.cos(x);
}

// Create data points for the functions
var data1 = [];
var data2 = [];
for (var x = -2 * Math.PI; x < 2 * Math.PI; x += 0.1) {
    data1.push({ x: x, y: f1(x) });
    data2.push({ x: x, y: f2(x) });
}

// Plot the functions in the same graph
var chart = new Chart(ctx, {
    type: 'line',
    data: {
        datasets: [{
            label: 'sin(x)',
            data: data1,
            borderColor: 'red'
        }, {
            label: 'cos(x)',
            data: data2,
            borderColor: 'blue'
        }]
    },
    options: {
        scales: {
            x: {
                type: 'linear'
            },
            y: {
                type: 'linear'
            }
        }
    }
});
823 chars
43 lines

related categories

gistlibby LogSnag