Coverage for app / documents / visuals / axes.py: 100%

78 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-10-31 11:23 +0000

1"""The plot area of a chart or plot: its value axis, grid and labels.""" 

2 

3from __future__ import annotations 

4 

5import math 

6from dataclasses import dataclass 

7 

8from .primitives import label, rule 

9from .scales import LinearScale, Ticks 

10from .text import ellipsize, format_tick, measure 

11from .theme import LABEL, LABEL_SIZE, MUTED, PX, RULE, TICK_SIZE, Theme 

12 

13#: recharts' grid: ``strokeDasharray="3 3"`` in ``#e5e7eb``. 

14GRID_DASH = (3 * PX, 3 * PX) 

15#: How far a category label is turned when the labels do not fit flat. 

16TURN = 35.0 

17#: The longest a turned category label may run. 

18TURNED_LABEL_MAX = 72.0 

19 

20 

21@dataclass(frozen=True) 

22class Area: 

23 """Where the data is drawn, in the drawing's points (y up).""" 

24 

25 left: float 

26 bottom: float 

27 width: float 

28 height: float 

29 

30 @property 

31 def right(self) -> float: 

32 return self.left + self.width 

33 

34 @property 

35 def top(self) -> float: 

36 return self.bottom + self.height 

37 

38 

39def tick_labels(ticks: Ticks) -> list[str]: 

40 return [format_tick(v, ticks.decimals) for v in ticks.values] 

41 

42 

43def value_axis_width(ticks: Ticks, theme: Theme) -> float: 

44 return max(measure(t, theme.regular, TICK_SIZE) for t in tick_labels(ticks)) + 6 

45 

46 

47def value_axis( 

48 ticks: Ticks, area: Area, scale: LinearScale, theme: Theme 

49) -> list[object]: 

50 """Horizontal grid lines at each tick, its label to the left.""" 

51 shapes: list[object] = [] 

52 for value, text in zip(ticks.values, tick_labels(ticks)): 

53 y = scale(value) 

54 shapes.append(rule(area.left, y, area.right, y, RULE, 0.6, GRID_DASH)) 

55 shapes.append( 

56 label( 

57 area.left - 4, 

58 y - TICK_SIZE * 0.35, 

59 text, 

60 theme.regular, 

61 TICK_SIZE, 

62 MUTED, 

63 "end", 

64 ) 

65 ) 

66 if ticks.lo < 0 < ticks.hi: 

67 shapes.append(rule(area.left, scale(0), area.right, scale(0), MUTED, 0.6)) 

68 return shapes 

69 

70 

71def numeric_axis( 

72 ticks: Ticks, area: Area, scale: LinearScale, theme: Theme 

73) -> list[object]: 

74 """Vertical grid lines at each tick of a numeric x axis, labels below.""" 

75 shapes: list[object] = [] 

76 for value, text in zip(ticks.values, tick_labels(ticks)): 

77 x = scale(value) 

78 shapes.append(rule(x, area.bottom, x, area.top, RULE, 0.6, GRID_DASH)) 

79 shapes.append( 

80 label( 

81 x, 

82 area.bottom - TICK_SIZE - 3, 

83 text, 

84 theme.regular, 

85 TICK_SIZE, 

86 MUTED, 

87 "middle", 

88 ) 

89 ) 

90 return shapes 

91 

92 

93def base_line(area: Area) -> object: 

94 return rule(area.left, area.bottom, area.right, area.bottom, MUTED, 0.6) 

95 

96 

97@dataclass(frozen=True) 

98class CategoryLabels: 

99 texts: list[str] 

100 turned: bool 

101 #: Every ``step``th label is drawn when they would crowd. 

102 step: int 

103 height: float 

104 

105 

106def fit_categories(labels: list[str], band: float, theme: Theme) -> CategoryLabels: 

107 """How category labels fit under their bands: flat when they fit, 

108 otherwise turned and shortened, and thinned when even that crowds.""" 

109 font, size = theme.regular, TICK_SIZE 

110 widest = max((measure(t, font, size) for t in labels), default=0.0) 

111 if widest <= band - 2: 

112 return CategoryLabels(labels, False, 1, size + 6) 

113 texts = [ellipsize(t, font, size, TURNED_LABEL_MAX) for t in labels] 

114 widest = max((measure(t, font, size) for t in texts), default=0.0) 

115 rad = math.radians(TURN) 

116 # Turned labels crowd across their slant: one every ``step`` bands keeps 

117 # a line's height between neighbours. 

118 step = max(1, math.ceil((size * 1.25) / max(band * math.sin(rad), 0.1))) 

119 height = widest * math.sin(rad) + size * math.cos(rad) + 6 

120 return CategoryLabels(texts, True, step, height) 

121 

122 

123def category_axis( 

124 fitted: CategoryLabels, centres: list[float], area: Area, theme: Theme 

125) -> list[object]: 

126 shapes: list[object] = [] 

127 for i in range(0, len(fitted.texts), fitted.step): 

128 x = centres[i] 

129 if fitted.turned: 

130 shapes.append( 

131 label( 

132 x + TICK_SIZE * 0.3, 

133 area.bottom - 4, 

134 fitted.texts[i], 

135 theme.regular, 

136 TICK_SIZE, 

137 MUTED, 

138 "end", 

139 TURN, 

140 ) 

141 ) 

142 else: 

143 shapes.append( 

144 label( 

145 x, 

146 area.bottom - TICK_SIZE - 3, 

147 fitted.texts[i], 

148 theme.regular, 

149 TICK_SIZE, 

150 MUTED, 

151 "middle", 

152 ) 

153 ) 

154 return shapes 

155 

156 

157def axis_titles( 

158 x_title: str, y_title: str, area: Area, x_title_y: float, theme: Theme 

159) -> list[object]: 

160 """A plot's axis labels: x centred below its ticks, y turned upright at 

161 the left.""" 

162 shapes: list[object] = [] 

163 if x_title: 

164 text = ellipsize(x_title, theme.regular, LABEL_SIZE, area.width) 

165 shapes.append( 

166 label( 

167 area.left + area.width / 2, 

168 x_title_y, 

169 text, 

170 theme.regular, 

171 LABEL_SIZE, 

172 LABEL, 

173 "middle", 

174 ) 

175 ) 

176 if y_title: 

177 text = ellipsize(y_title, theme.regular, LABEL_SIZE, area.height) 

178 shapes.append( 

179 label( 

180 LABEL_SIZE, 

181 area.bottom + area.height / 2, 

182 text, 

183 theme.regular, 

184 LABEL_SIZE, 

185 LABEL, 

186 "middle", 

187 90, 

188 ) 

189 ) 

190 return shapes