45 lines
2.8 KiB
Python
45 lines
2.8 KiB
Python
"""Style des points et des courbes."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from shortstats.web import chart_config, charts, marks
|
|
|
|
from ..outils.lignes import _scatter, fake_rows, multi_rows
|
|
|
|
|
|
def test_style_applies_to_points_and_lines():
|
|
style = {"shape": "diamond", "point": "large", "line": "thick"}
|
|
point = marks.style_mark({"type": "point", "filled": True, "size": 60}, style)
|
|
assert point["shape"] == "diamond" and point["size"] == round(60 * 1.4 * 1.4, 1)
|
|
line = marks.style_mark({"type": "line", "point": {"filled": True, "size": 45}, "strokeWidth": 2}, style)
|
|
assert line["strokeWidth"] == 3.2 and line["point"] == {"filled": True, "size": 88.2, "shape": "diamond"}
|
|
each = marks.style_mark({"type": "line", "strokeWidth": 1.3, "point": {"size": 16}}, {"line": "thin"})
|
|
assert each["strokeWidth"] == 0.78 and each["point"] == {"size": 16} # points inchangés
|
|
assert marks.style_mark({"type": "line"}, {"line": "xthick"})["strokeWidth"] == 4.8 # épaisseur du thème : 2
|
|
# axe de droite (carrés) : reste distinct de l'axe de gauche
|
|
assert marks.style_mark({"type": "point", "size": 60, "shape": "square"}, {"shape": "diamond"})["shape"] == "square"
|
|
assert marks.style_mark({"type": "point", "size": 60, "shape": "square"}, {"shape": "square"})["shape"] == "circle"
|
|
# barres, boîtes et vidéos mises en avant : jamais touchées
|
|
for mark in ({"type": "bar"}, {"type": "boxplot"}, {"type": "point", "size": 120, "style": "hl-point"},
|
|
{"type": "line", "strokeWidth": 2.5, "style": "hl-line"}):
|
|
assert marks.style_mark(mark, style) == mark
|
|
|
|
|
|
def test_style_on_built_charts():
|
|
style = {"shape": "triangle-up", "point": "small", "line": "thick"}
|
|
res = _scatter()
|
|
marks.apply(res, style)
|
|
assert res["spec"]["mark"]["shape"] == "triangle-up" and res["spec"]["mark"]["size"] == round(60 * 0.49, 1)
|
|
# deux ordonnées en points : gauche en triangles, droite en carrés
|
|
cfg = chart_config.validate_config({"x": "views", "y": "likes", "y_more": ["comments"], "type": "scatter",
|
|
"split_channels": False})
|
|
res = charts.build(cfg, multi_rows(cfg), None, None)
|
|
marks.apply(res, style)
|
|
assert [layer["mark"]["shape"] for layer in res["spec"]["layer"]] == ["triangle-up", "square"]
|
|
# petits diagrammes par chaîne : le style vaut pour chacun
|
|
cfg = chart_config.validate_config({"x": "publish_date", "y": "views", "type": "line", "split_channels": True})
|
|
rows = [r + (("Chaîne A", "Chaîne B")[i % 2],) for i, r in enumerate(fake_rows(cfg))]
|
|
res = charts.build(cfg, rows, None, None)
|
|
marks.apply(res, style)
|
|
body = res["spec"].get("spec", res["spec"])
|
|
assert body["mark"]["strokeWidth"] == 3.2 and body["mark"]["point"]["shape"] == "triangle-up"
|