Skip to content

graphical.ridgeline

RidgelineChart

A console renderable to draw a ridgeline chart.

Parameters:

Name Type Description Default
title str

Ttile of the ridgeline chart. Appears at the top.

required
value_range Optional[Tuple[float, float]]

Range of values. Defaults to None.

None
color Union[Color, str]

Color of the ridgelines. Defaults to "default".

'default'
ticks Optional[Tuple[float, float]]

Max and min ticks on y-axis. Defaults to None.

None
plot_style OneLinePlotStyle

Data representation syle. Defaults to OneLinePlotStyle.AREA.

AREA
box Box

Style of the containing box. Defaults to HEAVY.

HEAVY
Source code in src/graphical/ridgeline.py
class RidgelineChart:
    """A console renderable to draw a ridgeline chart.

    Args:
        title (str): Ttile of the ridgeline chart. Appears at the top.
        value_range (Optional[Tuple[float, float]], optional): Range of values. Defaults to None.
        color (Union[Color, str], optional): Color of the ridgelines. Defaults to "default".
        ticks (Optional[Tuple[float, float]], optional): Max and min ticks on y-axis. Defaults to None.
        plot_style (OneLinePlotStyle, optional): Data representation syle. Defaults to OneLinePlotStyle.AREA.
        box (Box, optional): Style of the containing box. Defaults to HEAVY.
    """

    def __init__(
        self,
        title: str,
        value_range: Optional[Tuple[float, float]] = None,
        color: Union[Color, str] = "default",
        ticks: Optional[Tuple[float, float]] = None,
        plot_style: OneLinePlotStyle = OneLinePlotStyle.AREA,
        box: Box = HEAVY,
    ):
        self.title = title
        self.value_range = value_range
        self.style = Style(color=color)
        self.ticks = ticks
        self.plot_style = plot_style
        self.box = box
        self.rows: List[RidgelineRow] = []

    def add_row(
        self,
        label: str,
        values: List[float],
        color: Union[Color, str] = "default",
        plot_style: Optional[OneLinePlotStyle] = None,
    ) -> RidgelineRow:
        """Add a ridgeline to the chart.

        Args:
            label (str): Label for the ridgeline row.
            values (List[float]): Data values for ridgeline row.
            color (Union[Color, str], optional): Color of the rigeline row. Defaults to "default".
            plot_style (Optional[OneLinePlotStyle], optional): Data representation syle. Defaults to None.

        Returns:
            RidgelineRow: The added row.
        """
        row = RidgelineRow(label, values, color, plot_style)
        self.rows.append(row)
        return row

    def __rich_console__(
        self, console: Console, options: ConsoleOptions
    ) -> RenderResult:
        width_graphs = max(len(d.values) for d in self.rows)
        if self.value_range:
            value_range = self.value_range
        else:
            row_min, row_max = zip(*[row.value_range() for row in self.rows])
            value_range = min(row_min), max(row_max)

        chart = LabelChartRenderer(title=self.title, ticks=self.ticks, box=self.box)
        for row in self.rows:
            row_style = Style(color=row.color) if row.color != "default" else self.style
            plot_style = row.plot_style if row.plot_style else self.plot_style
            content = Sparkline(
                values=row.values,
                value_range=value_range,
                color=row_style.color or "default",
                bgcolor=row_style.bgcolor or "default",
                plot_style=plot_style,
                end="",
            )
            chart.add_row(content=content, content_width=width_graphs, label=row.label)
        yield from chart.render()

    def __rich_measure__(
        self, console: Console, options: ConsoleOptions
    ) -> Measurement:
        width_labels = max(len(d.label) for d in self.rows) + 1
        width_graphs = max(len(d.values) for d in self.rows)
        width_border = 2
        width = width_labels + width_graphs + width_border
        return Measurement(width, width)

add_row(label, values, color='default', plot_style=None)

Add a ridgeline to the chart.

Parameters:

Name Type Description Default
label str

Label for the ridgeline row.

required
values List[float]

Data values for ridgeline row.

required
color Union[Color, str]

Color of the rigeline row. Defaults to "default".

'default'
plot_style Optional[OneLinePlotStyle]

Data representation syle. Defaults to None.

None

Returns:

Name Type Description
RidgelineRow RidgelineRow

The added row.

Source code in src/graphical/ridgeline.py
def add_row(
    self,
    label: str,
    values: List[float],
    color: Union[Color, str] = "default",
    plot_style: Optional[OneLinePlotStyle] = None,
) -> RidgelineRow:
    """Add a ridgeline to the chart.

    Args:
        label (str): Label for the ridgeline row.
        values (List[float]): Data values for ridgeline row.
        color (Union[Color, str], optional): Color of the rigeline row. Defaults to "default".
        plot_style (Optional[OneLinePlotStyle], optional): Data representation syle. Defaults to None.

    Returns:
        RidgelineRow: The added row.
    """
    row = RidgelineRow(label, values, color, plot_style)
    self.rows.append(row)
    return row