- size(600,600);
- //Create a vertical bar graph (Assignment 3)
- float loffset = width/12;
- float hoffset = height/12;
- float cptloffset = loffset;
- float cpthoffset = hoffset;
- for (int i = 0; i < 10; i++ ) {
- for (int j = 0; j < 10; j++ ) {
- rect(cptloffset, cpthoffset, loffset, hoffset);
- cpthoffset = cpthoffset + hoffset;
- }
- cptloffset = cptloffset + loffset;
- cpthoffset = hoffset;
- }
- // Initialize temporary array . used to scale with data
- float[] valuestmp = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
- // choose 10 values
- float[] values = {10, 65, 30, 25, 70, 45, 90, 17, 50, 83 };
- //float[] values = {1, 6, 3, 2, 7, 4, 9, 1, 5, 8 };
- //float[] values = {111, 666, 333, 222, 777, 444, 555, 999, 555, 888 };
- //float[] values = {1000, 5555, 3000, 2255, 7000, 4455, 9000, 995, 4555, 8000 };
- //float[] values = {57, 533, 123, 300, 101, 950, 123, 800, 765, 450 };
- float sum = 0;
- for (int i = 0; i < values.length; i++){
- sum = sum + values[i] ;
- }
- float meanfinal = (float)sum/values.length;
- float mean = meanfinal;
- float meantmp = mean ;
- //graph scale with the size of the canvas, and also scale with data.
- //I am able to change one of my variables and regardless of the value chosen
- // Initialize maximum element
- float max = values[0];
- for (int x = 0; x < values.length; x++)
- if (values[x] > max)
- max = values[x];
- float maxval = max * hoffset ;
- // copy array in another array
- for (int i = 0; i < values.length; i++){
- valuestmp[i] = values[i] ;
- }
- while(maxval >= height )
- {
- for (int i = 0; i < valuestmp.length; i++){
- valuestmp[i] = valuestmp[i] / 10;
- }
- meantmp = meantmp / 10;
- maxval = maxval / 10 ;
- }
- float value = 0;
- int cpt = 0;
- int coloroffset = 30;
- String msg = "";
- for(int i = 0; i < valuestmp.length ; i++){
- cpt++;
- value = valuestmp[i] ;
- if ((values[i] % 2 ) == 0 ) { // If the value is even, then it should be displayed in a shade of green.
- fill(0,coloroffset * value,0);
- msg = " " + (int)values[i] ;
- rect(cpt * loffset , (height - hoffset) - (value * hoffset), loffset, (value * hoffset));
- text(msg, cpt * loffset, (height-hoffset) - (value * hoffset));
- }
- else { // If it is odd, then it should be displayed as a shade of red.
- fill(coloroffset*value,0,0);
- msg = " " + (int)values[i] ;
- rect(cpt * loffset , (height-hoffset) - (value * hoffset), loffset, (value * hoffset));
- text(msg, cpt * loffset, (height-hoffset) - (value * hoffset));
- }
- }
- strokeWeight(5);
- stroke(0,0,255);
- line ( loffset , (height-hoffset)- (meantmp * hoffset) , (loffset * valuestmp.length) +loffset , (height-hoffset)- (meantmp * hoffset) );
- msg = " " + mean ;
- fill(0,0,255);
- text(msg, loffset*0.5 , (height-hoffset)- (meantmp * hoffset) );