JSFiddle - React, Tailwind, and code Playground
by jeffgw
HTML
<script src="http://code.highcharts.com/highcharts.js"></script>
<div id="container"></div>
CSS
#container {
min-width: 310px;
height: 400px;
margin: 0 auto;
}
JavaScript
var boxMullerRandom = (function () {
var phase = 0,
RAND_MAX,
array,
random,
x1, x2, w, z;
if (crypto && typeof crypto.getRandomValues === 'function') {
RAND_MAX = Math.pow(2, 32) - 1;
array = new Uint32Array(1);
random = function () {
crypto.getRandomValues(array);
return array[0] / RAND_MAX;
};
} else {
random = Math.random;
}
return function () {
if (!phase) {
do {
x1 = 2.0 * random() - 1.0;
x2 = 2.0 * random() - 1.0;
w = x1 * x1 + x2 * x2;
} while (w >= 1.0);
w = Math.sqrt((-3.0 * Math.log(w)) / w);
z = x1 * w;
} else {
z = x2 * w;
}
phase ^= 1;
return z;
}
}());
function randomWalk(steps, randFunc) {
steps = steps >>> 0 || 1000;
if (typeof randFunc !== 'function') {
randFunc = boxMullerRandom;
}
var points = [],
value = 0,
t;
for (t = 0; t < steps; t += 1) {
value += randFunc();
points.push([t, value]);
}
return points;
}
function getYValues(points) {
return points.map(function (point) {
return point[1];
});
}
function generatePlots(howMany) {
howMany = howMany >>> 0 || 10;
var plots = [],
index;
for (index = 0; index < howMany; index += 1) {
plots.push({
name: 'plot' + index,
data: getYValues(randomWalk())
});
}
return plots;
}
$('#container').highcharts({
title: {
text: 'Random Walk',
x: -20 //center
},
subtitle: {
text: 'Random Walk',
x: -20
},
xAxis: {
type: 'linear'
},
yAxis: {
title: {
text: 'Value'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
...