Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** BoxPlot Ver 6.0 By Panos Boufardeas 4-5-2017 @link https://pastebin.com/GtTPAqVW
- // 8-5-2017 Ver 6.1, Offset added in the Grid, Correction for Median and Q3, wiskers now shows the Min and Max value
- // Draw horizontal, Vertical
- // For this version we are going to use external data by Reading ASCII Data Files
- // How to populate Matrix from a text file @link http://www.amibroker.com/kb/index.php?s=matrix
- // In This boxplot Example i use the data from this link @link http://peltiertech.com/excel-box-and-whisker-diagrams-box-plots/
- // Aslo the code is ready to use your own data from a text file just enable the code @ line 52 PART 1)- populate Matrix from a text file
- // if you don’t have Amibroker version 621 then just make your own parameters instead of GuiButton, @ ParamToggle Vertical, Horizontal
- information about EXCEL calculation @link http://tinyurl.com/kcrxlfn
- */
- // the input file path
- // file = "C:\\samplematrix.txt";
- file = "C:\\temp\\beta.csv";
- // _TRACE_with_GuiButton_Parameter
- GuiButton( "DebugView", 1, Status( "pxwidth" )-80, 50, 80, 24, 7 );
- id = GuiGetEvent( 0, 0 ); event = GuiGetEvent( 0, 1 ); DebugOn=0;
- if( id == 1 && event == 1 ) {DebugOn=1; GuiSetText("Button clicked",1); }
- //if(DebugOn) _TRACE("#, Message = " + msgid);
- // ParamToggle Vertical, Horizontal
- GuiButton( "Vertical", 2, Status( "pxwidth" )-80, 80, 80, 24, 1);
- id = GuiGetEvent( 0, 0 ); event = GuiGetEvent( 0, 1 );
- Onoff = Nz( StaticVarGet( "BoxPlotGuiOnOff" ), 0 );
- bt = GetCursorMouseButtons();
- if( id == 2 && event == 1 )
- {
- if( Onoff == 0) StaticVarSet( "BoxPlotGuiOnOff", 1 );
- if( Onoff == 1) StaticVarSet( "BoxPlotGuiOnOff", 0 );
- }
- if(Onoff) { GuiSetText( "Horizontal", 2 ); }
- if( bt & 8 ) RequestTimedRefresh( 0.1 );
- // temporary data for testing
- myMatrix=MxFromString("[14.67,17.40,10.14,6.42;10.06,17.15,1.62,17.18;8.26,19.17,20.25,10.91;6.95,9.83,5.92,6.80;
- 5.05,8.26,9.68,6.22;11.47,8.18,17.46,5.53;11.95,12.07,7.43,4.88;3.88,17.28,17.78,4.86;8.31,9.05,10.22,14.21;
- 6.54,6.36,23.72,6.62;4.48,7.40,2.65,15.73;8.79,14.20,8.29,12.70;10.97,9.86,3.35,14.94;8.15,18.08,17.99,13.43;
- 7.13,11.75,10.21,8.63;15.14,10.29,5.45,13.92;7.67,9.79,10.94,0.57;15.24,10.36,13.73,12.69;
- 14.08,8.86,12.42,15.36;6.07,12.06,16.71,11.00]");
- /*
- ////// PART 1)- populate Matrix from a text file
- // define the size of the desired matrix
- rows = 20;
- cols = 4;
- // create matrix
- myMatrix = Matrix( rows, cols, 0 );
- // open file
- fh = fopen( file, "r" );
- if (fh==0) Error("file does NOT exist "+ File);
- if( fh )
- {
- i = 0;
- // iterate through the lines of input file
- for( i = 0; ! feof( fh ) AND i < rows; i++ )
- {
- // read a line of text
- line = fgets( fh );
- if( line == "" )
- {
- Error("Too few rows in the data file or an empty row found");
- break;
- }
- // iterate through the elements of the line
- for( j = 0; ( item = StrExtract( line, j ) ) != "" AND j < cols; j++ )
- {
- //if(DebugOn) _TRACE("#, line = " + line);
- // assign matrix element
- myMatrix[ i ][ j ] = StrToNum( item );
- }
- if( j < cols )
- {
- Error("Too few columns in data file");
- break;
- }
- }
- fclose( fh );
- }
- else
- {
- Error( "ERROR: file can not be opened" );
- }
- // spot check selected element
- //Title = "spot check M[ 2 ][ 3 ]: " + NumToStr( MyMatrix[ 2 ][ 3 ] )+ "\nspot check M[ 0 ][ 1 ]: " + NumToStr( MyMatrix[ 0 ][ 1 ] );
- */
- ////// PART 2)- Sort the matrix by column
- mxSortCol = MxSort( myMatrix, 1 ) ; // printf( MxToString( mxSortCol ) + " " );
- if(DebugOn) _TRACE("#, SortColumn = " + MxToString( mxSortCol ));
- // how many rows and column this matrix has?
- rows = MxGetSize( myMatrix, 0 );
- col = MxGetSize( myMatrix, 1 ); printf( "\nrows: %g, columns: %g\n\n", rows, col );
- ////// PART 3)- Find the Median
- ////// PART 4)- Find the MEAN = average, of the sequence 1+2+3+..n / n
- function MxMeanColumn( x )
- {
- sumX=0;
- for( i = 0; i < rows; i++ )
- {
- sumX += mxSortCol[ i ][ x ]; // sum of Column Χ
- }
- return MEAN= sumX/rows;
- }
- function MaxNumColumn( x ) // Find the Max Num of the Columns
- {
- MaxNum=zero=0;
- for( i = 0; i < rows; i++ )
- {
- MaxNum = mxSortCol[ i ][ x ];
- }
- MaxNumX= Max(MaxNum,zero);
- return MaxNumX;
- }
- Offset=0.5; tempMin=TempMax=test1=0;
- for( j = 0; j < col; j++ ) // loop for every column
- {
- MaxNumi= MaxNumColumn(j);
- if (TempMax<MaxNumi) TempMax=MaxNumi[j];
- MaxNum= Max(TempMax,MaxNumi)+Offset; //printf( "\n MaxNum_"+i+ " =\t" + MaxNum );
- // Find the Minimum Number of the Columns
- MinNumi=mxSortCol[0][j];
- if (tempMin>MinNumi) tempMin=MinNumi[j];
- MinNum=Min(tempMin,MinNumi)-Offset; //printf("\n MinNum_"+i+ " =\t" + MinNum );
- }
- GfxSetOverlayMode(2); // 2 only low-level graphics
- GfxSetTextColor(colorGrey40);
- GfxSetBkMode(1); // transparent
- function DrawCircle(x,y,radius,BackColor)
- {
- GfxSelectSolidBrush( BackColor );
- GfxSelectPen( colorYellow );
- GfxCircle( x, y, radius );
- }
- YOffset = 35;
- XOffset = 35;
- /////////////////////////////////////////////
- // Draw horizontal GridLines
- width = Status( "pxwidth" ) - 4 * XOffset;
- height = Status( "pxheight" ) - 2 * YOffset;
- YGrid=XGrid=0;
- function YDrawLevels( Miny, Maxy ) // horizontal GridLines
- {
- range = Maxy - Miny;
- if( range < 6 ) YGrid=XGrid = 0.5; else
- if( range < 11 ) YGrid=XGrid = 1; else
- if( range < 30 ) YGrid=XGrid = 2; else
- if( range < 51 ) YGrid=XGrid = 5; else
- if( range < 101 ) YGrid=XGrid = 10; else
- if( range < 201 ) YGrid=XGrid = 20; else
- if( range < 501 ) YGrid=XGrid = 50;
- GfxSelectPen( colorGrey40, 1, 2 );
- for( y = YGrid * ceil( Miny / YGrid ); y <= YGrid * floor( Maxy / YGrid ); y += YGrid )
- {
- yp = YOffset + Height * ( 1 - ( y - Miny ) / ( Maxy - Miny ) );
- GfxMoveTo( XOffset, yp );
- GfxLineTo( XOffset + width , yp );
- GfxTextOut( "" + y, XOffset + 2 + width, yp );
- }
- GfxSelectPen( colorGrey40, 2, 0 ); //the Frame
- GfxMoveTo( XOffset, YOffset );
- GfxLineTo( XOffset + width, YOffset ); // Top Line
- GfxLineTo( XOffset + width, YOffset + Height ); // Bottom
- GfxLineTo( XOffset , YOffset + Height );
- GfxLineTo( XOffset , YOffset );
- }
- /////////////////////////////////////////////
- // Draw horizontal Vertical GridLines
- function XDrawLevels( Minx, Maxx ) // Vertical GridLines
- {
- range = Maxx - Minx;
- if( range < 6 ) YGrid=XGrid = 0.5; else
- if( range < 11 ) YGrid=XGrid = 1; else
- if( range < 30 ) YGrid=XGrid = 2; else
- if( range < 51 ) YGrid=XGrid = 5; else
- if( range < 101 ) YGrid=XGrid = 10; else
- if( range < 201 ) YGrid=XGrid = 20; else
- if( range < 501 ) YGrid=XGrid = 50;
- GfxSelectPen( colorGrey40, 1, 2 );
- for( x = XGrid * ceil( Minx / XGrid ); x <= XGrid * floor( Maxx / XGrid ); x += XGrid )
- {
- xp = XOffset + Width * ( x - Minx ) / ( Maxx - Minx );
- GfxMoveTo( xp, YOffset );
- GfxLineTo( xp, YOffset + height );
- GfxTextOut( "" + x, xp - 10, YOffset + 2 + height );
- if(DebugOn) _TRACE("#,Vertical GridLines X= "+ x +",XGrid: "+XGrid + ", xpixels "+xp + ", width= "+width+ ", height= "+height );
- }
- GfxSelectPen( colorGrey40, 2, 0 ); //the Frame
- GfxMoveTo( XOffset, YOffset );
- GfxLineTo( XOffset + width, YOffset ); // Top Line
- GfxLineTo( XOffset + width, YOffset + Height ); // Bottom
- GfxLineTo( XOffset , YOffset + Height );
- GfxLineTo( XOffset , YOffset );
- }
- // For Chart Reading Information
- GfxTextOut( "Mean" , width+2*xOffset+12, Height/2 );
- DrawCircle(width+2*xOffset,Height/2+8,3,Colorred); // Mean
- GfxTextOut( "Median" , width+2*xOffset+12, Height/2+20 );
- GfxSelectPen( colorYellow, 1,2 );
- GfxMoveTo( width+2*xOffset-10, Height/2+30 ); GfxLineTo( width+2*xOffset, Height/2+30 );
- if(Onoff==1)xDrawLevels(MinNum,MaxNum);
- if(Onoff==0)YDrawLevels(MinNum,MaxNum);
- GfxSetTextColor( colorLightGrey );
- GfxTextOut( "BoxPlot, Ver 6.1 " , 45, 10 );
- ///////////////////////////////////////////////////////////////////////
- // -------------- Vertical Draw BoxPlot -------------------------------
- ////////////////////////////////////////////////////////////////////////
- function DrawBoxPlotVertical( Q4wkr, Q3wkr, Q3, IQR, Q1,Mean, bar, numbars, Miny, Maxy )
- {
- BarWidth = ( Status( "pxwidth" ) - 4 * XOffset ) / ( numbars + 0.5 );
- BarHeight = Status( "pxheight" ) - 2 * YOffset;
- GfxSelectPen( colorYellow, 1,0 );
- Ypix1 = YOffset + BarHeight * ( 1 - ( Q1 - Miny ) / ( Maxy - Miny ) ); // Correct BarHeight
- Ypix2 = YOffset + BarHeight * ( 1 - ( Q3 - Miny ) / ( Maxy - Miny ) );
- Ypix3 = YOffset + BarHeight * ( 1 - ( Q3wkr - Miny ) / ( Maxy - Miny ) );
- Ypix4 = YOffset + BarHeight * ( 1 - ( Q4wkr - Miny ) / ( Maxy - Miny ) );
- Ypix5 = YOffset + BarHeight * ( 1 - ( IQR - Miny ) / ( Maxy - Miny ) );
- Ypix6 = YOffset + BarHeight * ( 1 - ( Mean - Miny ) / ( Maxy - Miny ) );
- x0 = xp = XOffset + ( bar + 0.5 ) * BarWidth; //x0 θεση που αρχιζει το βαρ ειναι το(+0.5) δηλαδη το παχος του ΒΑΡ μεχρι το (1)
- x1 = xe = XOffset + ( bar + 1 ) * BarWidth; //x1 θεση που τελειωνει (+1)
- xMiddle = XOffset + ( bar + 0.75 ) * BarWidth; // Middle Box (+1/2)
- WLEnd = XOffset + ( bar + 0.70 ) * BarWidth;
- WREnd = XOffset + ( bar + 0.80 ) * BarWidth;
- //Draw Whisker
- GfxMoveTo( xMiddle + 0.5, Ypix3 ); GfxLineTo( xMiddle + 0.5, Ypix4 );
- //Draw Top Whiskers end
- GfxMoveTo( WLEnd, Ypix4 ); GfxLineTo( WREnd , Ypix4 );
- //Draw Bottom end
- GfxMoveTo( WLEnd, Ypix3 ); GfxLineTo( WREnd , Ypix3 );
- //Draw BoxPlot
- //GfxGradientRect( x0, Ypix2, x1, Ypix1, colorPaleBlue, colorPlum );
- GfxGradientRect( x0, Ypix2, x1, Ypix1,18, 3 );
- //Draw Median, IQR
- GfxSelectPen( colorYellow, 1,1 ); GfxMoveTo( x0, Ypix5 ); GfxLineTo( x1, Ypix5 );
- //Draw the Mean
- DrawCircle(xMiddle,Ypix6,2,Colorred);
- GfxTextOut( "Q3: " + Q3, x0, Ypix2 );
- GfxTextOut( "Q1 " + Q1, x0, Ypix1 );
- if(DebugOn) _TRACE("#,DrawBoxPlotVert, x0 = "+x0 + ",x1 " + x1 + ",Ypix1 "+Ypix1 + ",Ypix2 " + Ypix2);
- }
- //////////////////////////////////////////////////////////////
- // Horizontal /////////// Horizontal /////////// Horizontal /////////// Horizontal
- function DrawBoxPlotHorizontal( Q4wkr, Q3wkr, Q3, IQR, Q1, Mean , bar, numbars, Minx, Maxx )
- {
- BarWidth = Status( "pxwidth" ) - 4 * XOffset ;
- BarHeight = ( Status( "pxheight" ) - 2 * YOffset) / ( numbars + 0.5 );
- GfxSelectPen( colorYellow, 1, 0 );
- // xp = XOffset + Width * ( x - Minx ) / ( Maxx - Minx );
- Xpix1 = XOffset + BarWidth * ( Q1 - Minx ) / ( Maxx - Minx ); // Correct BarHeight
- Xpix2 = XOffset + BarWidth * ( Q3 - Minx ) / ( Maxx - Minx );
- Xpix3 = XOffset + BarWidth * ( Q3wkr - Minx ) / ( Maxx - Minx );
- Xpix4 = XOffset + BarWidth * ( Q4wkr - Minx ) / ( Maxx - Minx );
- Xpix5 = XOffset + BarWidth * ( IQR - Minx ) / ( Maxx - Minx );
- Xpix6 = XOffset + BarWidth * ( Mean - Minx ) / ( Maxx - Minx );
- y0 = xp = YOffset + ( bar + 0.5 ) * BarHeight; //y0 θεση που αρχιζει το βαρ ειναι το(+0.5) δηλαδη το παχος του ΒΑΡ μεχρι το (1)
- y1 = xe = YOffset + ( bar + 1 ) * BarHeight; //y1 θεση που τελειωνει (+1)
- xMiddle = YOffset + ( bar + 0.75 ) * BarHeight; // Middle Box (+1/2)
- WLEnd = YOffset + ( bar + 0.70 ) * BarHeight;
- WREnd = YOffset + ( bar + 0.80 ) * BarHeight;
- //DrawWhisker
- GfxMoveTo(Xpix3, xMiddle); GfxLineTo(Xpix4, xMiddle);
- //Draw Left Whiskers end
- GfxMoveTo( Xpix3, WLEnd ); GfxLineTo( Xpix3,WREnd );
- //Draw Right end
- GfxMoveTo( Xpix4, WLEnd ); GfxLineTo( Xpix4,WREnd );
- //DrawBoxPlot
- GfxGradientRect( Xpix1, y0, Xpix2,y1, colorPaleBlue, colorPlum );
- //Draw Median, IQR
- GfxSelectPen( colorYellow, 1,1 );
- GfxMoveTo( Xpix5, y0 );
- GfxLineTo( Xpix5, y1 );
- //Draw the Mean
- DrawCircle(Xpix6,xMiddle,2,Colorred);
- GfxTextOut( "Q3: " + Q3, Xpix2 ,y0);
- GfxTextOut( "Q1: " + Q1, Xpix1 ,y0 );
- //if(DebugOn) _TRACE("#,DrawBoxPlotHoriz, y0 = "+y0 + ",y1 " + y1 + ",Xpix1 "+Xpix1 + ",Xpix2 " + Xpix2);
- }
- //////////////////////////////////////////////////
- ////// PART 5) calculate the body and whisker for every column, and draw the boxPlot in the chart
- bar=0; MinAvg = MaxAvg = 0;
- for( j = 0; j < col; j++ ) // loop for every column
- { printf("\n\n --- Box J = "+j);
- MeanX= MxMeanColumn( j ); printf( "\n Mean Col "+j+"=\t" + MeanX );
- // Find the Median, that is the middle Number of the sequence
- Med1 = mxSortCol[rows*0.49][j]; //printf("\nMed1 Col "+j+"=\t "+ Med1 );
- Med2 = mxSortCol[rows*0.50][j]; //printf("\nMed2 Col "+j+"=\t "+ Med2 );
- Q2 = (Med1+Med2)/2; printf("\nQ2 Median "+j+"=\t "+ Q2 );
- Q1 = mxSortCol[rows*0.25][j]; printf("\nQ1 Col "+j+"=\t "+ Q1 );
- Q3 = mxSortCol[rows*0.74][j]; printf("\nQ3 Col "+j+"=\t "+ Q3 );
- Q1whisker = mxSortCol[0][j]; printf("\nQ1whisker Col "+j+"=\t "+ Q1whisker );
- Q4whisker = mxSortCol[rows-1][j]; printf("\nQ4whisker Col "+j+"=\t "+ Q4whisker );
- //printf("\n Q1 = %g,\t\n Q2 = %g\n", Q1, Q2 ); printf("Q3 = %g,\t\nQ1whisker = %g,\t\nQ4whisker = %g\n", Q3, Q1whisker , Q4whisker );
- if(DebugOn) _TRACE("#,(Loop) Col: "+J + ", Q1 = "+Q1 + ", Q2= "+Q2+ ", Q3= "+Q3 + ", Q1whisker= "+Q1whisker+ ", Q4whisker= "+Q4whisker);
- if(Onoff==0) DrawBoxPlotVertical( Q4whisker,Q1whisker,q3,q2,q1,Meanx,bar, numbars=col, Miny=MinNum, Maxy=MaxNum );
- if(Onoff==1) DrawBoxPlotHorizontal( Q4whisker,Q1whisker,q3,q2,q1,Meanx,bar, numbars=col, Miny=MinNum, Maxy=MaxNum ) ;
- if(DebugOn) _TRACE("#,----------");
- bar++;
- }
Add Comment
Please, Sign In to add comment