Skip to content

Commit bd9b8b7

Browse files
authored
Merge pull request #497 from palewire/main
feat(scatter): add color column and category customization support
2 parents 720dedf + a829331 commit bd9b8b7

4 files changed

Lines changed: 355 additions & 66 deletions

File tree

datawrapper/charts/scatter.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
ScatterSize,
1616
)
1717
from .models import AnnotationsMixin
18-
from .serializers import PlotHeight
18+
from .serializers import ColorCategory, PlotHeight
1919

2020

2121
class ScatterPlot(AnnotationsMixin, BaseChart):
@@ -198,6 +198,41 @@ class ScatterPlot(AnnotationsMixin, BaseChart):
198198
description="Whether to show the color key",
199199
)
200200

201+
#: The column with the color for the points
202+
color_column: str = Field(
203+
default="",
204+
alias="color-column",
205+
description="The column with the color for the points",
206+
)
207+
208+
#: A mapping of layer names to colors
209+
color_category: dict[str, str] = Field(
210+
default_factory=dict,
211+
alias="color-category",
212+
description="A mapping of layer names to colors",
213+
)
214+
215+
#: Dictionary mapping category names to their display labels in the color legend
216+
category_labels: dict[str, str] = Field(
217+
default_factory=dict,
218+
alias="category-labels",
219+
description="Dictionary mapping category names to their display labels in the color legend",
220+
)
221+
222+
#: List defining the order in which categories appear in the chart and legend
223+
category_order: list[str] = Field(
224+
default_factory=list,
225+
alias="category-order",
226+
description="List defining the order in which categories appear in the chart and legend",
227+
)
228+
229+
#: A list of columns to exclude from the color key
230+
exclude_from_color_key: list[str] = Field(
231+
default_factory=list,
232+
alias="exclude-from-color-key",
233+
description="A list of columns to exclude from the color key",
234+
)
235+
201236
#
202237
# Size
203238
#
@@ -498,6 +533,8 @@ def serialize_model(self) -> dict:
498533
axes["shape"] = self.shape_column
499534
if self.label_column:
500535
axes["labels"] = self.label_column
536+
if self.color_column:
537+
axes["color"] = self.color_column
501538

502539
# Add axes to metadata
503540
model["metadata"]["axes"] = axes
@@ -529,6 +566,13 @@ def serialize_model(self) -> dict:
529566
"outlines": self.outlines,
530567
"color-outline": self.color_outline,
531568
"show-color-key": self.show_color_key,
569+
"color-category": ColorCategory.serialize(
570+
self.color_category,
571+
self.category_labels,
572+
self.category_order,
573+
self.exclude_from_color_key,
574+
),
575+
"color-by-column": bool(self.color_category),
532576
# Size
533577
"size": self.size,
534578
"fixed-size": self.fixed_size,
@@ -604,6 +648,8 @@ def deserialize_model(cls, api_response: dict[str, Any]) -> dict[str, Any]:
604648
init_data["size_column"] = axes.get("size")
605649
init_data["shape_column"] = axes.get("shape")
606650
init_data["label_column"] = axes.get("labels")
651+
if "color" in axes:
652+
init_data["color_column"] = axes["color"]
607653

608654
# Parse x-axis
609655
x_axis = visualize.get("x-axis", {})
@@ -653,6 +699,9 @@ def deserialize_model(cls, api_response: dict[str, Any]) -> dict[str, Any]:
653699
if "show-color-key" in visualize:
654700
init_data["show_color_key"] = visualize["show-color-key"]
655701

702+
# Parse color-category using utility
703+
init_data.update(ColorCategory.deserialize(visualize.get("color-category")))
704+
656705
# Size
657706
if "size" in visualize:
658707
init_data["size"] = visualize["size"]

docs/user-guide/charts/multiple-column-charts.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,44 @@ chart.create()
174174

175175
## Reference
176176

177+
### MultipleColumnChart
178+
177179
```{eval-rst}
178180
.. parameter-table:: datawrapper.charts.MultipleColumnChart
179181
```
180182

183+
### MultipleColumnTextAnnotation
184+
181185
```{eval-rst}
182186
.. parameter-table:: datawrapper.charts.MultipleColumnTextAnnotation
183187
```
184188

189+
### MultipleColumnRangeAnnotation
190+
185191
```{eval-rst}
186192
.. parameter-table:: datawrapper.charts.MultipleColumnRangeAnnotation
187193
```
194+
195+
### MultipleColumnXRangeAnnotation
196+
197+
```{eval-rst}
198+
.. parameter-table:: datawrapper.charts.MultipleColumnXRangeAnnotation
199+
```
200+
201+
### MultipleColumnYRangeAnnotation
202+
203+
```{eval-rst}
204+
.. parameter-table:: datawrapper.charts.MultipleColumnYRangeAnnotation
205+
```
206+
207+
### MultipleColumnXLineAnnotation
208+
209+
```{eval-rst}
210+
.. parameter-table:: datawrapper.charts.MultipleColumnXLineAnnotation
211+
```
212+
213+
### MultipleColumnYLineAnnotation
214+
215+
```{eval-rst}
216+
.. parameter-table:: datawrapper.charts.MultipleColumnYLineAnnotation
217+
```

0 commit comments

Comments
 (0)