Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions datawrapper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
ScatterGridLines,
ScatterShape,
ScatterSize,
StrokeType,
StrokeWidth,
SymbolDisplay,
SymbolShape,
Expand Down Expand Up @@ -122,6 +123,7 @@
"ScatterGridLines",
"ScatterShape",
"ScatterSize",
"StrokeType",
"StrokeWidth",
"SymbolDisplay",
"SymbolShape",
Expand Down
2 changes: 2 additions & 0 deletions datawrapper/charts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
ScatterGridLines,
ScatterShape,
ScatterSize,
StrokeType,
StrokeWidth,
SymbolDisplay,
SymbolShape,
Expand Down Expand Up @@ -84,6 +85,7 @@
"ScatterGridLines",
"ScatterShape",
"ScatterSize",
"StrokeType",
"StrokeWidth",
"SymbolDisplay",
"SymbolShape",
Expand Down
22 changes: 20 additions & 2 deletions datawrapper/charts/annos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion datawrapper/charts/enums/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -45,6 +45,7 @@
"ScatterGridLines",
"ScatterShape",
"ScatterSize",
"StrokeType",
"StrokeWidth",
"SymbolDisplay",
"SymbolShape",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
10 changes: 10 additions & 0 deletions docs/user-guide/api/enums.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----------

Expand Down
Loading