> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.I67lMHsnaCH/rev.2941
 * 
 * authors: 
 *   Daniel Buschek
 *   Legento

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



/**
*
*/

int mode; // in out or changes

// Arrays to store data from .csv-file:
String[] names;
int[] ins;
int[] outs;
int[] change;

float maxIn = -99999;
float minIn = 99999;
float maxOut = -99999;
float minOut = 99999;
float maxAll;
float maxChange;

float diagW;
float diagH;
int borderX;
int borderY;

int count; // number of data items

float barHeight; // height of one bar
int gap = 0; // height of gap between bars


float animOut = 0;

void setup() {  // this is run once.   
    size(600, 900); 
    smooth();
    frameRate(30);
    textFont(createFont("Arial", 9));
    
    //Set values:
    borderX = 20;
    borderY = 20;
    diagW = width - 2*borderX;
    diagH = height - 2*borderY;
    

    String[] lines = loadStrings("/static/uploaded_resources/p.3542/input_clean_sorted.csv");
    count = lines.length;
    
    // Create data-arrays:
    names  = new String[count]; //To save the district names
    ins = new int[count];
    outs = new int[count];
    change = new int[count];
   
    // Iterate over lines:
    for(int i = 0; i < lines.length; i++) {
        
        //Split in columns:
        String[] line = splitTokens(lines[i], ";");
        names[i] = trim(line[0]); // store name     
        
        int inValue = int(trim(line[1])) + int(trim(line[3])); //Read in
        int outValue = int(trim(line[2])) + int(trim(line[4])); //Read out   
         
        ins[i] = inValue; // store in
        outs[i] = outValue; // store out
        change[i] = ins[i] - outs[i];
        
        // adjust min and max values accordingly
        minIn  = min(minIn, inValue);
        maxIn  = max(maxIn, inValue);
        minOut = min(minOut, outValue);
        maxOut = max(maxOut, outValue);
        maxChange = max(maxChange, abs(change[i]));
    }
    //-> Now all data is stored in the arrays.
    
    maxAll = max(maxIn, maxOut);
    barHeight = (diagH - (count - 1) * gap) / count;
} 


void draw() {  // this is run repeatedly.  
    // set the background color
    background(255);
    
    float middle = width/2;

    //Draw rulers:
    
    stroke(200);
    fill(200);
    for(int i = 1; i < 6; i ++) {
        float xLeft = middle - i/5.0 * diagW/2;
        float xRight = middle + i/5.0 * diagW/2;
        line(xLeft, borderY, xLeft, borderY + diagH);
        line(xRight, borderY, xRight, borderY + diagH);
        
        //Caption:
        String capLeft = "" + int(i/5.0 * ((mode==0)?maxIn:maxChange));
        text(capLeft, xLeft - textWidth(capLeft)/2, borderY/2.0 + textAscent());
        String capRight = "" + int(i/5.0 * ((mode==0)?maxOut:maxChange));
        text(capRight, xRight - textWidth(capRight)/2, borderY/2.0 + textAscent());
    }
    
    
    //Draw data:
    
 
    animOut = min(0.05+animOut*1.1, 1);

    
    //Iterate over data-cases:
    for(int i = 0; i < names.length; i++){
        noStroke();   
        float barLeftW = ins[i]/maxAll * diagW/2 * animOut;
        float barRightW = outs[i]/maxAll * diagW/2 * animOut;
        float barY = borderY + i*(barHeight + gap);
        
    //Mode 0:    
    if(mode == 0){
            
        //Draw in-bar:
        fill(138, 182, 203);
        rect(middle - barLeftW, barY, barLeftW, barHeight);
        
        //Draw out-bar:
        fill(234, 192, 107);
        rect(middle, barY, barRightW, barHeight);
        
        //Draw compare-hints:
        stroke(0);
        line(middle - barRightW, barY, middle - barRightW, barY + barHeight);
        line(middle + barLeftW, barY, middle + barLeftW, barY + barHeight);
    }
    //Mode 1:
    else {

        float changeBarW = abs(change[i]/maxChange * diagW/2) * animOut;
        if(change[i] < 0) { // more outs than ins
            fill(234, 192, 107);
            rect(middle, barY, changeBarW, barHeight);
        } else {
            fill(138, 182, 203);
            rect(middle - changeBarW, barY, changeBarW, barHeight);    
        }
            
        }      
    }
    
   //Draw captions:
   fill(0);       
   noStroke();  
   textAlign(LEFT, CENTER);
   for(int i = 0; i < names.length; i++){
        float barY = borderY + i*(barHeight + gap);
        text(names[i], middle - textWidth(names[i])/2, barY + barHeight/2);
    }
   textAlign(LEFT);
}


void mouseClicked() {
    mode = (mode + 1) % 2;
    animOut = 0;
    }