shinymap 0.1.0a1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- shinymap-0.1.0a1/.gitignore +25 -0
- shinymap-0.1.0a1/PKG-INFO +118 -0
- shinymap-0.1.0a1/README.md +100 -0
- shinymap-0.1.0a1/pyproject.toml +33 -0
- shinymap-0.1.0a1/src/shinymap/__init__.py +36 -0
- shinymap-0.1.0a1/src/shinymap/_colors.py +181 -0
- shinymap-0.1.0a1/src/shinymap/_ui.py +306 -0
- shinymap-0.1.0a1/src/shinymap/www/shinymap-shiny.js +264 -0
- shinymap-0.1.0a1/src/shinymap/www/shinymap.global.js +22068 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Local data downloads
|
|
2
|
+
data/prefectures.geojson
|
|
3
|
+
|
|
4
|
+
# Bytecode / caches
|
|
5
|
+
__pycache__/
|
|
6
|
+
*.pyc
|
|
7
|
+
|
|
8
|
+
# Virtual environments
|
|
9
|
+
.venv/
|
|
10
|
+
py/.venv/
|
|
11
|
+
|
|
12
|
+
# Frontend workspace artifacts
|
|
13
|
+
frontend/node_modules/
|
|
14
|
+
packages/shinymap/js/node_modules/
|
|
15
|
+
py/examples/react_demo/www/
|
|
16
|
+
frontend/r/www/
|
|
17
|
+
packages/shinymap/js/demo/dist/
|
|
18
|
+
archive/legacy/migration/shinymapjp/node_modules/
|
|
19
|
+
|
|
20
|
+
# R package scratch files
|
|
21
|
+
r/shinyjpmap/.Rproj.user/
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
# temporary
|
|
25
|
+
archive
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: shinymap
|
|
3
|
+
Version: 0.1.0a1
|
|
4
|
+
Summary: Interactive SVG map components for Shiny for Python
|
|
5
|
+
Project-URL: Homepage, https://github.com/kenjisato/shinymap
|
|
6
|
+
Author: shinymap contributors
|
|
7
|
+
License: MIT
|
|
8
|
+
Keywords: interactive,maps,shiny,svg,visualization
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Topic :: Scientific/Engineering :: Visualization
|
|
15
|
+
Requires-Python: >=3.12
|
|
16
|
+
Requires-Dist: shiny>=1.4.0
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
|
|
19
|
+
# shinymap (Python)
|
|
20
|
+
|
|
21
|
+
Shiny for Python adapter for the core `shinymap` renderer. It bundles the prebuilt JS assets and exposes helpers to drop maps into Shiny apps without touching React.
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pip install shinymap
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Or with uv:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
uv add shinymap
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## API
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
from shinymap import Map, MapPayload, input_map, output_map, render_map
|
|
39
|
+
from shinymap import scale_sequential, scale_qualitative, SEQUENTIAL, QUALITATIVE
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
- `input_map(id, geometry, mode="single"|"multiple"|"count", cycle=None, max_selection=None, hover_highlight=None, ...)` renders an interactive input.
|
|
43
|
+
- For `mode="single"`: returns a single selected region ID (string) or None
|
|
44
|
+
- For `mode="multiple"`: returns a list of selected region IDs
|
|
45
|
+
- For `mode="count"`: returns a dict mapping region IDs to counts
|
|
46
|
+
- `hover_highlight` accepts a dict with keys: `stroke_width`, `fill_opacity`, `stroke_color`, `fill_color` for customizing hover effects
|
|
47
|
+
- `output_map("map")` adds a placeholder in your UI; pair it with a `@render_map` output in the server.
|
|
48
|
+
- `Map` (alias for `MapBuilder`) provides a fluent API for building map payloads with method chaining.
|
|
49
|
+
- `MapPayload` models the data you can send to an output map: geometry, tooltips, fills, counts, active ids, default aesthetics, etc.
|
|
50
|
+
- `render_map` is a convenience decorator that serializes a `Map`/`MapPayload` (or dict) and mounts the React output map.
|
|
51
|
+
- `scale_sequential(counts, region_ids, max_count=None)` and `scale_qualitative(categories, region_ids, palette=None)` are helper functions for generating fill color maps.
|
|
52
|
+
|
|
53
|
+
## Minimal example
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
from shiny import App, ui
|
|
57
|
+
from shinymap import Map, input_map, output_map, render_map, scale_sequential
|
|
58
|
+
|
|
59
|
+
DEMO_GEOMETRY = {
|
|
60
|
+
"circle": "M25,50 A20,20 0 1 1 24.999,50 Z",
|
|
61
|
+
"square": "M10 10 H40 V40 H10 Z",
|
|
62
|
+
"triangle": "M75 70 L90 40 L60 40 Z",
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
TOOLTIPS = {"circle": "Circle", "square": "Square", "triangle": "Triangle"}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
app_ui = ui.page_fluid(
|
|
69
|
+
ui.h2("shinymap demo"),
|
|
70
|
+
ui.layout_columns(
|
|
71
|
+
input_map(
|
|
72
|
+
"region",
|
|
73
|
+
DEMO_GEOMETRY,
|
|
74
|
+
tooltips=TOOLTIPS,
|
|
75
|
+
mode="single",
|
|
76
|
+
hover_highlight={"stroke_width": 1},
|
|
77
|
+
),
|
|
78
|
+
output_map("summary"),
|
|
79
|
+
),
|
|
80
|
+
ui.br(),
|
|
81
|
+
ui.h4("Counts"),
|
|
82
|
+
ui.layout_columns(
|
|
83
|
+
input_map(
|
|
84
|
+
"clicks",
|
|
85
|
+
DEMO_GEOMETRY,
|
|
86
|
+
tooltips=TOOLTIPS,
|
|
87
|
+
mode="count",
|
|
88
|
+
hover_highlight={"stroke_width": 2, "fill_opacity": -0.3},
|
|
89
|
+
),
|
|
90
|
+
output_map("counts"),
|
|
91
|
+
),
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def server(input, output, session):
|
|
96
|
+
@render_map
|
|
97
|
+
def summary():
|
|
98
|
+
# mode="single" returns a single ID (string) or None
|
|
99
|
+
selected = input.region()
|
|
100
|
+
return (
|
|
101
|
+
Map(DEMO_GEOMETRY, tooltips=TOOLTIPS)
|
|
102
|
+
.with_active(selected)
|
|
103
|
+
.with_stroke_width(1.5)
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
@render_map
|
|
107
|
+
def counts():
|
|
108
|
+
# mode="count" returns a dict mapping region IDs to counts
|
|
109
|
+
counts_data = input.clicks() or {}
|
|
110
|
+
return (
|
|
111
|
+
Map(DEMO_GEOMETRY, tooltips=TOOLTIPS)
|
|
112
|
+
.with_fills(scale_sequential(counts_data, list(DEMO_GEOMETRY.keys()), max_count=10))
|
|
113
|
+
.with_counts(counts_data)
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
app = App(app_ui, server)
|
|
118
|
+
```
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# shinymap (Python)
|
|
2
|
+
|
|
3
|
+
Shiny for Python adapter for the core `shinymap` renderer. It bundles the prebuilt JS assets and exposes helpers to drop maps into Shiny apps without touching React.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install shinymap
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Or with uv:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
uv add shinymap
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## API
|
|
18
|
+
|
|
19
|
+
```python
|
|
20
|
+
from shinymap import Map, MapPayload, input_map, output_map, render_map
|
|
21
|
+
from shinymap import scale_sequential, scale_qualitative, SEQUENTIAL, QUALITATIVE
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
- `input_map(id, geometry, mode="single"|"multiple"|"count", cycle=None, max_selection=None, hover_highlight=None, ...)` renders an interactive input.
|
|
25
|
+
- For `mode="single"`: returns a single selected region ID (string) or None
|
|
26
|
+
- For `mode="multiple"`: returns a list of selected region IDs
|
|
27
|
+
- For `mode="count"`: returns a dict mapping region IDs to counts
|
|
28
|
+
- `hover_highlight` accepts a dict with keys: `stroke_width`, `fill_opacity`, `stroke_color`, `fill_color` for customizing hover effects
|
|
29
|
+
- `output_map("map")` adds a placeholder in your UI; pair it with a `@render_map` output in the server.
|
|
30
|
+
- `Map` (alias for `MapBuilder`) provides a fluent API for building map payloads with method chaining.
|
|
31
|
+
- `MapPayload` models the data you can send to an output map: geometry, tooltips, fills, counts, active ids, default aesthetics, etc.
|
|
32
|
+
- `render_map` is a convenience decorator that serializes a `Map`/`MapPayload` (or dict) and mounts the React output map.
|
|
33
|
+
- `scale_sequential(counts, region_ids, max_count=None)` and `scale_qualitative(categories, region_ids, palette=None)` are helper functions for generating fill color maps.
|
|
34
|
+
|
|
35
|
+
## Minimal example
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
from shiny import App, ui
|
|
39
|
+
from shinymap import Map, input_map, output_map, render_map, scale_sequential
|
|
40
|
+
|
|
41
|
+
DEMO_GEOMETRY = {
|
|
42
|
+
"circle": "M25,50 A20,20 0 1 1 24.999,50 Z",
|
|
43
|
+
"square": "M10 10 H40 V40 H10 Z",
|
|
44
|
+
"triangle": "M75 70 L90 40 L60 40 Z",
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
TOOLTIPS = {"circle": "Circle", "square": "Square", "triangle": "Triangle"}
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
app_ui = ui.page_fluid(
|
|
51
|
+
ui.h2("shinymap demo"),
|
|
52
|
+
ui.layout_columns(
|
|
53
|
+
input_map(
|
|
54
|
+
"region",
|
|
55
|
+
DEMO_GEOMETRY,
|
|
56
|
+
tooltips=TOOLTIPS,
|
|
57
|
+
mode="single",
|
|
58
|
+
hover_highlight={"stroke_width": 1},
|
|
59
|
+
),
|
|
60
|
+
output_map("summary"),
|
|
61
|
+
),
|
|
62
|
+
ui.br(),
|
|
63
|
+
ui.h4("Counts"),
|
|
64
|
+
ui.layout_columns(
|
|
65
|
+
input_map(
|
|
66
|
+
"clicks",
|
|
67
|
+
DEMO_GEOMETRY,
|
|
68
|
+
tooltips=TOOLTIPS,
|
|
69
|
+
mode="count",
|
|
70
|
+
hover_highlight={"stroke_width": 2, "fill_opacity": -0.3},
|
|
71
|
+
),
|
|
72
|
+
output_map("counts"),
|
|
73
|
+
),
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def server(input, output, session):
|
|
78
|
+
@render_map
|
|
79
|
+
def summary():
|
|
80
|
+
# mode="single" returns a single ID (string) or None
|
|
81
|
+
selected = input.region()
|
|
82
|
+
return (
|
|
83
|
+
Map(DEMO_GEOMETRY, tooltips=TOOLTIPS)
|
|
84
|
+
.with_active(selected)
|
|
85
|
+
.with_stroke_width(1.5)
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
@render_map
|
|
89
|
+
def counts():
|
|
90
|
+
# mode="count" returns a dict mapping region IDs to counts
|
|
91
|
+
counts_data = input.clicks() or {}
|
|
92
|
+
return (
|
|
93
|
+
Map(DEMO_GEOMETRY, tooltips=TOOLTIPS)
|
|
94
|
+
.with_fills(scale_sequential(counts_data, list(DEMO_GEOMETRY.keys()), max_count=10))
|
|
95
|
+
.with_counts(counts_data)
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
app = App(app_ui, server)
|
|
100
|
+
```
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.18"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "shinymap"
|
|
7
|
+
version = "0.1.0a1"
|
|
8
|
+
description = "Interactive SVG map components for Shiny for Python"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
authors = [{name = "shinymap contributors"}]
|
|
11
|
+
license = {text = "MIT"}
|
|
12
|
+
requires-python = ">=3.12"
|
|
13
|
+
dependencies = [
|
|
14
|
+
"shiny>=1.4.0"
|
|
15
|
+
]
|
|
16
|
+
keywords = ["shiny", "visualization", "interactive", "maps", "svg"]
|
|
17
|
+
classifiers = [
|
|
18
|
+
"Development Status :: 3 - Alpha",
|
|
19
|
+
"Intended Audience :: Developers",
|
|
20
|
+
"License :: OSI Approved :: MIT License",
|
|
21
|
+
"Programming Language :: Python :: 3",
|
|
22
|
+
"Programming Language :: Python :: 3.12",
|
|
23
|
+
"Topic :: Scientific/Engineering :: Visualization",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
[project.urls]
|
|
27
|
+
Homepage = "https://github.com/kenjisato/shinymap"
|
|
28
|
+
|
|
29
|
+
[tool.hatch.build.targets.wheel]
|
|
30
|
+
packages = ["src/shinymap"]
|
|
31
|
+
|
|
32
|
+
[tool.hatch.build.targets.sdist]
|
|
33
|
+
include = ["src", "README.md"]
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
__version__ = "0.0.1"
|
|
2
|
+
|
|
3
|
+
"""Core Shiny for Python bindings for the shinymap renderer."""
|
|
4
|
+
|
|
5
|
+
from ._colors import (
|
|
6
|
+
NEUTRALS,
|
|
7
|
+
QUALITATIVE,
|
|
8
|
+
SEQUENTIAL_BLUE,
|
|
9
|
+
SEQUENTIAL_GREEN,
|
|
10
|
+
SEQUENTIAL_ORANGE,
|
|
11
|
+
lerp_hex,
|
|
12
|
+
scale_diverging,
|
|
13
|
+
scale_qualitative,
|
|
14
|
+
scale_sequential,
|
|
15
|
+
)
|
|
16
|
+
from ._ui import Map, MapBuilder, MapPayload, input_map, output_map, render_map
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
"__version__",
|
|
20
|
+
"Map",
|
|
21
|
+
"MapBuilder",
|
|
22
|
+
"MapPayload",
|
|
23
|
+
"input_map",
|
|
24
|
+
"output_map",
|
|
25
|
+
"render_map",
|
|
26
|
+
# Color utilities
|
|
27
|
+
"NEUTRALS",
|
|
28
|
+
"QUALITATIVE",
|
|
29
|
+
"SEQUENTIAL_BLUE",
|
|
30
|
+
"SEQUENTIAL_GREEN",
|
|
31
|
+
"SEQUENTIAL_ORANGE",
|
|
32
|
+
"lerp_hex",
|
|
33
|
+
"scale_sequential",
|
|
34
|
+
"scale_diverging",
|
|
35
|
+
"scale_qualitative",
|
|
36
|
+
]
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
"""Color scale utilities for shinymap."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Mapping
|
|
6
|
+
|
|
7
|
+
# Tailwind-inspired color palettes
|
|
8
|
+
NEUTRALS = {
|
|
9
|
+
"stroke": "#1f2937", # slate-800
|
|
10
|
+
"stroke_active": "#0f172a", # slate-900
|
|
11
|
+
"fill": "#e2e8f0", # slate-200
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
QUALITATIVE = [
|
|
15
|
+
"#2563eb", # blue-600
|
|
16
|
+
"#16a34a", # green-600
|
|
17
|
+
"#f59e0b", # amber-500
|
|
18
|
+
"#ef4444", # red-500
|
|
19
|
+
"#a855f7", # purple-500
|
|
20
|
+
"#06b6d4", # cyan-500
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
SEQUENTIAL_BLUE = ["#eff6ff", "#bfdbfe", "#93c5fd", "#60a5fa", "#3b82f6", "#2563eb", "#1d4ed8"]
|
|
24
|
+
SEQUENTIAL_GREEN = ["#ecfdf3", "#bbf7d0", "#86efac", "#4ade80", "#22c55e", "#16a34a", "#15803d"]
|
|
25
|
+
SEQUENTIAL_ORANGE = ["#fff7ed", "#ffedd5", "#fed7aa", "#fdba74", "#fb923c", "#f97316", "#c2410c"]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def lerp_hex(start: str, end: str, t: float) -> str:
|
|
29
|
+
"""Interpolate between two hex colors.
|
|
30
|
+
|
|
31
|
+
Args:
|
|
32
|
+
start: Starting hex color (e.g., "#ff0000")
|
|
33
|
+
end: Ending hex color (e.g., "#00ff00")
|
|
34
|
+
t: Interpolation factor, clamped to [0, 1]
|
|
35
|
+
|
|
36
|
+
Returns:
|
|
37
|
+
Interpolated hex color
|
|
38
|
+
"""
|
|
39
|
+
t = max(0.0, min(1.0, t))
|
|
40
|
+
s_rgb = tuple(int(start[i : i + 2], 16) for i in (1, 3, 5))
|
|
41
|
+
e_rgb = tuple(int(end[i : i + 2], 16) for i in (1, 3, 5))
|
|
42
|
+
mix = tuple(int(s + (e - s) * t) for s, e in zip(s_rgb, e_rgb))
|
|
43
|
+
return f"#{mix[0]:02x}{mix[1]:02x}{mix[2]:02x}"
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def scale_sequential(
|
|
47
|
+
counts: Mapping[str, int],
|
|
48
|
+
region_ids: list[str],
|
|
49
|
+
palette: list[str] = SEQUENTIAL_BLUE,
|
|
50
|
+
neutral_color: str = NEUTRALS["fill"],
|
|
51
|
+
max_count: int | None = None,
|
|
52
|
+
) -> dict[str, str]:
|
|
53
|
+
"""Create a sequential color scale based on counts.
|
|
54
|
+
|
|
55
|
+
Regions with count=0 get the neutral color.
|
|
56
|
+
Regions with counts are colored from palette[0] (low) to palette[-1] (high).
|
|
57
|
+
|
|
58
|
+
Args:
|
|
59
|
+
counts: Mapping of region_id to count
|
|
60
|
+
region_ids: All region IDs to include
|
|
61
|
+
palette: Color palette to use (default: SEQUENTIAL_BLUE)
|
|
62
|
+
neutral_color: Color for regions with count=0
|
|
63
|
+
max_count: Fixed maximum for scaling. If None, uses dynamic max from counts.
|
|
64
|
+
For interactive visualizations, use a fixed value to prevent
|
|
65
|
+
other regions from changing color when one region is clicked.
|
|
66
|
+
|
|
67
|
+
Returns:
|
|
68
|
+
Mapping of region_id to hex color
|
|
69
|
+
"""
|
|
70
|
+
# Determine the maximum count for scaling
|
|
71
|
+
if max_count is None:
|
|
72
|
+
# Dynamic: use the actual maximum from current counts
|
|
73
|
+
max_count = max(counts.values(), default=0)
|
|
74
|
+
|
|
75
|
+
if max_count <= 0:
|
|
76
|
+
return {region_id: neutral_color for region_id in region_ids}
|
|
77
|
+
|
|
78
|
+
fills = {}
|
|
79
|
+
for region_id in region_ids:
|
|
80
|
+
count = counts.get(region_id, 0)
|
|
81
|
+
# Use continuous color interpolation for smooth visual feedback
|
|
82
|
+
# Interpolate between palette colors based on count
|
|
83
|
+
if count == 0:
|
|
84
|
+
fills[region_id] = palette[0]
|
|
85
|
+
elif count >= max_count:
|
|
86
|
+
fills[region_id] = palette[-1]
|
|
87
|
+
else:
|
|
88
|
+
# Map count to continuous position in palette
|
|
89
|
+
t = count / max_count # Normalize to [0, 1]
|
|
90
|
+
palette_pos = t * (len(palette) - 1) # Map to [0, palette_size-1]
|
|
91
|
+
|
|
92
|
+
# Interpolate between adjacent palette colors
|
|
93
|
+
lower_idx = int(palette_pos)
|
|
94
|
+
upper_idx = min(lower_idx + 1, len(palette) - 1)
|
|
95
|
+
blend_factor = palette_pos - lower_idx # Fraction between colors
|
|
96
|
+
|
|
97
|
+
fills[region_id] = lerp_hex(palette[lower_idx], palette[upper_idx], blend_factor)
|
|
98
|
+
|
|
99
|
+
return fills
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def scale_diverging(
|
|
103
|
+
values: Mapping[str, float],
|
|
104
|
+
region_ids: list[str],
|
|
105
|
+
low_color: str = "#ef4444", # red
|
|
106
|
+
mid_color: str = "#f3f4f6", # gray
|
|
107
|
+
high_color: str = "#3b82f6", # blue
|
|
108
|
+
midpoint: float = 0.0,
|
|
109
|
+
) -> dict[str, str]:
|
|
110
|
+
"""Create a diverging color scale (red-white-blue style).
|
|
111
|
+
|
|
112
|
+
Args:
|
|
113
|
+
values: Mapping of region_id to numeric value
|
|
114
|
+
region_ids: All region IDs to include
|
|
115
|
+
low_color: Color for low values
|
|
116
|
+
mid_color: Color for midpoint value
|
|
117
|
+
high_color: Color for high values
|
|
118
|
+
midpoint: Value that maps to mid_color
|
|
119
|
+
|
|
120
|
+
Returns:
|
|
121
|
+
Mapping of region_id to hex color
|
|
122
|
+
"""
|
|
123
|
+
fills = {}
|
|
124
|
+
val_list = [v for v in values.values() if v is not None]
|
|
125
|
+
if not val_list:
|
|
126
|
+
return {region_id: mid_color for region_id in region_ids}
|
|
127
|
+
|
|
128
|
+
min_val, max_val = min(val_list), max(val_list)
|
|
129
|
+
|
|
130
|
+
for region_id in region_ids:
|
|
131
|
+
value = values.get(region_id)
|
|
132
|
+
if value is None:
|
|
133
|
+
fills[region_id] = mid_color
|
|
134
|
+
elif value <= midpoint:
|
|
135
|
+
# Interpolate from low_color to mid_color
|
|
136
|
+
if min_val < midpoint:
|
|
137
|
+
t = (value - min_val) / (midpoint - min_val)
|
|
138
|
+
else:
|
|
139
|
+
t = 0.0
|
|
140
|
+
fills[region_id] = lerp_hex(low_color, mid_color, t)
|
|
141
|
+
else:
|
|
142
|
+
# Interpolate from mid_color to high_color
|
|
143
|
+
if max_val > midpoint:
|
|
144
|
+
t = (value - midpoint) / (max_val - midpoint)
|
|
145
|
+
else:
|
|
146
|
+
t = 0.0
|
|
147
|
+
fills[region_id] = lerp_hex(mid_color, high_color, t)
|
|
148
|
+
|
|
149
|
+
return fills
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def scale_qualitative(
|
|
153
|
+
categories: Mapping[str, str | None],
|
|
154
|
+
region_ids: list[str],
|
|
155
|
+
palette: list[str] = QUALITATIVE,
|
|
156
|
+
neutral_color: str = NEUTRALS["fill"],
|
|
157
|
+
) -> dict[str, str]:
|
|
158
|
+
"""Create a qualitative color scale for categorical data.
|
|
159
|
+
|
|
160
|
+
Args:
|
|
161
|
+
categories: Mapping of region_id to category name (or None)
|
|
162
|
+
region_ids: All region IDs to include
|
|
163
|
+
palette: Color palette to use (cycles if more categories than colors)
|
|
164
|
+
neutral_color: Color for regions with no category
|
|
165
|
+
|
|
166
|
+
Returns:
|
|
167
|
+
Mapping of region_id to hex color
|
|
168
|
+
"""
|
|
169
|
+
# Build category -> color mapping
|
|
170
|
+
unique_cats = sorted({cat for cat in categories.values() if cat is not None})
|
|
171
|
+
cat_colors = {cat: palette[i % len(palette)] for i, cat in enumerate(unique_cats)}
|
|
172
|
+
|
|
173
|
+
fills = {}
|
|
174
|
+
for region_id in region_ids:
|
|
175
|
+
cat = categories.get(region_id)
|
|
176
|
+
if cat is None:
|
|
177
|
+
fills[region_id] = neutral_color
|
|
178
|
+
else:
|
|
179
|
+
fills[region_id] = cat_colors[cat]
|
|
180
|
+
|
|
181
|
+
return fills
|