WebGL vs SVG in JavaScript
Implement WebGL for increased speed, improved interactivity, and the ability to plot even more data!
New to Plotly?
Plotly is a free and open-source graphing library for JavaScript. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.
function gaussianRand() {
var rand = 0;
for (var i = 0; i < 6; i += 1) {
rand += Math.random();
}
return (rand / 6)-0.5;
}
var X = [],
Y = [],
n = 100000,
i;
for (i = 0; i < n; i += 1) {
X.push(gaussianRand());
Y.push(gaussianRand());
}
var data = [{
type: "scattergl",
mode: "markers",
marker: {
line: {
width: 1,
color: '#404040'}
},
x: X,
y: Y
}]
Plotly.newPlot('myDiv', data)
function gaussianRand() {
var rand = 0;
for (var i = 0; i < 6; i += 1) {
rand += Math.random();
}
return (rand / 6)-0.5;
}
var X = [],
Y = [],
n = 1000000,
i;
for (i = 0; i < n; i += 1) {
X.push(gaussianRand());
Y.push(gaussianRand());
}
var data = [{
type: "scattergl",
mode: "markers",
marker: {
color : 'rgb(152, 0, 0)',
line: {
width: 1,
color: 'rgb(0,0,0)'}
},
x: X,
y: Y
}]
Plotly.newPlot('myDiv', data)
function gaussianRand() {
var rand = 0;
for (var i = 0; i < 6; i += 1) {
rand += Math.random();
}
return (rand / 6)-0.5;
}
var start_value = 0,
stop_value = 1,
point_num = 5000,
trace_num = 10;
var curr_value = start_value;
var step = (stop_value - start_value) / (point_num - 1);
var data = [];
for (var j = 0; j < trace_num; j++) {
var X = [],
Y = [];
for (var i = 0; i < point_num; i++) {
X.push(curr_value + (step * i));
Y.push((gaussianRand()*8)+(j*5));
}
data.push({
type: "scattergl",
mode: "line",
x: X,
y: Y
})
}
var layout = {showlegend: false}
Plotly.newPlot('myDiv', data = data, layout = layout)
Multiple WebGL Contexts
Most browsers have a limit of between 8 and 16 WebGL contexts per page. A Plotly WebGL-based figure may use multiple WebGL contexts, but generally you'll be able to render between 4 and 8 figures on one page. If you exceed the browser limit on WebGL contexts, some figures won't render and you'll see an error. In the console in Chrome, for example, you'll see the error: "Too many active WebGL contexts. Oldest context will be lost". If you encounter WebGL context limits when using WebGL-based figures, you can use [Virtual WebGL](https://github.com/greggman/virtual-webgl), which virtualizes a single WebGL context into multiple contexts. To use it, add the following script on your page:
<script src="https://unpkg.com/virtual-webgl@1.0.6/src/virtual-webgl.js"></script>