//BARCHART var data; var dict = {}; var max_count = 0; var dict_size = 0; function preload() { data = loadTable('health.csv', 'csv', 'header'); } function setup() { createCanvas(800,800); //cycle through the table for (var r = 0; r < data.getRowCount(); r++) { var disease = data.getString(r, 0); var count = data.getNum(r,3); if(disease in dict) { dict[disease][0] += count; } else { dict[disease] = []; dict[disease][0] = count; //fill with colors dict[disease][1] = Math.random() *255; dict[disease][2] = Math.random() *255; dict[disease][3] = Math.random() *255; dict_size++; } } for(key in dict) { if(dict[key][0] >= max_count) { max_count = dict[key][0]; } } } function draw() { clear(); textSize(20); text("Number of Disease Cases in the United States", 50, 50); textSize(10); //axes drawn (draw little lines on axes) line(100,100,100,600); line(100,600,700,600); //change to be dynamically calculated var count = 0; for(var l = 600; l >= 100; l--) { if(l%50 == 0) { fill(0); line(90,l,110,l); fill(0); text(round(count), 20, l); count += round(.1 * max_count); } } //add user interaction when a bar is hovered over var bar_width = 600/dict_size; var scale = 500/max_count; var counter = 1; for(key in dict) { //textSize(10); fill(dict[key][1], dict[key][2], dict[key][3]); rect(counter * bar_width, 600, 50, -dict[key][0]*scale); if(mouseX > counter*bar_width && mouseY > 600-dict[key][0]*scale && mouseX < counter*bar_width + 50 && mouseY < 600) { fill(0); text(key + " " + dict[key][0], mouseX, mouseY); } fill(0); text(key, counter * bar_width, 610); //same colors for legend var legend_y = 100 +(counter*20) fill(dict[key][1], dict[key][2], dict[key][3]); text(key, 500, 100 + (counter*20)); counter++; } //legend fill(0); textSize(20); text("Legend", 500,100); }