Skip to main content
Loading...

More JavaScript Posts

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)`
            })
            })
//Use at https://mee6.xyz/leaderboard/732262089447702588
//Higher the spammy stat, the more spammy that person is.
//This is because mee doesn't give experience to people who post more comments in a minute.
function statBlock(title, value){
	let elm = document.createElement("div");
	elm.className = "leaderboardPlayerStatBlock";
	let titleElm = document.createElement("div");
	titleElm.className = "leaderboardPlayerStatName";
	titleElm.textContent = title;
	let valueElm = document.createElement("div");
	valueElm.className = "leaderboardPlayerStatValue";
	valueElm.textContent = value;
	elm.appendChild(titleElm);
	elm.appendChild(valueElm);
	elm.remove = function(){
	this.parentElement.removeChild(this);
	}
	return elm;
}
for(let player of Array.from(document.getElementsByClassName("leaderboardPlayer"))){
	if(player.spamminess) player.spamminess.remove();
	let messages = null;
	let experience = null;
	const statBlockArray = Array.from(player.querySelectorAll(".leaderboardPlayerStatBlock"));
	for(let statBlock of statBlockArray){
	const statName = statBlock.querySelector(".leaderboardPlayerStatName").textContent;
	const text = statBlock.querySelector(".leaderboardPlayerStatValue").textContent;
	const number = ((text.includes("k"))? (text.replace("k","") * 1000.0) : +(text));
	if(statName.includes("MESSAGES")){
	messages = number;
	}else if(statName.includes("EXPERIENCE")){
	experience = number;
	}
	}
	const stats = player.querySelector(".leaderboardPlayerStats");
	const messagesElement = stats.firstChild;

	const spamminess = ((messages/experience)*2000.0).toFixed(2);
	
	player.spamminess = stats.insertBefore(statBlock("SPAMMINESS", spamminess), messagesElement);
}