@@ -286,7 +286,7 @@ def test_empty(self):
286
286
{'props' : [['' , '' ]], 'selector' : 'row1_col0' }]
287
287
self .assertEqual (result , expected )
288
288
289
- def test_bar (self ):
289
+ def test_bar_align_left (self ):
290
290
df = pd .DataFrame ({'A' : [0 , 1 , 2 ]})
291
291
result = df .style .bar ()._compute ().ctx
292
292
expected = {
@@ -319,7 +319,7 @@ def test_bar(self):
319
319
result = df .style .bar (color = 'red' , width = 50 )._compute ().ctx
320
320
self .assertEqual (result , expected )
321
321
322
- def test_bar_0points (self ):
322
+ def test_bar_align_left_0points (self ):
323
323
df = pd .DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ], [7 , 8 , 9 ]])
324
324
result = df .style .bar ()._compute ().ctx
325
325
expected = {(0 , 0 ): ['width: 10em' , ' height: 80%' ],
@@ -369,6 +369,89 @@ def test_bar_0points(self):
369
369
', transparent 0%)' ]}
370
370
self .assertEqual (result , expected )
371
371
372
+ def test_bar_align_zero_pos_and_neg (self ):
373
+ df = pd .DataFrame ({'A' : [- 10 , 0 , 20 , 90 ]})
374
+
375
+ result = df .style .bar (align = 'zero' , color = [
376
+ '#d65f5f' , '#5fba7d' ], width = 90 )._compute ().ctx
377
+
378
+ expected = {(0 , 0 ): ['width: 10em' ,
379
+ ' height: 80%' ,
380
+ 'background: linear-gradient(90deg, transparent 0%, transparent 45.0%, #d65f5f 45.0%, #d65f5f 50%, transparent 50%)' ],
381
+ (1 , 0 ): ['width: 10em' ,
382
+ ' height: 80%' ,
383
+ 'background: linear-gradient(90deg, transparent 0%, transparent 50%, #5fba7d 50%, #5fba7d 50.0%, transparent 50.0%)' ],
384
+ (2 , 0 ): ['width: 10em' ,
385
+ ' height: 80%' ,
386
+ 'background: linear-gradient(90deg, transparent 0%, transparent 50%, #5fba7d 50%, #5fba7d 60.0%, transparent 60.0%)' ],
387
+ (3 , 0 ): ['width: 10em' ,
388
+ ' height: 80%' ,
389
+ 'background: linear-gradient(90deg, transparent 0%, transparent 50%, #5fba7d 50%, #5fba7d 95.0%, transparent 95.0%)' ]}
390
+ self .assertEqual (result , expected )
391
+
392
+ def test_bar_align_mid_pos_and_neg (self ):
393
+ df = pd .DataFrame ({'A' : [- 10 , 0 , 20 , 90 ]})
394
+
395
+ result = df .style .bar (align = 'mid' , color = [
396
+ '#d65f5f' , '#5fba7d' ])._compute ().ctx
397
+
398
+ expected = {(0 , 0 ): ['width: 10em' ,
399
+ ' height: 80%' ,
400
+ 'background: linear-gradient(90deg, transparent 0%, transparent 0.0%, #d65f5f 0.0%, #d65f5f 10.0%, transparent 10.0%)' ],
401
+ (1 , 0 ): ['width: 10em' ,
402
+ ' height: 80%' ,
403
+ 'background: linear-gradient(90deg, transparent 0%, transparent 10.0%, #d65f5f 10.0%, #d65f5f 10.0%, transparent 10.0%)' ],
404
+ (2 , 0 ): ['width: 10em' ,
405
+ ' height: 80%' ,
406
+ 'background: linear-gradient(90deg, transparent 0%, transparent 10.0%, #5fba7d 10.0%, #5fba7d 30.0%, transparent 30.0%)' ],
407
+ (3 , 0 ): ['width: 10em' ,
408
+ ' height: 80%' ,
409
+ 'background: linear-gradient(90deg, transparent 0%, transparent 10.0%, #5fba7d 10.0%, #5fba7d 100.0%, transparent 100.0%)' ]}
410
+
411
+ self .assertEqual (result , expected )
412
+
413
+ def test_bar_align_mid_all_pos (self ):
414
+ df = pd .DataFrame ({'A' : [10 , 20 , 50 , 100 ]})
415
+
416
+ result = df .style .bar (align = 'mid' , color = [
417
+ '#d65f5f' , '#5fba7d' ])._compute ().ctx
418
+
419
+ expected = {(0 , 0 ): ['width: 10em' ,
420
+ ' height: 80%' ,
421
+ 'background: linear-gradient(90deg, transparent 0%, transparent 0%, #5fba7d 0%, #5fba7d 10.0%, transparent 10.0%)' ],
422
+ (1 , 0 ): ['width: 10em' ,
423
+ ' height: 80%' ,
424
+ 'background: linear-gradient(90deg, transparent 0%, transparent 0%, #5fba7d 0%, #5fba7d 20.0%, transparent 20.0%)' ],
425
+ (2 , 0 ): ['width: 10em' ,
426
+ ' height: 80%' ,
427
+ 'background: linear-gradient(90deg, transparent 0%, transparent 0%, #5fba7d 0%, #5fba7d 50.0%, transparent 50.0%)' ],
428
+ (3 , 0 ): ['width: 10em' ,
429
+ ' height: 80%' ,
430
+ 'background: linear-gradient(90deg, transparent 0%, transparent 0%, #5fba7d 0%, #5fba7d 100.0%, transparent 100.0%)' ]}
431
+
432
+ self .assertEqual (result , expected )
433
+
434
+ def test_bar_align_mid_all_neg (self ):
435
+ df = pd .DataFrame ({'A' : [- 100 , - 60 , - 30 , - 20 ]})
436
+
437
+ result = df .style .bar (align = 'mid' , color = [
438
+ '#d65f5f' , '#5fba7d' ])._compute ().ctx
439
+
440
+ expected = {(0 , 0 ): ['width: 10em' ,
441
+ ' height: 80%' ,
442
+ 'background: linear-gradient(90deg, transparent 0%, transparent 0.0%, #d65f5f 0.0%, #d65f5f 100%, transparent 100%)' ],
443
+ (1 , 0 ): ['width: 10em' ,
444
+ ' height: 80%' ,
445
+ 'background: linear-gradient(90deg, transparent 0%, transparent 40.0%, #d65f5f 40.0%, #d65f5f 100%, transparent 100%)' ],
446
+ (2 , 0 ): ['width: 10em' ,
447
+ ' height: 80%' ,
448
+ 'background: linear-gradient(90deg, transparent 0%, transparent 70.0%, #d65f5f 70.0%, #d65f5f 100%, transparent 100%)' ],
449
+ (3 , 0 ): ['width: 10em' ,
450
+ ' height: 80%' ,
451
+ 'background: linear-gradient(90deg, transparent 0%, transparent 80.0%, #d65f5f 80.0%, #d65f5f 100%, transparent 100%)' ]}
452
+
453
+ self .assertEqual (result , expected )
454
+
372
455
def test_highlight_null (self , null_color = 'red' ):
373
456
df = pd .DataFrame ({'A' : [0 , np .nan ]})
374
457
result = df .style .highlight_null ()._compute ().ctx
0 commit comments