Skip to content

Legends

legends

ContinuousColorscaleLegend

Bases: ContinuousLegendBase[SegmentedContinuousColorscale]

Legend for continuous color mappings

Source code in submodules/mesqual/mesqual/visualizations/folium_viz_system/legends/continuous_color.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class ContinuousColorscaleLegend(ContinuousLegendBase[SegmentedContinuousColorscale]):
    """Legend for continuous color mappings"""

    def specific_visual_styles(self) -> str:
        # no extra CSS beyond base
        return ""

    def create_segment_visual(self, start: float, end: float, colors: Any) -> str:
        if isinstance(colors, list):
            if len(colors) == 1:
                gradient = f"background: {colors[0]};"
            else:
                stops = [
                    f"{color} {100 * i / (len(colors) - 1):.1f}%"
                    for i, color in enumerate(colors)
                ]
                gradient = f"background: linear-gradient(to right, {', '.join(stops)});"
        else:
            gradient = f"background: {colors};"
        return f'<div class="segment-visual" style="{gradient}"></div>'

DiscreteColorLegend

Bases: DiscreteLegendBase[DiscreteColorMapping]

Legend for discrete color mappings

Source code in submodules/mesqual/mesqual/visualizations/folium_viz_system/legends/discrete_color.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class DiscreteColorLegend(DiscreteLegendBase[DiscreteColorMapping]):
    """Legend for discrete color mappings"""

    def __init__(
            self,
            mapping: DiscreteColorMapping,
            swatch_size: int = 20,
            **kwargs
    ):
        super().__init__(mapping, **kwargs)
        self.swatch_size = swatch_size

    def specific_visual_styles(self) -> str:
        id_selector = f"#{self.get_name()}"
        return f"""
            {id_selector} .color-swatch {{
                width: {self.swatch_size}px;
                height: {self.swatch_size}px;
                border: 1px solid #ccc;
                border-radius: 2px;
            }}
        """

    def create_visual_element(self, color: str) -> str:
        return f'<div class="color-swatch" style="background-color: {color};"></div>'

DiscreteLineDashLegend

Bases: DiscreteLegendBase[DiscreteLineDashPatternMapping]

Legend for line dash pattern mappings

Source code in submodules/mesqual/mesqual/visualizations/folium_viz_system/legends/discrete_line_dash.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class DiscreteLineDashLegend(DiscreteLegendBase[DiscreteLineDashPatternMapping]):
    """Legend for line dash pattern mappings"""

    def __init__(
            self,
            mapping: DiscreteLineDashPatternMapping,
            line_color: str = "#000000",
            line_width: int = 2,
            **kwargs
    ):
        super().__init__(mapping, **kwargs)
        self.line_color = line_color
        self.line_width = line_width

    def specific_visual_styles(self) -> str:
        id_selector = f"#{self.get_name()}"
        return f"""
            {id_selector} svg {{
                /* you can put shared SVG styling here if needed */
            }}
        """

    def create_visual_element(self, pattern: str) -> str:
        dash_array = f'stroke-dasharray="{pattern}"' if pattern else ""
        return f"""
            <svg width="{self.visual_column_width}" height="{self.line_width * 2}">
                <line x1="0" y1="{self.line_width}" x2="{self.visual_column_width}" y2="{self.line_width}"
                      stroke="{self.line_color}" stroke-width="{self.line_width}" {dash_array}/>
            </svg>
        """

DiscreteLineWidthLegend

Bases: DiscreteLegendBase[DiscreteLineWidthMapping]

Legend for discrete line width mappings

Source code in submodules/mesqual/mesqual/visualizations/folium_viz_system/legends/discrete_line_width.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class DiscreteLineWidthLegend(DiscreteLegendBase[DiscreteLineWidthMapping]):
    """Legend for discrete line width mappings"""

    def __init__(
            self,
            mapping: DiscreteLineWidthMapping,
            line_color: str = "#000000",
            show_pixel_values: bool = False,
            **kwargs
    ):
        super().__init__(mapping, **kwargs)
        self.line_color = line_color
        self.show_pixel_values = show_pixel_values

    def specific_visual_styles(self) -> str:
        id_selector = f"#{self.get_name()}"
        return f"""
            {id_selector} .pixel-value {{
                font-size: {self.label_font_size - 2}px;
                color: {self.title_color};
                opacity: 0.7;
                margin-left: 5px;
            }}
        """

    def create_visual_element(self, width: float) -> str:
        height = max(10, width * 2)
        return f"""
            <svg width="{self.visual_column_width}" height="{height}">
                <line x1="0" y1="{height / 2}" x2="{self.visual_column_width}" y2="{height / 2}"
                      stroke="{self.line_color}" stroke-width="{width}"/>
            </svg>
        """

    def render_content(self) -> str:
        items = []

        # Override to add pixel values
        for key, width in sorted(self.mapping.mapping.items()):
            visual = self.create_visual_element(width)
            label = self._format_value(key)
            if self.show_pixel_values:
                label += f'<span class="pixel-value">({width}px)</span>'

            items.append(f"""
                <div class="discrete-item">
                    <div class="visual-column">{visual}</div>
                    <div class="label-column">{label}</div>
                </div>
            """)

        if self.mapping.default_output is not None:
            width = self.mapping.default_output
            visual = self.create_visual_element(width)
            label = "Other"
            if self.show_pixel_values:
                label += f'<span class="pixel-value">({width}px)</span>'

            items.append(f"""
                <div class="discrete-item">
                    <div class="visual-column">{visual}</div>
                    <div class="label-column">{label}</div>
                </div>
            """)

        return ''.join(items)