Changed around line 1
+ // Generate random PCA-like data
+ function generateRandomLeaders(num) {
+ const leaders = [];
+ for (let i = 0; i < num; i++) {
+ leaders.push({
+ x: Math.random() * 2 - 1,
+ y: Math.random() * 2 - 1,
+ name: `Leader ${i + 1}`,
+ traits: {
+ authoritarianism: Math.random(),
+ corruption: Math.random(),
+ charisma: Math.random(),
+ longevity: Math.random()
+ }
+ });
+ }
+ return leaders;
+ }
+
+ // Initialize plot
+ function initPlot() {
+ const canvas = document.getElementById('leaderPlot');
+ const ctx = canvas.getContext('2d');
+ const leaders = generateRandomLeaders(200);
+
+ // Draw points
+ leaders.forEach(leader => {
+ const x = (leader.x + 1) * canvas.width / 2;
+ const y = (leader.y + 1) * canvas.height / 2;
+
+ ctx.beginPath();
+ ctx.arc(x, y, 3, 0, 2 * Math.PI);
+ ctx.fillStyle = '#ff4b2b';
+ ctx.fill();
+ });
+
+ // Find match functionality
+ document.getElementById('findMatch').addEventListener('click', () => {
+ const input = document.getElementById('leaderTraits').value.toLowerCase();
+ const match = findClosestMatch(input, leaders);
+ document.getElementById('matchResult').textContent = `Closest match: ${match.name}`;
+ });
+ }
+
+ // Simple matching algorithm
+ function findClosestMatch(input, leaders) {
+ // Convert input to trait scores (very basic implementation)
+ const inputTraits = {
+ authoritarianism: input.includes('authoritarian') ? 1 : 0,
+ corruption: input.includes('corrupt') ? 1 : 0,
+ charisma: input.includes('charismatic') ? 1 : 0,
+ longevity: input.includes('long') ? 1 : 0
+ };
+
+ // Find closest match
+ let closest = leaders[0];
+ let minDistance = Infinity;
+
+ leaders.forEach(leader => {
+ const distance = Math.sqrt(
+ Math.pow(leader.traits.authoritarianism - inputTraits.authoritarianism, 2) +
+ Math.pow(leader.traits.corruption - inputTraits.corruption, 2) +
+ Math.pow(leader.traits.charisma - inputTraits.charisma, 2) +
+ Math.pow(leader.traits.longevity - inputTraits.longevity, 2)
+ );
+
+ if (distance < minDistance) {
+ minDistance = distance;
+ closest = leader;
+ }
+ });
+
+ return closest;
+ }
+
+ // Initialize when DOM is loaded
+ document.addEventListener('DOMContentLoaded', initPlot);