@@ -1228,6 +1228,184 @@ describe('Test box calc', function() {
1228
1228
Plots . doCalcdata ( gd ) ;
1229
1229
return gd . calcdata [ 0 ] ;
1230
1230
}
1231
+
1232
+ it ( 'should compute fence values differently depending on *distribution*' , function ( ) {
1233
+ // Create a dataset that would have a negative lower fence with normal distribution
1234
+ var y = [ 10 , 20 , 30 , 40 , 1000 ] ;
1235
+
1236
+ // Test with normal distribution
1237
+ var cd = _calc ( {
1238
+ y : y ,
1239
+ distribution : 'normal'
1240
+ } ) ;
1241
+ // The normal distribution fence could potentially be negative
1242
+
1243
+ // Test with log-normal distribution
1244
+ var cd2 = _calc ( {
1245
+ y : y ,
1246
+ distribution : 'log-normal'
1247
+ } ) ;
1248
+ // The log-normal lower fence should be higher (not negative)
1249
+ expect ( cd2 [ 0 ] . lf ) . toBeGreaterThan ( 0 , 'log-normal distribution lower fence is positive' ) ;
1250
+
1251
+ // Skip test with negative values as the implementation gracefully handles them via Math.max
1252
+
1253
+ // Test auto distribution on a log axis
1254
+ var cd4 = _calc ( {
1255
+ y : y ,
1256
+ distribution : 'auto'
1257
+ } , {
1258
+ yaxis : { type : 'log' }
1259
+ } ) ;
1260
+ // Should use log-normal distribution
1261
+ expect ( cd4 [ 0 ] . lf ) . toBeGreaterThan ( 0 , 'auto distribution on log axis' ) ;
1262
+ expect ( cd4 [ 0 ] . lf ) . toBeCloseTo ( cd2 [ 0 ] . lf , 6 , 'auto distribution equals log-normal on log axis' ) ;
1263
+ } ) ;
1264
+
1265
+ it ( 'should prevent negative whiskers with log-normal distribution' , function ( ) {
1266
+ // This dataset would produce negative lower fence with normal distribution calculation
1267
+ // (but the implementation will clamp to the minimum value)
1268
+ var dataset = [ 2 , 3 , 5 , 10 , 200 ] ;
1269
+
1270
+ // Calculate with normal distribution
1271
+ var cdNormal = _calc ( {
1272
+ y : dataset ,
1273
+ distribution : 'normal'
1274
+ } ) ;
1275
+
1276
+ // Calculate with log-normal distribution
1277
+ var cdLogNormal = _calc ( {
1278
+ y : dataset ,
1279
+ distribution : 'log-normal'
1280
+ } ) ;
1281
+
1282
+ // Verify log-normal lower fence is positive
1283
+ expect ( cdLogNormal [ 0 ] . lf ) . toBeGreaterThan ( 0 , 'log-normal lower fence is positive' ) ;
1284
+ } ) ;
1285
+
1286
+ it ( 'should set usesLogNormal flag correctly for log-normal distribution' , function ( ) {
1287
+ // Use a typical log-normally distributed dataset
1288
+ var dataset = [ 1 , 2 , 5 , 10 , 20 , 50 , 100 ] ;
1289
+
1290
+ var cd = _calc ( {
1291
+ y : dataset ,
1292
+ distribution : 'log-normal'
1293
+ } ) ;
1294
+
1295
+ // Verify the usesLogNormal flag is set
1296
+ expect ( cd [ 0 ] . usesLogNormal ) . toBe ( true , 'usesLogNormal flag is set for log-normal distribution' ) ;
1297
+
1298
+ // Check that the fence values are reasonable
1299
+ expect ( cd [ 0 ] . lf ) . toBeGreaterThan ( 0 , 'log-normal lower fence is positive' ) ;
1300
+ expect ( cd [ 0 ] . lf ) . toBeLessThan ( cd [ 0 ] . q1 , 'lower fence is less than q1' ) ;
1301
+ expect ( cd [ 0 ] . uf ) . toBeGreaterThan ( cd [ 0 ] . q3 , 'upper fence is greater than q3' ) ;
1302
+ } ) ;
1303
+
1304
+ it ( 'should use correct distribution mode for auto setting' , function ( ) {
1305
+ var dataset = [ 1 , 2 , 5 , 10 , 20 , 50 , 100 ] ;
1306
+
1307
+ // Test on linear axis
1308
+ var cdLinear = _calc ( {
1309
+ y : dataset ,
1310
+ distribution : 'auto'
1311
+ } , {
1312
+ yaxis : { type : 'linear' }
1313
+ } ) ;
1314
+
1315
+ // Calculate with explicitly set normal distribution
1316
+ var cdNormal = _calc ( {
1317
+ y : dataset ,
1318
+ distribution : 'normal'
1319
+ } ) ;
1320
+
1321
+ // Verify auto on linear axis uses normal distribution
1322
+ expect ( cdLinear [ 0 ] . lf ) . toBeCloseTo ( cdNormal [ 0 ] . lf , 6 , 'auto distribution equals normal on linear axis' ) ;
1323
+ expect ( cdLinear [ 0 ] . uf ) . toBeCloseTo ( cdNormal [ 0 ] . uf , 6 , 'auto distribution equals normal on linear axis' ) ;
1324
+
1325
+ // Test on log axis
1326
+ var cdLog = _calc ( {
1327
+ y : dataset ,
1328
+ distribution : 'auto'
1329
+ } , {
1330
+ yaxis : { type : 'log' }
1331
+ } ) ;
1332
+
1333
+ // Calculate with explicitly set log-normal distribution
1334
+ var cdLogNormal = _calc ( {
1335
+ y : dataset ,
1336
+ distribution : 'log-normal'
1337
+ } ) ;
1338
+
1339
+ // Verify auto on log axis uses log-normal distribution
1340
+ expect ( cdLog [ 0 ] . lf ) . toBeCloseTo ( cdLogNormal [ 0 ] . lf , 6 , 'auto distribution equals log-normal on log axis' ) ;
1341
+ expect ( cdLog [ 0 ] . uf ) . toBeCloseTo ( cdLogNormal [ 0 ] . uf , 6 , 'auto distribution equals log-normal on log axis' ) ;
1342
+ } ) ;
1343
+
1344
+ it ( 'should correctly handle explicit fence values' , function ( ) {
1345
+ var dataset = [ 1 , 2 , 5 , 10 , 20 , 50 , 100 ] ;
1346
+
1347
+ // With normal distribution and no explicit fences (baseline)
1348
+ var cdNormalBaseline = _calc ( {
1349
+ y : dataset ,
1350
+ distribution : 'normal'
1351
+ } ) ;
1352
+
1353
+ // With log-normal distribution and no explicit fences (baseline)
1354
+ var cdLogNormalBaseline = _calc ( {
1355
+ y : dataset ,
1356
+ distribution : 'log-normal'
1357
+ } ) ;
1358
+
1359
+ // Fence values must be valid (>= q1 and <= q3)
1360
+ var validLowerFence = cdNormalBaseline [ 0 ] . q1 ;
1361
+ var validUpperFence = cdNormalBaseline [ 0 ] . q3 ;
1362
+
1363
+ // With normal distribution and valid explicit fences
1364
+ var cdNormal = _calc ( {
1365
+ y : dataset ,
1366
+ distribution : 'normal' ,
1367
+ lowerfence : [ validLowerFence ] ,
1368
+ upperfence : [ validUpperFence ]
1369
+ } ) ;
1370
+
1371
+ // With log-normal distribution and valid explicit fences
1372
+ var cdLogNormal = _calc ( {
1373
+ y : dataset ,
1374
+ distribution : 'log-normal' ,
1375
+ lowerfence : [ validLowerFence ] ,
1376
+ upperfence : [ validUpperFence ]
1377
+ } ) ;
1378
+
1379
+ // Verify explicit fence values are used when valid
1380
+ expect ( cdNormal [ 0 ] . lf ) . toEqual ( validLowerFence , 'normal distribution uses valid explicit lower fence' ) ;
1381
+ expect ( cdNormal [ 0 ] . uf ) . toEqual ( validUpperFence , 'normal distribution uses valid explicit upper fence' ) ;
1382
+ expect ( cdLogNormal [ 0 ] . lf ) . toEqual ( validLowerFence , 'log-normal distribution uses valid explicit lower fence' ) ;
1383
+ expect ( cdLogNormal [ 0 ] . uf ) . toEqual ( validUpperFence , 'log-normal distribution uses valid explicit upper fence' ) ;
1384
+ } ) ;
1385
+
1386
+ it ( 'should handle extreme data distributions correctly' , function ( ) {
1387
+ // Very skewed dataset that would have strongly negative whiskers with normal distribution
1388
+ var extremeDataset = [ 1 , 2 , 3 , 4 , 5 , 1000 , 2000 , 5000 ] ;
1389
+
1390
+ // With normal distribution
1391
+ var cdNormal = _calc ( {
1392
+ y : extremeDataset ,
1393
+ distribution : 'normal'
1394
+ } ) ;
1395
+
1396
+ // With log-normal distribution
1397
+ var cdLogNormal = _calc ( {
1398
+ y : extremeDataset ,
1399
+ distribution : 'log-normal'
1400
+ } ) ;
1401
+
1402
+ // Verify log-normal gives reasonable positive whiskers
1403
+ expect ( cdLogNormal [ 0 ] . lf ) . toBeGreaterThan ( 0 , 'log-normal gives positive lower fence for extreme data' ) ;
1404
+
1405
+ // Verify usesLogNormal flag is set correctly
1406
+ expect ( cdNormal [ 0 ] . usesLogNormal ) . toBe ( false , 'normal distribution sets flag to false' ) ;
1407
+ expect ( cdLogNormal [ 0 ] . usesLogNormal ) . toBe ( true , 'log-normal distribution sets flag to true' ) ;
1408
+ } ) ;
1231
1409
1232
1410
it ( 'should compute q1/q3 depending on *quartilemethod*' , function ( ) {
1233
1411
// samples from https://en.wikipedia.org/wiki/Quartile
0 commit comments