JavaScript preprocessors can help make authoring JavaScript easier and more convenient. For instance, CoffeeScript can help prevent easy-to-make mistakes and offer a cleaner syntax and Babel can bring ECMAScript 6 features to browsers that only support ECMAScript 5.

Any URL's added here will be added as `<script>`

s in order, and run *before* the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.

If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.

You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.

+ add another resource

If active, Pens will autosave every 30 seconds after being saved once.

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Visit your global Editor Settings.

` ````
<input type="checkbox" id="decimate" name="decimate" value="Checked">
<label for="decimate"> Decimate</label><br>
<div id="plot" style="width: 500px; height: 400px"></div>
```

` ````
```

` ````
function decimate(x, y, lowx, highx, width) {
//return immediately if too few points to matter
if (x.length < 2*width) {
return { decx: x, decy: y, decimated: false };
}
//return immediately if points are not sorted
for (let i = 1; i < x.length; i++) {
if (x[i] < x[i - 1]) {
return { decx: x, decy: y, decimated: false };
}
}
//find zoomed subset
let ilow = 0; //lowest index in zoomed range
let ihigh = x.length - 1; //highest index in zoomed range
if (x[0] > lowx || x[x.length - 1] < highx) {}
else if ((lowx !== undefined) && (highx !== undefined)) {
let low = ilow;
let high = ihigh;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (x[mid] === lowx) {
break;
}
if (x[mid] < lowx) {
low = mid + 1;
} else {
high = mid - 1;
}
}
if (low >= high) {
ilow = high;
} else {
ilow= low;
}
low = ilow;
high = ihigh;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (x[mid] === highx) {
break;
}
if (x[mid] < highx) {
low = mid + 1;
} else {
high = mid - 1;
}
}
if (low >= high) {
ihigh = high;
} else {
ihigh = low;
}
}
//return immediately if zoomed subset has too few points
if ((ihigh - ilow) < width) {
return { decx: x.slice(ilow, ihigh + 1), decy: y.slice(ilow, ihigh + 1) };
}
//start with initial real points
let decx = new Array(2*width + 2);
decx[0] = x[ilow];
let decy = new Array(2*width + 2);
decy[0] = y[ilow];
//calculate all points between start and end
let step = (x[ihigh] - x[ilow]) / width;
let currmin = Number.MAX_VALUE;
let currmax = -Number.MAX_VALUE;
let prevbin = x[ilow];
let currBinX = [];
let j = 1; //j tracks which point you're on in the decimated space
let sum = 0;
let counter = 0;
for (let i = ilow; i < ihigh; i++) {
//set points in current bin and move on
if ((x[i] - prevbin) > step) {
let xmean = sum / counter; //use midpoint of current bin for point
sum = 0;
counter = 0;
decx[j] = xmean;
decx[j + 1] = xmean;
decy[j] = (currmin);
decy[j + 1] = (currmax);
currmin = y[i];
currmax = y[i];
prevbin += step;
currBinX = [];
j += 2;
}
if (y[i] > currmax) {
currmax = y[i];
}
if (y[i] < currmin) {
currmin = y[i];
}
sum += x[i];
counter++;
}
decx[2*width] = (prevbin + step/2);
decx[2*width + 1] = (prevbin + step/2);
decy[2*width] = (currmin);
decy[2*width + 1] = (currmax);
//add last real points to end of decimated arrays
decx[2*width + 2] = (x[ihigh - 1]);
decy[2*width + 2] = (y[ihigh - 1]);
return { x: decx, y: decy, decimated: true };
}
function plot() {
let x = [];
let y = [];
let n = 500000;
let freq = Math.random()*10;
for (let i = 0; i < n; i++) {
if ((i > 50000) && (i < 150000) && (i !== 100000)) {
continue;
}
if (i === 100000) {
x.push(i);
y.push(1);
continue;
}
x.push(i);
y.push(Math.random() + Math.sin(freq*i/n) - 0.5);
}
if (document.getElementById('decimate').checked) {
let decimated = decimate(x, y, 0, n - 1, 500);
x = decimated.x;
y = decimated.y;
}
Plotly.react('plot', [{ x: x, y: y, type: 'scattergl' }]).then(
setTimeout(function() {
plot()
}, 0));
}
plot();
```

999px

Also see: Tab Triggers