|
5 | 5 |
|
6 | 6 | from .base import BaseChart |
7 | 7 | from .enums import DateFormat, NumberFormat, ReplaceFlagsType, ValueLabelAlignment |
8 | | -from .models import RangeAnnotation, TextAnnotation |
9 | | -from .serializers import ColorCategory, CustomRange, ModelListSerializer, ReplaceFlags |
| 8 | +from .models import AnnotationsMixin |
| 9 | +from .serializers import ColorCategory, CustomRange, ReplaceFlags |
10 | 10 |
|
11 | 11 |
|
12 | 12 | class BarOverlay(BaseModel): |
@@ -75,7 +75,7 @@ class BarOverlay(BaseModel): |
75 | 75 | ) |
76 | 76 |
|
77 | 77 |
|
78 | | -class BarChart(BaseChart): |
| 78 | +class BarChart(AnnotationsMixin, BaseChart): |
79 | 79 | """A base class for the Datawrapper API's bar chart.""" |
80 | 80 |
|
81 | 81 | model_config = ConfigDict( |
@@ -384,20 +384,6 @@ class BarChart(BaseChart): |
384 | 384 | description="A list of the highlighted series", |
385 | 385 | ) |
386 | 386 |
|
387 | | - #: A list of text annotations to display on the chart |
388 | | - text_annotations: list[TextAnnotation | dict[Any, Any]] = Field( |
389 | | - default_factory=list, |
390 | | - alias="text-annotations", |
391 | | - description="A list of text annotations to display on the chart", |
392 | | - ) |
393 | | - |
394 | | - #: A list of range annotations to display on the chart |
395 | | - range_annotations: list[RangeAnnotation | dict[Any, Any]] = Field( |
396 | | - default_factory=list, |
397 | | - alias="range-annotations", |
398 | | - description="A list of range annotations to display on the chart", |
399 | | - ) |
400 | | - |
401 | 387 | @field_validator("replace_flags") |
402 | 388 | @classmethod |
403 | 389 | def validate_replace_flags( |
@@ -460,15 +446,12 @@ def serialize_model(self) -> dict: |
460 | 446 | "overlays": [], |
461 | 447 | # Annotations |
462 | 448 | "highlighted-series": self.highlighted_series, |
463 | | - "text-annotations": ModelListSerializer.serialize( |
464 | | - self.text_annotations, TextAnnotation |
465 | | - ), |
466 | | - "range-annotations": ModelListSerializer.serialize( |
467 | | - self.range_annotations, RangeAnnotation |
468 | | - ), |
469 | 449 | } |
470 | 450 | ) |
471 | 451 |
|
| 452 | + # Add annotations |
| 453 | + model["metadata"]["visualize"].update(self._serialize_annotations()) |
| 454 | + |
472 | 455 | # Add the overlays, if any |
473 | 456 | for overlay_obj in self.overlays: |
474 | 457 | # If the overlay is a dictionary, validate it and convert it to a BarOverlay object |
@@ -608,12 +591,6 @@ def deserialize_model(cls, api_response: dict[str, Any]) -> dict[str, Any]: |
608 | 591 | if "highlighted-series" in visualize: |
609 | 592 | init_data["highlighted_series"] = visualize["highlighted-series"] |
610 | 593 |
|
611 | | - # Annotations |
612 | | - init_data["text_annotations"] = TextAnnotation.deserialize_model( |
613 | | - visualize.get("text-annotations") |
614 | | - ) |
615 | | - init_data["range_annotations"] = RangeAnnotation.deserialize_model( |
616 | | - visualize.get("range-annotations") |
617 | | - ) |
| 594 | + init_data.update(cls._deserialize_annotations(visualize)) |
618 | 595 |
|
619 | 596 | return init_data |
0 commit comments