panosbouf

BoxPlot For Amibroker

May 4th, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.96 KB | None | 0 0
  1. /** BoxPlot Ver 6.0 By Panos Boufardeas 4-5-2017 @link https://pastebin.com/GtTPAqVW
  2. // 8-5-2017 Ver 6.1, Offset added in the Grid, Correction for Median and Q3, wiskers now shows the Min and Max value
  3.  
  4. // Draw horizontal, Vertical
  5. // For this version we are going to use external data by Reading ASCII Data Files
  6. // How to populate Matrix from a text file @link http://www.amibroker.com/kb/index.php?s=matrix
  7. // In This boxplot Example i use the data from this link @link http://peltiertech.com/excel-box-and-whisker-diagrams-box-plots/
  8. // 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
  9. // if you don’t have Amibroker version 621 then just make your own parameters instead of GuiButton, @ ParamToggle Vertical, Horizontal
  10.  
  11. information about EXCEL calculation @link http://tinyurl.com/kcrxlfn
  12. */
  13.  
  14.  
  15. // the input file path
  16. // file = "C:\\samplematrix.txt";
  17. file = "C:\\temp\\beta.csv";
  18.  
  19. // _TRACE_with_GuiButton_Parameter
  20. GuiButton( "DebugView", 1, Status( "pxwidth" )-80, 50, 80, 24, 7 );
  21. id = GuiGetEvent( 0, 0 ); event = GuiGetEvent( 0, 1 ); DebugOn=0;
  22. if( id == 1 && event == 1 ) {DebugOn=1; GuiSetText("Button clicked",1); }
  23. //if(DebugOn) _TRACE("#, Message = " + msgid);
  24.  
  25. // ParamToggle Vertical, Horizontal
  26. GuiButton( "Vertical", 2, Status( "pxwidth" )-80, 80, 80, 24, 1);
  27. id = GuiGetEvent( 0, 0 ); event = GuiGetEvent( 0, 1 );
  28. Onoff = Nz( StaticVarGet( "BoxPlotGuiOnOff" ), 0 );
  29. bt = GetCursorMouseButtons();
  30.  
  31.  
  32. if( id == 2 && event == 1 )
  33. {
  34. if( Onoff == 0) StaticVarSet( "BoxPlotGuiOnOff", 1 );
  35. if( Onoff == 1) StaticVarSet( "BoxPlotGuiOnOff", 0 );
  36.  
  37. }
  38.  
  39. if(Onoff) { GuiSetText( "Horizontal", 2 ); }
  40.  
  41. if( bt & 8 ) RequestTimedRefresh( 0.1 );
  42.  
  43. // temporary data for testing
  44. 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;
  45. 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;
  46. 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;
  47. 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;
  48. 14.08,8.86,12.42,15.36;6.07,12.06,16.71,11.00]");
  49.  
  50.  
  51. /*
  52. ////// PART 1)- populate Matrix from a text file
  53.  
  54. // define the size of the desired matrix
  55. rows = 20;
  56. cols = 4;
  57.  
  58. // create matrix
  59. myMatrix = Matrix( rows, cols, 0 );
  60.  
  61. // open file
  62. fh = fopen( file, "r" );
  63. if (fh==0) Error("file does NOT exist "+ File);
  64. if( fh )
  65. {
  66. i = 0;
  67.  
  68. // iterate through the lines of input file
  69. for( i = 0; ! feof( fh ) AND i < rows; i++ )
  70. {
  71. // read a line of text
  72. line = fgets( fh );
  73.  
  74. if( line == "" )
  75. {
  76. Error("Too few rows in the data file or an empty row found");
  77. break;
  78. }
  79.  
  80. // iterate through the elements of the line
  81. for( j = 0; ( item = StrExtract( line, j ) ) != "" AND j < cols; j++ )
  82. {
  83. //if(DebugOn) _TRACE("#, line = " + line);
  84. // assign matrix element
  85. myMatrix[ i ][ j ] = StrToNum( item );
  86. }
  87.  
  88. if( j < cols )
  89. {
  90. Error("Too few columns in data file");
  91. break;
  92. }
  93. }
  94.  
  95. fclose( fh );
  96. }
  97. else
  98. {
  99. Error( "ERROR: file can not be opened" );
  100. }
  101.  
  102. // spot check selected element
  103. //Title = "spot check M[ 2 ][ 3 ]: " + NumToStr( MyMatrix[ 2 ][ 3 ] )+ "\nspot check M[ 0 ][ 1 ]: " + NumToStr( MyMatrix[ 0 ][ 1 ] );
  104. */
  105.  
  106. ////// PART 2)- Sort the matrix by column
  107. mxSortCol = MxSort( myMatrix, 1 ) ; // printf( MxToString( mxSortCol ) + " " );
  108. if(DebugOn) _TRACE("#, SortColumn = " + MxToString( mxSortCol ));
  109.  
  110. // how many rows and column this matrix has?
  111. rows = MxGetSize( myMatrix, 0 );
  112. col = MxGetSize( myMatrix, 1 ); printf( "\nrows: %g, columns: %g\n\n", rows, col );
  113.  
  114. ////// PART 3)- Find the Median
  115.  
  116. ////// PART 4)- Find the MEAN = average, of the sequence 1+2+3+..n / n
  117. function MxMeanColumn( x )
  118. {
  119. sumX=0;
  120. for( i = 0; i < rows; i++ )
  121. {
  122. sumX += mxSortCol[ i ][ x ]; // sum of Column Χ
  123. }
  124. return MEAN= sumX/rows;
  125. }
  126.  
  127. function MaxNumColumn( x ) // Find the Max Num of the Columns
  128. {
  129. MaxNum=zero=0;
  130. for( i = 0; i < rows; i++ )
  131. {
  132. MaxNum = mxSortCol[ i ][ x ];
  133. }
  134. MaxNumX= Max(MaxNum,zero);
  135. return MaxNumX;
  136. }
  137.  
  138.  
  139. Offset=0.5; tempMin=TempMax=test1=0;
  140. for( j = 0; j < col; j++ ) // loop for every column
  141. {
  142. MaxNumi= MaxNumColumn(j);
  143. if (TempMax<MaxNumi) TempMax=MaxNumi[j];
  144. MaxNum= Max(TempMax,MaxNumi)+Offset; //printf( "\n MaxNum_"+i+ " =\t" + MaxNum );
  145. // Find the Minimum Number of the Columns
  146. MinNumi=mxSortCol[0][j];
  147. if (tempMin>MinNumi) tempMin=MinNumi[j];
  148. MinNum=Min(tempMin,MinNumi)-Offset; //printf("\n MinNum_"+i+ " =\t" + MinNum );
  149. }
  150.  
  151. GfxSetOverlayMode(2); // 2 only low-level graphics
  152. GfxSetTextColor(colorGrey40);
  153. GfxSetBkMode(1); // transparent
  154.  
  155.  
  156. function DrawCircle(x,y,radius,BackColor)
  157. {
  158. GfxSelectSolidBrush( BackColor );
  159. GfxSelectPen( colorYellow );
  160. GfxCircle( x, y, radius );
  161. }
  162.  
  163. YOffset = 35;
  164. XOffset = 35;
  165. /////////////////////////////////////////////
  166. // Draw horizontal GridLines
  167. width = Status( "pxwidth" ) - 4 * XOffset;
  168. height = Status( "pxheight" ) - 2 * YOffset;
  169.  
  170.  
  171. YGrid=XGrid=0;
  172.  
  173. function YDrawLevels( Miny, Maxy ) // horizontal GridLines
  174. {
  175. range = Maxy - Miny;
  176. if( range < 6 ) YGrid=XGrid = 0.5; else
  177. if( range < 11 ) YGrid=XGrid = 1; else
  178. if( range < 30 ) YGrid=XGrid = 2; else
  179. if( range < 51 ) YGrid=XGrid = 5; else
  180. if( range < 101 ) YGrid=XGrid = 10; else
  181. if( range < 201 ) YGrid=XGrid = 20; else
  182. if( range < 501 ) YGrid=XGrid = 50;
  183. GfxSelectPen( colorGrey40, 1, 2 );
  184. for( y = YGrid * ceil( Miny / YGrid ); y <= YGrid * floor( Maxy / YGrid ); y += YGrid )
  185. {
  186. yp = YOffset + Height * ( 1 - ( y - Miny ) / ( Maxy - Miny ) );
  187. GfxMoveTo( XOffset, yp );
  188. GfxLineTo( XOffset + width , yp );
  189. GfxTextOut( "" + y, XOffset + 2 + width, yp );
  190. }
  191.  
  192. GfxSelectPen( colorGrey40, 2, 0 ); //the Frame
  193. GfxMoveTo( XOffset, YOffset );
  194. GfxLineTo( XOffset + width, YOffset ); // Top Line
  195. GfxLineTo( XOffset + width, YOffset + Height ); // Bottom
  196. GfxLineTo( XOffset , YOffset + Height );
  197. GfxLineTo( XOffset , YOffset );
  198.  
  199. }
  200.  
  201. /////////////////////////////////////////////
  202. // Draw horizontal Vertical GridLines
  203. function XDrawLevels( Minx, Maxx ) // Vertical GridLines
  204. {
  205. range = Maxx - Minx;
  206. if( range < 6 ) YGrid=XGrid = 0.5; else
  207. if( range < 11 ) YGrid=XGrid = 1; else
  208. if( range < 30 ) YGrid=XGrid = 2; else
  209. if( range < 51 ) YGrid=XGrid = 5; else
  210. if( range < 101 ) YGrid=XGrid = 10; else
  211. if( range < 201 ) YGrid=XGrid = 20; else
  212. if( range < 501 ) YGrid=XGrid = 50;
  213. GfxSelectPen( colorGrey40, 1, 2 );
  214.  
  215. for( x = XGrid * ceil( Minx / XGrid ); x <= XGrid * floor( Maxx / XGrid ); x += XGrid )
  216. {
  217. xp = XOffset + Width * ( x - Minx ) / ( Maxx - Minx );
  218.  
  219. GfxMoveTo( xp, YOffset );
  220. GfxLineTo( xp, YOffset + height );
  221. GfxTextOut( "" + x, xp - 10, YOffset + 2 + height );
  222. if(DebugOn) _TRACE("#,Vertical GridLines X= "+ x +",XGrid: "+XGrid + ", xpixels "+xp + ", width= "+width+ ", height= "+height );
  223. }
  224.  
  225. GfxSelectPen( colorGrey40, 2, 0 ); //the Frame
  226. GfxMoveTo( XOffset, YOffset );
  227. GfxLineTo( XOffset + width, YOffset ); // Top Line
  228. GfxLineTo( XOffset + width, YOffset + Height ); // Bottom
  229. GfxLineTo( XOffset , YOffset + Height );
  230. GfxLineTo( XOffset , YOffset );
  231. }
  232.  
  233. // For Chart Reading Information
  234. GfxTextOut( "Mean" , width+2*xOffset+12, Height/2 );
  235. DrawCircle(width+2*xOffset,Height/2+8,3,Colorred); // Mean
  236. GfxTextOut( "Median" , width+2*xOffset+12, Height/2+20 );
  237. GfxSelectPen( colorYellow, 1,2 );
  238. GfxMoveTo( width+2*xOffset-10, Height/2+30 ); GfxLineTo( width+2*xOffset, Height/2+30 );
  239.  
  240. if(Onoff==1)xDrawLevels(MinNum,MaxNum);
  241. if(Onoff==0)YDrawLevels(MinNum,MaxNum);
  242. GfxSetTextColor( colorLightGrey );
  243. GfxTextOut( "BoxPlot, Ver 6.1 " , 45, 10 );
  244.  
  245. ///////////////////////////////////////////////////////////////////////
  246. // -------------- Vertical Draw BoxPlot -------------------------------
  247. ////////////////////////////////////////////////////////////////////////
  248.  
  249. function DrawBoxPlotVertical( Q4wkr, Q3wkr, Q3, IQR, Q1,Mean, bar, numbars, Miny, Maxy )
  250. {
  251. BarWidth = ( Status( "pxwidth" ) - 4 * XOffset ) / ( numbars + 0.5 );
  252. BarHeight = Status( "pxheight" ) - 2 * YOffset;
  253. GfxSelectPen( colorYellow, 1,0 );
  254. Ypix1 = YOffset + BarHeight * ( 1 - ( Q1 - Miny ) / ( Maxy - Miny ) ); // Correct BarHeight
  255. Ypix2 = YOffset + BarHeight * ( 1 - ( Q3 - Miny ) / ( Maxy - Miny ) );
  256. Ypix3 = YOffset + BarHeight * ( 1 - ( Q3wkr - Miny ) / ( Maxy - Miny ) );
  257. Ypix4 = YOffset + BarHeight * ( 1 - ( Q4wkr - Miny ) / ( Maxy - Miny ) );
  258. Ypix5 = YOffset + BarHeight * ( 1 - ( IQR - Miny ) / ( Maxy - Miny ) );
  259. Ypix6 = YOffset + BarHeight * ( 1 - ( Mean - Miny ) / ( Maxy - Miny ) );
  260.  
  261. x0 = xp = XOffset + ( bar + 0.5 ) * BarWidth; //x0 θεση που αρχιζει το βαρ ειναι το(+0.5) δηλαδη το παχος του ΒΑΡ μεχρι το (1)
  262. x1 = xe = XOffset + ( bar + 1 ) * BarWidth; //x1 θεση που τελειωνει (+1)
  263. xMiddle = XOffset + ( bar + 0.75 ) * BarWidth; // Middle Box (+1/2)
  264. WLEnd = XOffset + ( bar + 0.70 ) * BarWidth;
  265. WREnd = XOffset + ( bar + 0.80 ) * BarWidth;
  266. //Draw Whisker
  267. GfxMoveTo( xMiddle + 0.5, Ypix3 ); GfxLineTo( xMiddle + 0.5, Ypix4 );
  268. //Draw Top Whiskers end
  269. GfxMoveTo( WLEnd, Ypix4 ); GfxLineTo( WREnd , Ypix4 );
  270. //Draw Bottom end
  271. GfxMoveTo( WLEnd, Ypix3 ); GfxLineTo( WREnd , Ypix3 );
  272. //Draw BoxPlot
  273. //GfxGradientRect( x0, Ypix2, x1, Ypix1, colorPaleBlue, colorPlum );
  274. GfxGradientRect( x0, Ypix2, x1, Ypix1,18, 3 );
  275. //Draw Median, IQR
  276. GfxSelectPen( colorYellow, 1,1 ); GfxMoveTo( x0, Ypix5 ); GfxLineTo( x1, Ypix5 );
  277. //Draw the Mean
  278. DrawCircle(xMiddle,Ypix6,2,Colorred);
  279.  
  280.  
  281. GfxTextOut( "Q3: " + Q3, x0, Ypix2 );
  282. GfxTextOut( "Q1 " + Q1, x0, Ypix1 );
  283.  
  284. if(DebugOn) _TRACE("#,DrawBoxPlotVert, x0 = "+x0 + ",x1 " + x1 + ",Ypix1 "+Ypix1 + ",Ypix2 " + Ypix2);
  285. }
  286.  
  287.  
  288.  
  289. //////////////////////////////////////////////////////////////
  290. // Horizontal /////////// Horizontal /////////// Horizontal /////////// Horizontal
  291. function DrawBoxPlotHorizontal( Q4wkr, Q3wkr, Q3, IQR, Q1, Mean , bar, numbars, Minx, Maxx )
  292. {
  293. BarWidth = Status( "pxwidth" ) - 4 * XOffset ;
  294. BarHeight = ( Status( "pxheight" ) - 2 * YOffset) / ( numbars + 0.5 );
  295. GfxSelectPen( colorYellow, 1, 0 );
  296. // xp = XOffset + Width * ( x - Minx ) / ( Maxx - Minx );
  297. Xpix1 = XOffset + BarWidth * ( Q1 - Minx ) / ( Maxx - Minx ); // Correct BarHeight
  298. Xpix2 = XOffset + BarWidth * ( Q3 - Minx ) / ( Maxx - Minx );
  299. Xpix3 = XOffset + BarWidth * ( Q3wkr - Minx ) / ( Maxx - Minx );
  300. Xpix4 = XOffset + BarWidth * ( Q4wkr - Minx ) / ( Maxx - Minx );
  301. Xpix5 = XOffset + BarWidth * ( IQR - Minx ) / ( Maxx - Minx );
  302. Xpix6 = XOffset + BarWidth * ( Mean - Minx ) / ( Maxx - Minx );
  303. y0 = xp = YOffset + ( bar + 0.5 ) * BarHeight; //y0 θεση που αρχιζει το βαρ ειναι το(+0.5) δηλαδη το παχος του ΒΑΡ μεχρι το (1)
  304. y1 = xe = YOffset + ( bar + 1 ) * BarHeight; //y1 θεση που τελειωνει (+1)
  305. xMiddle = YOffset + ( bar + 0.75 ) * BarHeight; // Middle Box (+1/2)
  306. WLEnd = YOffset + ( bar + 0.70 ) * BarHeight;
  307. WREnd = YOffset + ( bar + 0.80 ) * BarHeight;
  308. //DrawWhisker
  309. GfxMoveTo(Xpix3, xMiddle); GfxLineTo(Xpix4, xMiddle);
  310. //Draw Left Whiskers end
  311. GfxMoveTo( Xpix3, WLEnd ); GfxLineTo( Xpix3,WREnd );
  312. //Draw Right end
  313. GfxMoveTo( Xpix4, WLEnd ); GfxLineTo( Xpix4,WREnd );
  314. //DrawBoxPlot
  315. GfxGradientRect( Xpix1, y0, Xpix2,y1, colorPaleBlue, colorPlum );
  316. //Draw Median, IQR
  317. GfxSelectPen( colorYellow, 1,1 );
  318. GfxMoveTo( Xpix5, y0 );
  319. GfxLineTo( Xpix5, y1 );
  320. //Draw the Mean
  321. DrawCircle(Xpix6,xMiddle,2,Colorred);
  322.  
  323. GfxTextOut( "Q3: " + Q3, Xpix2 ,y0);
  324. GfxTextOut( "Q1: " + Q1, Xpix1 ,y0 );
  325.  
  326. //if(DebugOn) _TRACE("#,DrawBoxPlotHoriz, y0 = "+y0 + ",y1 " + y1 + ",Xpix1 "+Xpix1 + ",Xpix2 " + Xpix2);
  327. }
  328.  
  329.  
  330. //////////////////////////////////////////////////
  331. ////// PART 5) calculate the body and whisker for every column, and draw the boxPlot in the chart
  332. bar=0; MinAvg = MaxAvg = 0;
  333. for( j = 0; j < col; j++ ) // loop for every column
  334. { printf("\n\n --- Box J = "+j);
  335. MeanX= MxMeanColumn( j ); printf( "\n Mean Col "+j+"=\t" + MeanX );
  336. // Find the Median, that is the middle Number of the sequence
  337. Med1 = mxSortCol[rows*0.49][j]; //printf("\nMed1 Col "+j+"=\t "+ Med1 );
  338. Med2 = mxSortCol[rows*0.50][j]; //printf("\nMed2 Col "+j+"=\t "+ Med2 );
  339. Q2 = (Med1+Med2)/2; printf("\nQ2 Median "+j+"=\t "+ Q2 );
  340.  
  341. Q1 = mxSortCol[rows*0.25][j]; printf("\nQ1 Col "+j+"=\t "+ Q1 );
  342. Q3 = mxSortCol[rows*0.74][j]; printf("\nQ3 Col "+j+"=\t "+ Q3 );
  343. Q1whisker = mxSortCol[0][j]; printf("\nQ1whisker Col "+j+"=\t "+ Q1whisker );
  344. Q4whisker = mxSortCol[rows-1][j]; printf("\nQ4whisker Col "+j+"=\t "+ Q4whisker );
  345.  
  346. //printf("\n Q1 = %g,\t\n Q2 = %g\n", Q1, Q2 ); printf("Q3 = %g,\t\nQ1whisker = %g,\t\nQ4whisker = %g\n", Q3, Q1whisker , Q4whisker );
  347. if(DebugOn) _TRACE("#,(Loop) Col: "+J + ", Q1 = "+Q1 + ", Q2= "+Q2+ ", Q3= "+Q3 + ", Q1whisker= "+Q1whisker+ ", Q4whisker= "+Q4whisker);
  348.  
  349. if(Onoff==0) DrawBoxPlotVertical( Q4whisker,Q1whisker,q3,q2,q1,Meanx,bar, numbars=col, Miny=MinNum, Maxy=MaxNum );
  350. if(Onoff==1) DrawBoxPlotHorizontal( Q4whisker,Q1whisker,q3,q2,q1,Meanx,bar, numbars=col, Miny=MinNum, Maxy=MaxNum ) ;
  351. if(DebugOn) _TRACE("#,----------");
  352. bar++;
  353. }
Add Comment
Please, Sign In to add comment