Skip to content

Commit 21bf843

Browse files
committed
update calendar options, examples and unit test
1 parent e42869d commit 21bf843

5 files changed

Lines changed: 190 additions & 5 deletions

File tree

example/calendar_example.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,39 @@ def calendar_base() -> Calendar:
3535
return c
3636

3737

38+
@C.funcs
39+
def calendar_label_setting() -> Calendar:
40+
begin = datetime.date(2017, 1, 1)
41+
end = datetime.date(2017, 12, 31)
42+
data = [
43+
[str(begin + datetime.timedelta(days=i)), random.randint(1000, 25000)]
44+
for i in range((end - begin).days + 1)
45+
]
46+
47+
c = (
48+
Calendar()
49+
.add(
50+
"",
51+
data,
52+
calendar_opts=opts.CalendarOpts(
53+
range_="2017",
54+
daylabel_opts=opts.CalendarDayLabelOpts(name_map="cn"),
55+
monthlabel_opts=opts.CalendarMonthLabelOpts(name_map="cn"),
56+
),
57+
)
58+
.set_global_opts(
59+
title_opts=opts.TitleOpts(title="Calendar-2017年微信步数情况(中文 Label)"),
60+
visualmap_opts=opts.VisualMapOpts(
61+
max_=20000,
62+
min_=500,
63+
orient="horizontal",
64+
is_piecewise=True,
65+
pos_top="230px",
66+
pos_left="100px",
67+
),
68+
)
69+
)
70+
return c
71+
72+
3873
Page().add(*[fn() for fn, _ in C.charts]).render()

pyecharts/options/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
AxisTickOpts,
3838
BrushOpts,
3939
CalendarOpts,
40+
CalendarDayLabelOpts,
41+
CalendarMonthLabelOpts,
42+
CalendarYearLabelOpts,
4043
DataZoomOpts,
4144
Grid3DOpts,
4245
GridOpts,

pyecharts/options/global_options.py

