diff --git a/datawrapper/__init__.py b/datawrapper/__init__.py index a549d60..bf23b14 100644 --- a/datawrapper/__init__.py +++ b/datawrapper/__init__.py @@ -55,6 +55,7 @@ ScatterGridLines, ScatterShape, ScatterSize, + StrokeType, StrokeWidth, SymbolDisplay, SymbolShape, @@ -122,6 +123,7 @@ "ScatterGridLines", "ScatterShape", "ScatterSize", + "StrokeType", "StrokeWidth", "SymbolDisplay", "SymbolShape", diff --git a/datawrapper/charts/__init__.py b/datawrapper/charts/__init__.py index 4fa1293..045cd69 100644 --- a/datawrapper/charts/__init__.py +++ b/datawrapper/charts/__init__.py @@ -23,6 +23,7 @@ ScatterGridLines, ScatterShape, ScatterSize, + StrokeType, StrokeWidth, SymbolDisplay, SymbolShape, @@ -84,6 +85,7 @@ "ScatterGridLines", "ScatterShape", "ScatterSize", + "StrokeType", "StrokeWidth", "SymbolDisplay", "SymbolShape", diff --git a/datawrapper/charts/annos.py b/datawrapper/charts/annos.py index 18c4dc0..40878a3 100644 --- a/datawrapper/charts/annos.py +++ b/datawrapper/charts/annos.py @@ -2,7 +2,13 @@ from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator -from .enums import ArrowHead, ConnectorLineType, LineInterpolation, StrokeWidth +from .enums import ( + ArrowHead, + ConnectorLineType, + LineInterpolation, + StrokeType, + StrokeWidth, +) class ConnectorLine(BaseModel): @@ -471,12 +477,24 @@ class RangeAnnotation(BaseModel): ) #: The stroke type of the annotation, if the display style is a line - stroke_type: Literal["solid", "dashed", "dotted"] = Field( + stroke_type: StrokeType | str = Field( default="solid", alias="strokeType", description=" The stroke type of the annotation, if the display style is a line", ) + @field_validator("stroke_type") + @classmethod + def validate_stroke_type(cls, v: StrokeType | str) -> StrokeType | str: + """Validate that stroke_type is a valid StrokeType value.""" + if isinstance(v, str): + valid_values = [e.value for e in StrokeType] + if v not in valid_values: + raise ValueError( + f"Invalid stroke type: {v}. Must be one of {valid_values}" + ) + return v + #: The stroke width of the annotation, if the display style is a line stroke_width: StrokeWidth | int = Field( default=2, diff --git a/datawrapper/charts/enums/__init__.py b/datawrapper/charts/enums/__init__.py index 4d5ffbe..c4a69be 100644 --- a/datawrapper/charts/enums/__init__.py +++ b/datawrapper/charts/enums/__init__.py @@ -1,6 +1,6 @@ """Enum classes for Datawrapper chart formatting and styling options.""" -from .connector_line import ArrowHead, ConnectorLineType, StrokeWidth +from .annos import ArrowHead, ConnectorLineType, StrokeType, StrokeWidth from .date_format import DateFormat from .grid_display import GridDisplay from .grid_label import GridLabelAlign, GridLabelPosition @@ -45,6 +45,7 @@ "ScatterGridLines", "ScatterShape", "ScatterSize", + "StrokeType", "StrokeWidth", "SymbolDisplay", "SymbolShape", diff --git a/datawrapper/charts/enums/connector_line.py b/datawrapper/charts/enums/annos.py similarity index 74% rename from datawrapper/charts/enums/connector_line.py rename to datawrapper/charts/enums/annos.py index d9a003f..02564ce 100644 --- a/datawrapper/charts/enums/connector_line.py +++ b/datawrapper/charts/enums/annos.py @@ -52,6 +52,32 @@ class StrokeWidth(int, Enum): THICK = 3 +class StrokeType(str, Enum): + """Stroke type options for line annotations. + + Controls the dash pattern of stroke lines in range annotations. + + Attributes: + SOLID: Solid line (no dashes) + DASHED: Dashed line pattern + DOTTED: Dotted line pattern + + Examples: + >>> from datawrapper.charts.annos import RangeAnnotation, StrokeType + >>> anno = RangeAnnotation( + ... type="x", + ... x0=0, + ... x1=10, + ... display="line", + ... stroke_type=StrokeType.DASHED, + ... ) + """ + + SOLID = "solid" + DASHED = "dashed" + DOTTED = "dotted" + + class ArrowHead(str, Enum): """Arrow head options for connector lines. diff --git a/docs/user-guide/api/enums.rst b/docs/user-guide/api/enums.rst index 3a16ca1..34e6dda 100644 --- a/docs/user-guide/api/enums.rst +++ b/docs/user-guide/api/enums.rst @@ -177,6 +177,16 @@ ScatterSize .. enum-table:: datawrapper.charts.enums.ScatterSize +StrokeType +---------- + +.. code-block:: python + + import datawrapper as dw + connector = dw.RangeAnnotation(stroke_type=dw.StrokeType.DASHED) + +.. enum-table:: datawrapper.charts.enums.StrokeType + StrokeWidth -----------