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
50 changes: 44 additions & 6 deletions branca/colormap.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,29 @@ class ColorMap(MacroElement):
The right bound of the color scale.
caption: str
A caption to draw with the colormap.
text_color: str, default "black"
The color for the text.
max_labels : int, default 10
Maximum number of legend tick labels
"""

_template = ENV.get_template("color_scale.js")

def __init__(self, vmin=0.0, vmax=1.0, caption="", max_labels=10):
def __init__(
self,
vmin=0.0,
vmax=1.0,
caption="",
text_color="black",
max_labels=10,
):
super().__init__()
self._name = "ColorMap"

self.vmin = vmin
self.vmax = vmax
self.caption = caption
self.text_color = text_color
self.index = [vmin, vmax]
self.max_labels = max_labels
self.tick_labels = None
Expand Down Expand Up @@ -185,22 +195,32 @@ def _repr_html_(self):
for i in range(self.width)
],
)
+ '<text x="0" y="38" style="text-anchor:start; font-size:11px; font:Arial">{}</text>'.format( # noqa
+ (
'<text x="0" y="38" style="text-anchor:start; font-size:11px;'
' font:Arial; fill:{}">{}</text>'
).format(
self.text_color,
self.vmin,
)
+ "".join(
[
(
'<text x="{}" y="38"; style="text-anchor:middle; font-size:11px; font:Arial">{}</text>' # noqa
).format(x_ticks[i], val_ticks[i])
'<text x="{}" y="38"; style="text-anchor:middle; font-size:11px;'
' font:Arial; fill:{}">{}</text>'
).format(x_ticks[i], self.text_color, val_ticks[i])
for i in range(1, nb_ticks - 1)
],
)
+ '<text x="{}" y="38" style="text-anchor:end; font-size:11px; font:Arial">{}</text>'.format(
+ (
'<text x="{}" y="38" style="text-anchor:end; font-size:11px;'
' font:Arial; fill:{}">{}</text>'
).format(
self.width,
self.text_color,
self.vmax,
)
+ '<text x="0" y="12" style="font-size:11px; font:Arial">{}</text>'.format(
+ '<text x="0" y="12" style="font-size:11px; font:Arial; fill:{}">{}</text>'.format(
self.text_color,
self.caption,
)
+ "</svg>"
Expand Down Expand Up @@ -233,6 +253,10 @@ class LinearColormap(ColorMap):
vmax : float, default 1.
The maximal value for the colormap.
Values higher than `vmax` will be bound directly to `colors[-1]`.
caption: str
A caption to draw with the colormap.
text_color: str, default "black"
The color for the text.
max_labels : int, default 10
Maximum number of legend tick labels
tick_labels: list of floats, default None
Expand All @@ -245,13 +269,15 @@ def __init__(
vmin=0.0,
vmax=1.0,
caption="",
text_color="black",
max_labels=10,
tick_labels=None,
):
super().__init__(
vmin=vmin,
vmax=vmax,
caption=caption,
text_color=text_color,
max_labels=max_labels,
)
self.tick_labels = tick_labels
Expand Down Expand Up @@ -415,13 +441,15 @@ def to_step(
]

caption = self.caption
text_color = self.text_color

return StepColormap(
colors,
index=index,
vmin=index[0],
vmax=index[-1],
caption=caption,
text_color=text_color,
max_labels=max_labels,
tick_labels=self.tick_labels,
)
Expand All @@ -439,6 +467,7 @@ def scale(self, vmin=0.0, vmax=1.0, max_labels=10):
vmin=vmin,
vmax=vmax,
caption=self.caption,
text_color=self.text_color,
max_labels=max_labels,
)

Expand Down Expand Up @@ -469,6 +498,10 @@ class StepColormap(ColorMap):
vmax : float, default 1.
The maximal value for the colormap.
Values higher than `vmax` will be bound directly to `colors[-1]`.
caption: str
A caption to draw with the colormap.
text_color: str, default "black"
The color for the text.
max_labels : int, default 10
Maximum number of legend tick labels
tick_labels: list of floats, default None
Expand All @@ -482,13 +515,15 @@ def __init__(
vmin=0.0,
vmax=1.0,
caption="",
text_color="black",
max_labels=10,
tick_labels=None,
):
super().__init__(
vmin=vmin,
vmax=vmax,
caption=caption,
text_color=text_color,
max_labels=max_labels,
)
self.tick_labels = tick_labels
Expand Down Expand Up @@ -544,6 +579,8 @@ def to_linear(self, index=None, max_labels=10):
index=index,
vmin=self.vmin,
vmax=self.vmax,
caption=self.caption,
text_color=self.text_color,
max_labels=max_labels,
)

Expand All @@ -560,6 +597,7 @@ def scale(self, vmin=0.0, vmax=1.0, max_labels=10):
vmin=vmin,
vmax=vmax,
caption=self.caption,
text_color=self.text_color,
max_labels=max_labels,
)

Expand Down
2 changes: 2 additions & 0 deletions branca/templates/color_scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

{{this.get_name()}}.g = {{this.get_name()}}.svg.append("g")
.attr("class", "key")
.attr("fill", {{ this.text_color | tojson }})
.attr("transform", "translate(25,16)");

{{this.get_name()}}.g.selectAll("rect")
Expand All @@ -51,5 +52,6 @@
{{this.get_name()}}.g.call({{this.get_name()}}.xAxis).append("text")
.attr("class", "caption")
.attr("y", 21)
.attr("fill", {{ this.text_color | tojson }})
.text({{ this.caption|tojson }});
{% endmacro %}
7 changes: 4 additions & 3 deletions examples/Custom_colormap.ipynb

Large diffs are not rendered by default.