@@ -427,3 +427,79 @@ def test_all_annotation_positions():
427427
428428if __name__ == "__main__" :
429429 draw_all_annotation_positions ()
430+
431+
432+ # Tests for datetime axis annotation support (issue #3065)
433+ import datetime
434+
435+
436+ def test_vline_datetime_string_annotation ():
437+ """add_vline with annotation_text on datetime x-axis should not crash."""
438+ fig = go .Figure ()
439+ fig .add_trace (go .Scatter (x = ["2018-01-01" , "2018-06-01" , "2018-12-31" ], y = [1 , 2 , 3 ]))
440+ fig .add_vline (x = "2018-09-24" , annotation_text = "test" )
441+ assert len (fig .layout .annotations ) == 1
442+ assert fig .layout .annotations [0 ].text == "test"
443+ assert fig .layout .annotations [0 ].x == "2018-09-24"
444+
445+
446+ def test_hline_with_datetime_xaxis ():
447+ """numeric add_hline should still work with datetime x-axis."""
448+ fig = go .Figure ()
449+ fig .add_trace (go .Scatter (x = ["2018-01-01" , "2018-06-01" , "2018-12-31" ], y = [1 , 2 , 3 ]))
450+ fig .add_hline (y = 2 , annotation_text = "hline test" )
451+ assert len (fig .layout .annotations ) == 1
452+ assert fig .layout .annotations [0 ].text == "hline test"
453+ assert fig .layout .annotations [0 ].y == 2
454+
455+
456+ def test_vrect_datetime_string_annotation ():
457+ """add_vrect with annotation_text on datetime x-axis should not crash."""
458+ fig = go .Figure ()
459+ fig .add_trace (go .Scatter (x = ["2018-01-01" , "2018-06-01" , "2018-12-31" ], y = [1 , 2 , 3 ]))
460+ fig .add_vrect (x0 = "2018-03-01" , x1 = "2018-09-01" , annotation_text = "rect test" )
461+ assert len (fig .layout .annotations ) == 1
462+ assert fig .layout .annotations [0 ].text == "rect test"
463+ assert fig .layout .annotations [0 ].x == "2018-09-01"
464+
465+
466+ def test_vline_datetime_object_annotation ():
467+ """add_vline with datetime.datetime object should not crash."""
468+ fig = go .Figure ()
469+ fig .add_trace (
470+ go .Scatter (
471+ x = [
472+ datetime .datetime (2018 , 1 , 1 ),
473+ datetime .datetime (2018 , 6 , 1 ),
474+ datetime .datetime (2018 , 12 , 31 ),
475+ ],
476+ y = [1 , 2 , 3 ],
477+ )
478+ )
479+ fig .add_vline (x = datetime .datetime (2018 , 9 , 24 ), annotation_text = "dt test" )
480+ assert len (fig .layout .annotations ) == 1
481+ assert fig .layout .annotations [0 ].text == "dt test"
482+ assert fig .layout .annotations [0 ].x == datetime .datetime (2018 , 9 , 24 , 0 , 0 )
483+
484+
485+ def test_vrect_datetime_object_annotation ():
486+ """add_vrect with datetime.datetime objects should compute correct mean."""
487+ fig = go .Figure ()
488+ fig .add_trace (
489+ go .Scatter (
490+ x = [
491+ datetime .datetime (2018 , 1 , 1 ),
492+ datetime .datetime (2018 , 6 , 1 ),
493+ datetime .datetime (2018 , 12 , 31 ),
494+ ],
495+ y = [1 , 2 , 3 ],
496+ )
497+ )
498+ fig .add_vrect (
499+ x0 = datetime .datetime (2018 , 3 , 1 ),
500+ x1 = datetime .datetime (2018 , 9 , 1 ),
501+ annotation_text = "rect dt test" ,
502+ )
503+ assert len (fig .layout .annotations ) == 1
504+ assert fig .layout .annotations [0 ].text == "rect dt test"
505+ assert fig .layout .annotations [0 ].x == datetime .datetime (2018 , 9 , 1 )
0 commit comments