xxxxxxxxxx
let data = [78, 64, 63, 68, 51, 44, 52, 50, 51, 45, 36, 35, 44, 42];
let years = [];
// Generate array of years from 2002 to 2018
for (let i = 2002; i <= 2018; i += 2) {
years.push(i);
}
// Create data object with years as keys
let dataObj = {};
for (let i = 0; i < years.length; i++) {
dataObj[years[i]] = data[i];
}
function setup() {
createCanvas(800, 400,SVG);
colorMode(HSB, 360, 100, 100, 100);
noStroke();
background(0); // black background
// Draw circles representing the values
let maxCircles = max(data);
let columnWidth = width / years.length;
for (let i = 0; i < years.length; i++) {
let year = years[i];
let numCircles = dataObj[year];
let columnX = i * columnWidth;
let columnY = height;
for (let j = 0; j < numCircles; j++) {
let x = columnX + random(columnWidth);
let y = columnY - map(j, 0, numCircles - 1, 0, columnY);
let size = map(j, 0, numCircles - 1, 5, 50); // set circle size based on position in column
let hue = map(dataObj[year], 0, maxCircles, 0, 360);
fill(hue, 80, 80, 80);
ellipse(x, y, size, size);
}
// Display year above column
textSize(16);
textAlign(CENTER);
fill(255);
text(year, columnX + columnWidth / 2, 20);
}
}
function keyPressed() {
if (key === 'D') {
save("my_sketch2.svg");
}
}