Skip to content

Commit 72cacb3

Browse files
committed
feat(charts): support palette indices for AreaFill colors
Update AreaFill model to accept both hex strings and palette indices for color fields. Changes include: - Allow `color` field to accept int (palette index) or str (hex color) - Change default `color` from "#15607a" to 0 (first palette color) - Allow `color_negative` to accept int, str, or None - Change default `color_negative` from "#cc0000" to None (disabled) - Update auto-enable logic to check for None instead of default hex value - Only serialize `colorNegative` when explicitly set (not None) - Update field descriptions to reflect new behavior This provides more flexibility for users to reference theme palette colors by index while maintaining backward compatibility with hex color strings.
1 parent 077b8b0 commit 72cacb3

1 file changed

Lines changed: 19 additions & 11 deletions

File tree

datawrapper/charts/annos.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,10 @@ class AreaFill(BaseModel):
321321
#: The line to fill upwards to
322322
to_column: str = Field(alias="to", description="The line to fill upwards to")
323323

324-
#: The color of the fill
325-
color: str = Field(default="#15607a", description="The color of the fill")
324+
#: The color of the fill (hex string or palette index)
325+
color: str | int = Field(
326+
default=0, description="The color of the fill (hex string or palette index)"
327+
)
326328

327329
#: The opacity of the fill
328330
opacity: float = Field(default=0.3, description="The opacity of the fill")
@@ -334,11 +336,11 @@ class AreaFill(BaseModel):
334336
description="Whether to use different colors when there are negative values",
335337
)
336338

337-
#: The color of the fill when it is negative
338-
color_negative: str = Field(
339-
default="#cc0000",
339+
#: The color of the fill when it is negative (hex string or palette index, None = disabled)
340+
color_negative: str | int | None = Field(
341+
default=None,
340342
alias="colorNegative",
341-
description="The color of the fill when it is negative",
343+
description="The color of the fill when it is negative (hex string or palette index, None = disabled)",
342344
)
343345

344346
#: The interpolation method to use when drawing lines
@@ -364,29 +366,35 @@ def validate_interpolation(
364366
def auto_enable_mixed_colors(self) -> "AreaFill":
365367
"""Auto-enable use_mixed_colors when color_negative is provided.
366368
367-
If a user provides a color_negative value (different from the default),
369+
If a user provides a color_negative value (not None),
368370
automatically enable use_mixed_colors to make the feature work as expected.
369371
"""
370-
# Only auto-enable if color_negative differs from default and use_mixed_colors is False
371-
if self.color_negative != "#cc0000" and not self.use_mixed_colors:
372+
# Only auto-enable if color_negative is provided and use_mixed_colors is False
373+
if self.color_negative is not None and not self.use_mixed_colors:
372374
self.use_mixed_colors = True
373375
return self
374376

375377
def serialize_model(self) -> dict:
376378
"""Serialize the model to a dictionary for the Datawrapper API.
377379
378380
Note: The 'id' field is not included in the output as it's used as the dict key.
381+
Only includes colorNegative if it's not None.
379382
"""
380-
return {
383+
result = {
381384
"from": self.from_column,
382385
"to": self.to_column,
383386
"color": self.color,
384387
"opacity": self.opacity,
385388
"useMixedColors": self.use_mixed_colors,
386-
"colorNegative": self.color_negative,
387389
"interpolation": self.interpolation,
388390
}
389391

392+
# Only include colorNegative if it's provided (not None)
393+
if self.color_negative is not None:
394+
result["colorNegative"] = self.color_negative
395+
396+
return result
397+
390398
@classmethod
391399
def deserialize_model(cls, api_data: dict[str, dict] | list | None) -> list[dict]:
392400
"""Deserialize area fills from API response format.

0 commit comments

Comments
 (0)