Loading...
More JavaScript Posts
const rectSize = 15; // Size of the color boxconst spacing = 5; // Space between legend itemslet legend = canvas.append('g').attr('class','chart-legend').attr('transform', `translate(${0}, ${height + 3})`);// Sample data for the legendconst 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 + 15var bbox = legendItem.node().getBBox()var width = bbox.widthconsole.log(width)// Add rectangle for colorlegendItem.append('rect').attr('width', rectSize).attr('height', rectSize).attr('fill', d.color);// Add labellegendItem.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 = 12var legendLength = 0legend.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 yetlet startingPosition = 0if (i > 0) {startingPosition = legendLength}legendLength += width + legendItems_spacing// return `translate(${xx - (width + 30)},0$)`return `translate(${startingPosition},0)`})})
// `date` is a `Date` objectconst formatYmd = date => date.toISOString().slice(0, 10);// ExampleformatYmd(new Date()); // 2020-05-06
const deepMerge = (a, b, fn) =>[...new Set([...Object.keys(a), ...Object.keys(b)])].reduce((acc, key) => ({ ...acc, [key]: fn(key, a[key], b[key]) }),{});deepMerge({ a: true, b: { c: [1, 2, 3] } },{ a: false, b: { d: [1, 2, 3] } },(key, a, b) => (key === 'a' ? a && b : Object.assign({}, a, b)));// { a: false, b: { c: [ 1, 2, 3 ], d: [ 1, 2, 3 ] } }
//QM Helper by Leif Messinger//Groups numbers by number of bits and shows their binary representations.//To be used on x.comconst minterms = prompt("Enter your minterms separated by commas").split(",").map(x => parseInt(x.trim()));const maxNumBits = minterms.reduce(function(a, b) {return Math.max(a, Math.log2(b));});const bitGroups = [];for(let i = 0; i < maxNumBits; ++i){bitGroups.push([]);}for(const minterm of minterms){let outputString = (minterm+" ");//Count the bitslet count = 0;for (var i = maxNumBits; i >= 0; --i) {if((minterm >> i) & 1){++count;outputString += "1";}else{outputString += "0";}}bitGroups[count].push(outputString);}document.body.textContent = "";document.body.style.setProperty("white-space", "pre");for(const group of bitGroups){for(const outputString of group){document.body.textContent += outputString + "\r\n";}}
/*** @param {number[]} nums* @return {boolean}*/var containsDuplicate = function(nums) {return new Set(nums).size !== nums.length};
const permutations = arr => {if (arr.length <= 2) return arr.length === 2 ? [arr, [arr[1], arr[0]]] : arr;return arr.reduce((acc, item, i) =>acc.concat(permutations([...arr.slice(0, i), ...arr.slice(i + 1)]).map(val => [item,...val,])),[]);};permutations([1, 33, 5]);// [ [1, 33, 5], [1, 5, 33], [33, 1, 5], [33, 5, 1], [5, 1, 33], [5, 33, 1] ]