Skip to main content

Timer

Feb 5, 2021LeifMessinger
Loading...

More JavaScript Posts

Horizontal legend (d3.js)

Dec 17, 2024shaiRos

0 likes • 2 views

const rectSize = 15; // Size of the color box
const spacing = 5; // Space between legend items
let legend = canvas.append('g').attr('class','chart-legend')
.attr('transform', `translate(${0}, ${height + 3})`);
// Sample data for the legend
const legendData = [
{ label: 'low', color: '#1f77b4' },
{ label: 'medium', color: '#ff7f0e' },
{ label: 'high', color: '#2ca02c' },
{ label: 'very high', color: '#2ca02c' }
];
legend.selectAll('g')
.data(legendData)
.join('g')
.attr('class', 'legend-item')
.each(function (d, i) {
const legendItem = d3.select(this);
console.log(d.label)
console.log(d.label?.length)
let labelWidth = (d.label.length * 5) + 7 + 15
var bbox = legendItem.node().getBBox()
var width = bbox.width
console.log(width)
// Add rectangle for color
legendItem.append('rect')
.attr('width', rectSize)
.attr('height', rectSize)
.attr('fill', d.color);
// Add label
legendItem.append('text')
.attr('x', rectSize + spacing) // Position text to the right of the rectangle
.attr('y', rectSize / 2) // Align text vertically with the rectangle
.attr('dy', '.35em') // Adjust vertical alignment
.attr('text-anchor', 'start') // Align text to the start
.text(d.label)
.attr('fill', 'black'); // Optional: Text color
// legendItem.attr('transform', `translate(${i * (rectSize + spacing + labelWidth)}, 0)`) // Position each item horizontally
});
const legendItems_spacing = 12
var legendLength = 0
legend.selectAll('.legend-item').each(function(d,i) {
d3.select(this).attr('transform',function() {
var bbox = d3.select(this).node().getBBox()
var width = bbox.width
// if (width == 0) width = (d.text.length * 5) +15 // not in dom yet
let startingPosition = 0
if (i > 0) {
startingPosition = legendLength
}
legendLength += width + legendItems_spacing
// return `translate(${xx - (width + 30)},0$)`
return `translate(${startingPosition},0)`
})
})

Clone an array

Nov 19, 2022CodeCatch

0 likes • 1 view

// `arr` is an array
const clone = arr => arr.slice(0);
// Or
const clone = arr => [...arr];
// Or
const clone = arr => Array.from(arr);
// Or
const clone = arr => arr.map(x => x);
// Or
const clone = arr => JSON.parse(JSON.stringify(arr));
// Or
const clone = arr => arr.concat([]);

List Largest Files Google Drive

Nov 16, 2023LeifMessinger

0 likes • 6 views

function filePath(file){
let folders = file.getParents();
const parents = [];
function makePathString(folderArray){
let path = "";
folderArray.forEach((folder)=>{
path += "/" + folder.getName();
});
return path;
}
while (folders.hasNext()) {
const folder = folders.next(); //This should hopefully remove that folder from the iterator
parents.unshift(folder);
}
return makePathString(parents);
}
function myFunction() {
const myEmail = Session.getEffectiveUser().getEmail();
Logger.log("My email is " + myEmail);
// Log the name of every file in the user's Drive.
var fileIterator = DriveApp.getFiles();
const files = []; //List of [File, size] entries
const filesMaxSize = 10;
while (fileIterator.hasNext()) {
var file = fileIterator.next();
const owner = file.getOwner();
//Only files I own
if((!(owner)) || (!(owner.getEmail)) || owner.getEmail() != myEmail){
continue;
}
const entry = [file, file.getSize()];
function slideUp(arr, index){
//Let's keep sliding it up so we don't have to sort it at the end.
for(let i = index; i > 0; --i){ //Stops at 1
const nextFile = arr[i-1];
if(nextFile[1] > entry[1]){
break;
}else{
//Swap with the next file to slide the file up
const temp = arr[i];
arr[i] = arr[i - 1];
arr[i - 1] = temp;
}
}
}
if(files.length < filesMaxSize){
files.push(entry);
slideUp(files, files.length - 1);
}else{
if(entry[1] > files[files.length - 1][1]){ //If it's bigger than the smallest file in the list.
files[files.length - 1] = entry; //Replace the smallest file
slideUp(files, files.length - 1); //Slide it up
}
}
}
for(let i = 0; i < filesMaxSize; ++i){
const file = files[i][0];
const size = files[i][1];
Logger.log(size + "\t" + filePath(file) + "/" + file.getName() + "\t" + file.getOwner().getName());
}
}

TrackerHeads Acronyms

Aug 30, 2024C S

1 like • 18 views

# Acronyms
## Computer Numerical Control (CNC)
- Typicalldfdfsdffdafdasgfdgfgfdfdafdasfdafda

Subsets of an array

Nov 19, 2022CodeCatch

0 likes • 2 views

const getSubsets = arr => arr.reduce((prev, curr) => prev.concat(prev.map(k => k.concat(curr))), [[]]);
// Examples
getSubsets([1, 2]); // [[], [1], [2], [1, 2]]
getSubsets([1, 2, 3]); // [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]

JWT Authentication

Mar 11, 2021C S

0 likes • 1 view

const jwt = require("jsonwebtoken");
const authToken = (req, res, next) => {
const token = req.headers["x-auth-token"];
try {
req.user = jwt.verify(token, process.env.ACCESS_TOKEN_SECRET);
next();
} catch (err) {
console.log(err.message);
res.status(401).json({ msg: "Error authenticating token" });
}
};
module.exports = authToken;