@@ -265,7 +265,7 @@ def test_empty(self):
265
265
{'props' : [['' , '' ]], 'selector' : 'row1_col0' }]
266
266
self .assertEqual (result , expected )
267
267
268
- def test_bar (self ):
268
+ def test_bar_align_left (self ):
269
269
df = pd .DataFrame ({'A' : [0 , 1 , 2 ]})
270
270
result = df .style .bar ()._compute ().ctx
271
271
expected = {
@@ -298,7 +298,7 @@ def test_bar(self):
298
298
result = df .style .bar (color = 'red' , width = 50 )._compute ().ctx
299
299
self .assertEqual (result , expected )
300
300
301
- def test_bar_0points (self ):
301
+ def test_bar_align_left_0points (self ):
302
302
df = pd .DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ], [7 , 8 , 9 ]])
303
303
result = df .style .bar ()._compute ().ctx
304
304
expected = {(0 , 0 ): ['width: 10em' , ' height: 80%' ],
@@ -348,6 +348,89 @@ def test_bar_0points(self):
348
348
', transparent 0%)' ]}
349
349
self .assertEqual (result , expected )
350
350
351
+ def test_bar_align_zero_pos_and_neg (self ):
352
+ df = pd .DataFrame ({'A' : [- 10 , 0 , 20 , 90 ]})
353
+
354
+ result = df .style .bar (align = 'zero' , color = [
355
+ '#d65f5f' , '#5fba7d' ], width = 90 )._compute ().ctx
356
+
357
+ expected = {(0 , 0 ): ['width: 10em' ,
358
+ ' height: 80%' ,
359
+ 'background: linear-gradient(90deg, transparent 0%, transparent 45.0%, #d65f5f 45.0%, #d65f5f 50%, transparent 50%)' ],
360
+ (1 , 0 ): ['width: 10em' ,
361
+ ' height: 80%' ,
362
+ 'background: linear-gradient(90deg, transparent 0%, transparent 50%, #5fba7d 50%, #5fba7d 50.0%, transparent 50.0%)' ],
363
+ (2 , 0 ): ['width: 10em' ,
364
+ ' height: 80%' ,
365
+ 'background: linear-gradient(90deg, transparent 0%, transparent 50%, #5fba7d 50%, #5fba7d 60.0%, transparent 60.0%)' ],
366
+ (3 , 0 ): ['width: 10em' ,
367
+ ' height: 80%' ,
368
+ 'background: linear-gradient(90deg, transparent 0%, transparent 50%, #5fba7d 50%, #5fba7d 95.0%, transparent 95.0%)' ]}
369
+ self .assertEqual (result , expected )
370
+
371
+ def test_bar_align_mid_pos_and_neg (self ):
372
+ df = pd .DataFrame ({'A' : [- 10 , 0 , 20 , 90 ]})
373
+
374
+ result = df .style .bar (align = 'mid' , color = [
375
+ '#d65f5f' , '#5fba7d' ])._compute ().ctx
376
+
377
+ expected = {(0 , 0 ): ['width: 10em' ,
378
+ ' height: 80%' ,
379
+ 'background: linear-gradient(90deg, transparent 0%, transparent 0.0%, #d65f5f 0.0%, #d65f5f 10.0%, transparent 10.0%)' ],
380
+ (1 , 0 ): ['width: 10em' ,
381
+ ' height: 80%' ,
382
+ 'background: linear-gradient(90deg, transparent 0%, transparent 10.0%, #d65f5f 10.0%, #d65f5f 10.0%, transparent 10.0%)' ],
383
+ (2 , 0 ): ['width: 10em' ,
384
+ ' height: 80%' ,
385
+ 'background: linear-gradient(90deg, transparent 0%, transparent 10.0%, #5fba7d 10.0%, #5fba7d 30.0%, transparent 30.0%)' ],
386
+ (3 , 0 ): ['width: 10em' ,
387
+ ' height: 80%' ,
388
+ 'background: linear-gradient(90deg, transparent 0%, transparent 10.0%, #5fba7d 10.0%, #5fba7d 100.0%, transparent 100.0%)' ]}
389
+
390
+ self .assertEqual (result , expected )
391
+
392
+ def test_bar_align_mid_all_pos (self ):
393
+ df = pd .DataFrame ({'A' : [10 , 20 , 50 , 100 ]})
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%, #5fba7d 0%, #5fba7d 10.0%, transparent 10.0%)' ],
401
+ (1 , 0 ): ['width: 10em' ,
402
+ ' height: 80%' ,
403
+ 'background: linear-gradient(90deg, transparent 0%, transparent 0%, #5fba7d 0%, #5fba7d 20.0%, transparent 20.0%)' ],
404
+ (2 , 0 ): ['width: 10em' ,
405
+ ' height: 80%' ,
406
+ 'background: linear-gradient(90deg, transparent 0%, transparent 0%, #5fba7d 0%, #5fba7d 50.0%, transparent 50.0%)' ],
407
+ (3 , 0 ): ['width: 10em' ,
408
+ ' height: 80%' ,
409
+ 'background: linear-gradient(90deg, transparent 0%, transparent 0%, #5fba7d 0%, #5fba7d 100.0%, transparent 100.0%)' ]}
410
+
411
+ self .assertEqual (result , expected )
412
+
413
+ def test_bar_align_mid_all_neg (self ):
414
+ df = pd .DataFrame ({'A' : [- 100 , - 60 , - 30 , - 20 ]})
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.0%, #d65f5f 0.0%, #d65f5f 100%, transparent 100%)' ],
422
+ (1 , 0 ): ['width: 10em' ,
423
+ ' height: 80%' ,
424
+ 'background: linear-gradient(90deg, transparent 0%, transparent 40.0%, #d65f5f 40.0%, #d65f5f 100%, transparent 100%)' ],
425
+ (2 , 0 ): ['width: 10em' ,
426
+ ' height: 80%' ,
427
+ 'background: linear-gradient(90deg, transparent 0%, transparent 70.0%, #d65f5f 70.0%, #d65f5f 100%, transparent 100%)' ],
428
+ (3 , 0 ): ['width: 10em' ,
429
+ ' height: 80%' ,
430
+ 'background: linear-gradient(90deg, transparent 0%, transparent 80.0%, #d65f5f 80.0%, #d65f5f 100%, transparent 100%)' ]}
431
+
432
+ self .assertEqual (result , expected )
433
+
351
434
def test_highlight_null (self , null_color = 'red' ):
352
435
df = pd .DataFrame ({'A' : [0 , np .nan ]})
353
436
result = df .style .highlight_null ()._compute ().ctx
0 commit comments