Lines changed: 109 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from ..globals import CurrentConfig, RenderType, ThemeType
22
from ..options.series_options import (
33
BasicOpts,
4+
ItemStyleOpts,
45
JSFunc,
56
LabelOpts,
67
LineStyleOpts,
@@ -618,26 +619,130 @@ def __init__(
618619
self.opts: dict = {"name": name, "max": max_, "min": min_, "color": color}
619620

620621

622+
class CalendarDayLabelOpts(BasicOpts):
623+
def __init__(
624+
self,
625+
is_show: bool = True,
626+
first_day: int = 0,
627+
margin: Optional[int] = None,
628+
position: str = "start",
629+
name_map: Union[str, Sequence] = "en",
630+
label_color: str = "#000",
631+
label_font_style: str = "normal",
632+
label_font_weight: str = "normal",
633+
label_font_family: str = "sans-serif",
634+
label_font_size: int = 12,
635+
align: Optional[str] = None,
636+
vertical_align: Optional[str] = None,
637+
):
638+
self.opts: dict = {
639+
"show": is_show,
640+
"firstDay": first_day,
641+
"margin": margin,
642+
"position": position,
643+
"nameMap": name_map,
644+
"color": label_color,
645+
"fontStyle": label_font_style,
646+
"fontWeight": label_font_weight,
647+
"fontFamily": label_font_family,
648+
"fontSize": label_font_size,
649+
"align": align,
650+
"verticalAlign": vertical_align,
651+
}
652+
653+
654+
class CalendarMonthLabelOpts(BasicOpts):
655+
def __init__(
656+
self,
657+
is_show: bool = True,
658+
align: Optional[str] = None,
659+
margin: Optional[int] = None,
660+
position: str = "start",
661+
name_map: Union[str, Sequence] = "en",
662+
formatter: JSFunc = None,
663+
label_color: str = "#000",
664+
label_font_style: str = "normal",
665+
label_font_weight: str = "normal",
666+
label_font_family: str = "sans-serif",
667+
label_font_size: int = 12,
668+
vertical_align: Optional[str] = None,
669+
):
670+
self.opts: dict = {
671+
"show": is_show,
672+
"align": align,
673+
"margin": margin,
674+
"position": position,
675+
"nameMap": name_map,
676+
"formatter": formatter,
677+
"color": label_color,
678+
"fontStyle": label_font_style,
679+
"fontWeight": label_font_weight,
680+
"fontFamily": label_font_family,
681+
"fontSize": label_font_size,
682+
"verticalAlign": vertical_align,
683+
}
684+
685+
686+
class CalendarYearLabelOpts(BasicOpts):
687+
def __init__(
688+
self,
689+
is_show: bool = True,
690+
margin: Optional[int] = None,
691+
position: Optional[str] = None,
692+
formatter: JSFunc = None,
693+
label_color: str = "#000",
694+
label_font_style: str = "normal",
695+
label_font_weight: str = "normal",
696+
label_font_family: str = "sans-serif",
697+
label_font_size: int = 12,
698+
align: Optional[str] = None,
699+
vertical_align: Optional[str] = None,
700+
):
701+
self.opts: dict = {
702+
"show": is_show,
703+
"margin": margin,
704+
"position": position,
705+
"formatter": formatter,
706+
"color": label_color,
707+
"fontStyle": label_font_style,
708+
"fontWeight": label_font_weight,
709+
"fontFamily": label_font_family,
710+
"fontSize": label_font_size,
711+
"align": align,
712+
"verticalAlign": vertical_align,
713+
}
714+
715+
621716
class CalendarOpts(BasicOpts):
622717
def __init__(
623718
self,
624719
pos_left: Optional[str] = None,
625720
pos_top: Optional[str] = None,
626721
pos_right: Optional[str] = None,
627722
pos_bottom: Optional[str] = None,
628-
orient: Optional[str] = None,
723+
width: Optional[str] = "auto",
724+
height: Optional[str] = None,
725+
orient: Optional[str] = "horizontal",
629726
range_: Union[str, Sequence, int] = None,
630-
daylabel_opts: Union[LabelOpts, dict, None] = None,
631-
monthlabel_opts: Union[LabelOpts, dict, None] = None,
632-
yearlabel_opts: Union[LabelOpts, dict, None] = None,
727+
cell_size: Union[int, Sequence] = 20,
728+
splitline_opts: Union[SplitLineOpts, dict, None] = None,
729+
itemstyle_opts: Union[ItemStyleOpts, dict, None] = None,
730+
daylabel_opts: Union[CalendarDayLabelOpts, dict, None] = None,
731+
monthlabel_opts: Union[CalendarMonthLabelOpts, dict, None] = None,
732+
yearlabel_opts: Union[CalendarYearLabelOpts, dict, None] = None,
633733
):
634734
self.opts: dict = {
635735
"left": pos_left,
636736
"top": pos_top,
637737
"right": pos_right,
638738
"bottom": pos_bottom,
739+
"width": width,
740+
"height": height,
639741
"orient": orient,
640742
"range": range_,
743+
"cellSize": cell_size,
744+
"splitLine": splitline_opts,
745+
"itemStyle": itemstyle_opts,
641746
"dayLabel": daylabel_opts,
642747
"monthLabel": monthlabel_opts,
643748
"yearLabel": yearlabel_opts,

pyecharts/types.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
VisualMap = Union[_VisualMapType, Sequence[_VisualMapType], None]
4747

4848
Calendar = Union[opts.CalendarOpts, dict, None]
49+
CalendarDayLabelOpts = Union[opts.CalendarDayLabelOpts, dict, None]
50+
CalendarMonthLabelOpts = Union[opts.CalendarMonthLabelOpts, dict, None]
51+
calendarYearLabelOpts = Union[opts.CalendarYearLabelOpts, dict, None]
4952

5053
GraphNode = Union[opts.GraphNode, dict]
5154
GraphLink = Union[opts.GraphLink, dict]

test/test_calendar.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import random
33
from unittest.mock import patch
44

5-
from nose.tools import assert_equal
5+
from nose.tools import assert_equal, assert_in
66

77
from pyecharts import options as opts
88
from pyecharts.charts import Calendar
@@ -35,3 +35,42 @@ def test_calendar_base(fake_writer):
3535
_, content = fake_writer.call_args[0]
3636
assert_equal(c.theme, "white")
3737
assert_equal(c.renderer, "canvas")
38+
39+
40+
@patch("pyecharts.render.engine.write_utf8_html_file")
41+
def test_calendar_setting(fake_writer):
42+
begin = datetime.date(2017, 1, 1)
43+
end = datetime.date(2017, 12, 31)
44+
data = [
45+
[str(begin + datetime.timedelta(days=i)), random.randint(1000, 25000)]
46+
for i in range((end - begin).days + 1)
47+
]
48+
49+
c = (
50+
Calendar()
51+
.add(
52+
"",
53+
data,
54+
calendar_opts=opts.CalendarOpts(
55+
range_="2017",
56+
cell_size=15,
57+
daylabel_opts=opts.CalendarDayLabelOpts(name_map="cn"),
58+
monthlabel_opts=opts.CalendarMonthLabelOpts(name_map="cn"),
59+
),
60+
)
61+
.set_global_opts(
62+
visualmap_opts=opts.VisualMapOpts(
63+
max_=20000,
64+
min_=500,
65+
orient="horizontal",
66+
is_piecewise=True,
67+
pos_top="230px",
68+
pos_left="100px",
69+
)
70+
)
71+
)
72+
c.render()
73+
_, content = fake_writer.call_args[0]
74+
assert_in("cellSize", content)
75+
assert_in("dayLabel", content)
76+
assert_in("monthLabel", content)

0 commit comments

Comments
 (0)