Ad

Creating Mathematical Charts Using Chart.js JQuery

- 1 answer

Have you got any ideas how to create user-interactive line math charts with chart.js library, something like in wolframalpha? I know that i need to calculate x and y to draw chart correctly and range. It should look like myExample I can do line charts with static data, put in a code but need to know how to draw it after user provide function e.g 2x+5. All ideas and tricks will be very useful :) Thanks

Ad

Answer

Use math.js to parse the string input into a function.

// If "input" is a variable representing the string input for y
var f = math.eval('f(x) = ' + input');

Then compute a range of values for f to pass into chart.js:

// If "min" and "max" are values representing the minimum and max  values
var values = [],
var x = min;
var increment = (max - min) / 10000;
for (var i = 0; i < 10000; i++)
    values[i] = f(x + increment * i);
Ad
source: stackoverflow.com
Ad