HISTROGRAM !!!
/* Program Draw Histrogram */
/* Variable */
int Space=50,MiniSpace=5;
int MaxValueY=500,ValuePerScaleY=50,RatioY=1;
int MinValueX=40,MaxValueX=100,ValuePerScaleX=10,RatioX=4;
/*
Ratio = Pixel/Value
Example
Ratio = 2 is mean value 1 points equal to 2 pixels
Ratio = 0.5 is mean value 1 points equal to 0.5 pixels
*/
int n = int((MaxValueX-MinValueX)/ValuePerScaleX);
//int[] Y = new int[n];
int[] Y = new int[n];
int[] StartX = new int[n];
void setup(){
size(500,600);
MiniSpace=((MinValueX*RatioX)<MiniSpace)?(MinValueX*RatioX):MiniSpace;
background(255);
stroke(0);
fill(0);
frameRate(60);
for(int i=0;i<n;i++){
Y[i] = int(random(0,MaxValueY));
}
for(int i=0;i<n;i++){
StartX[i] = Space+((MiniSpace + (ValuePerScaleX*i))*RatioX);
}
}
void draw(){
background(255);
stroke(0);
fill(0);
int MinValue = FindMin(Y);
int MaxValue = FindMax(Y);
float AverageValue = FindAverage(Y);
DrawAxis(MinValueX,MaxValueX,MaxValueY,ValuePerScaleX,ValuePerScaleY,RatioX,RatioY,Space,MiniSpace);
for(int i=0;i<n;i++){
DrawGraph(StartX[i],height-Space-(Y[i]*RatioY),ValuePerScaleX*RatioX,Y[i]*RatioY,MinValue,MaxValue);
}
DrawMinMaxAvg(MinValue,MaxValue,AverageValue);
DrawLineNow(mouseX,mouseY);
}
void mouseDragged(){
for(int i=0;i<n;i++){
if((StartX[i]<mouseX)&&(mouseX<(StartX[i]+(ValuePerScaleX*RatioX)))){
if((height-mouseY)>(Space+(MaxValueY*RatioY))){
Y[i] = MaxValueY;
}
else{
if((height-mouseY)<Space){
Y[i] = 0;
}
else{
Y[i] = height-mouseY-Space;
}
}
}
}
}
void mousePressed(){
for(int i=0;i<n;i++){
if((StartX[i]<mouseX)&&(mouseX<(StartX[i]+(ValuePerScaleX*RatioX)))){
if((height-mouseY)>(Space+(MaxValueY*RatioY))){
Y[i] = MaxValueY;
}
else{
if((height-mouseY)<Space){
Y[i] = 0;
}
else{
Y[i] = height-mouseY-Space;
}
}
}
}
}
void DrawAxis(int MinX,int MaxX,int MaxY,int SX,int SY,int RX,int RY,int Sp,int MSp){
int GapTextX1=5,GapTextY1=5,GapTextX2=0,GapTextY2=20;
//1 mean Y axis , 2 mean X axis
stroke(0);
noFill();
rect(Sp,height-Sp-(MaxY*RY),MSp+((MaxX-MinX)*RX)+Sp,MaxY*RY);
//draw scale Y
stroke(150);
fill(0);
textAlign(RIGHT);
text(0,Sp-GapTextX1,height-Sp+GapTextY1);
text(MaxY,Sp-GapTextX1,height-Sp-(MaxY*RY)+GapTextY1);
for(int i=1;(SY*i)<MaxY;i++){
line(Sp,height-Sp-(SY*RY*i),Sp+MSp+((MaxX-MinX)*RX)+Sp,height-Sp-(SY*RY*i));
text(SY*i,Sp-GapTextX1,height-Sp-(SY*RY*i)+GapTextY1);
}
//draw scale X
textAlign(CENTER);
stroke(0);
if(MSp > 0){
line(Sp+(MSp*RX),height-Sp-5,Sp+(MSp*RX),height-Sp+5);
text(MinX,Sp+(MSp*RX)-GapTextX2,height-Sp+GapTextY2);
}
line(Sp+(MSp*RX)+((MaxX-MinX)*RX),height-Sp-5,Sp+(MSp*RX)+((MaxX-MinX)*RX),height-Sp+5);
text(MaxX,Sp+(MSp*RX)+((MaxX-MinX)*RX)-GapTextX2,height-Sp+GapTextY2);
for(int i=1;(SX*i)<(MaxX-MinX);i++){
line(Sp+(MSp*RX)+(SX*RX*i),height-Sp-5,Sp+(MSp*RX)+(SX*RX*i),height-Sp+5);
text(MinX+(SX*i),Sp+(MSp*RX)+(SX*RX*i)-GapTextX2,height-Sp+GapTextY2);
}
}
void DrawGraph(int X,int Y,int W,int H,int min,int max){
color C;
if((H==min)&&(min!=max)){
C = color(122,255,127);
}
else{
if((H==max)&&(min!=max)){
C = color(255,0,0);
}
else{
C = color(255,255,0);
}
}
fill(C);
rect(X,Y,W,H);
}
void DrawLineNow(int x,int y){
stroke(0);
fill(0);
int LLine=10,LGap=5;
//assign x
if(x<Space){
x=Space;
}
else{
if(x>(Space+(MiniSpace+((MaxValueX-MinValueX)*RatioX))+Space)){
x=Space+(MiniSpace+((MaxValueX-MinValueX)*RatioX))+Space;
}
}
//assign y
if((height-y)<Space){
y=height-Space;
}
else{
if((height-y)>(Space+(MaxValueY*RatioY))){
y=height-(Space+(MaxValueY*RatioY));
}
}
//draw line x
for(int i=0;(Space+(LLine*i)+(LGap*i))<x;i++){
if((Space+(LLine*(i+1))+(LGap*i))<x){
line(Space+(LLine*i)+(LGap*i),y,Space+(LLine*(i+1))+(LGap*i),y);
}
else{
line(Space+(LLine*i)+(LGap*i),y,x,y);
}
}
//draw line y
for(int i=0;(Space+(LLine*i)+(LGap*i))<(height-y);i++){
if((Space+(LLine*(i+1))+(LGap*i))<(height-y)){
line(x,height-Space-(LLine*i)-(LGap*i),x,height-Space-(LLine*(i+1))-(LGap*i));
}
else{
line(x,height-Space-(LLine*i)-(LGap*i),x,y);
}
}
text((height-y-Space)/RatioY,x,y);
}
void DrawMinMaxAvg(int min,int max ,float avg){
textAlign(LEFT);
//draw min
fill(122,255,127);
text("Min = "+min,Space+MiniSpace+((MaxValueX-MinValueX)*RatioX)+Space+5,height-Space-(MaxValueY*RatioY));
//draw max
fill(255,0,0);
text("Max = "+max,Space+MiniSpace+((MaxValueX-MinValueX)*RatioX)+Space+5,height-Space-(MaxValueY*RatioY)+15);
//draw average
stroke(0,0,255);
fill(0,0,255);
line(Space,height-Space-(avg*RatioY),Space+MiniSpace+((MaxValueX-MinValueX)*RatioX)+Space,height-Space-(avg*RatioY));
text("Average = "+avg,Space+MiniSpace+((MaxValueX-MinValueX)*RatioX)+Space+5,height-Space-(MaxValueY*RatioY)+30);
}
int FindMin(int[] a){
int min = a[0];
for(int i=0;i<a.length;i++){
min = (min<a[i])? min:a[i];
}
return min;
}
int FindMax(int[] a){
int max = a[0];
for(int i=0;i<a.length;i++){
max = (max>a[i])? max:a[i];
}
return max;
}
float FindAverage(int[] a){
int sum = 0;
for(int i=0;i<a.length;i++){
sum = sum + a[i];
}
float avg = sum/a.length;
return avg;
}