Skip to content

graphical.sparkline

OneLinePlotStyle

Bases: Enum

Styles for one-line plots.

Parameters:

Name Type Description Default
cell_style PlotCellStyle

One of the plot styles defined in PlotCellStyle.

required
Source code in src/graphical/sparkline.py
class OneLinePlotStyle(Enum):
    """Styles for one-line plots.

    Args:
        cell_style (PlotCellStyle): One of the plot styles defined in PlotCellStyle.
    """

    LINE = PlotCellStyle.LINE_V
    """Style for line plots."""
    AREA = PlotCellStyle.BLOCK_V
    """Style for area plots"""
    HORIZON = PlotCellStyle.BLOCK_V
    """Style for horizon plots."""
    SHADE = PlotCellStyle.SHADE
    """Style for shaded plots."""

    def __new__(cls, *args, **kwargs):
        value = len(cls.__members__) + 1
        obj = object.__new__(cls)
        obj._value_ = value
        return obj

    def __init__(self, cell_style: PlotCellStyle):
        self.cell_style = cell_style

AREA = PlotCellStyle.BLOCK_V class-attribute instance-attribute

Style for area plots

HORIZON = PlotCellStyle.BLOCK_V class-attribute instance-attribute

Style for horizon plots.

LINE = PlotCellStyle.LINE_V class-attribute instance-attribute

Style for line plots.

SHADE = PlotCellStyle.SHADE class-attribute instance-attribute

Style for shaded plots.

Sparkline

A console renderable to draw a sparkline.

Parameters:

Name Type Description Default
values List[float]

Values displayed in the sparkline.

required
value_range Tuple[float, float]

Range of values. Defaults to None

None
color Union[Color, str]

Color of the sparkline. Defaults to "default".

'default'
bgcolor Union[Color, str]

Background color of the sparkline. Defaults to "default".

'default'
plot_style OneLinePlotStyle

Data representation syle. Defaults to OneLinePlotStyle.AREA.

AREA
end str

End character. Defaults to newline.

'\n'
Source code in src/graphical/sparkline.py
class Sparkline:
    """A console renderable to draw a sparkline.

    Args:
        values (List[float]): Values displayed in the sparkline.
        value_range (Tuple[float, float], optional): Range of values. Defaults to None
        color (Union[Color, str]): Color of the sparkline. Defaults to "default".
        bgcolor (Union[Color, str]): Background color of the sparkline. Defaults to "default".
        plot_style (OneLinePlotStyle): Data representation syle. Defaults to OneLinePlotStyle.AREA.
        end (str): End character. Defaults to newline.
    """

    def __init__(
        self,
        values: List[float],
        value_range: Optional[Tuple[float, float]] = None,
        color: Union[Color, str] = "default",
        bgcolor: Union[Color, str] = "default",
        plot_style: OneLinePlotStyle = OneLinePlotStyle.AREA,
        end: str = "\n",
    ):
        self.values = values
        self.value_range = value_range
        self.style = Style(color=color, bgcolor=bgcolor)
        self.plot_style = plot_style
        self.end = end

    def __rich_console__(
        self, console: Console, options: ConsoleOptions
    ) -> RenderResult:
        value_range = self.value_range or (min(self.values), max(self.values))
        cell_style = self.plot_style.cell_style

        if self.plot_style == OneLinePlotStyle.HORIZON:
            lower, upper = value_range
            mid = lower + (upper - lower) / 2
            mid_shade = Color.from_triplet(
                blend_rgb(
                    self.style.bgcolor.get_truecolor(),
                    self.style.color.get_truecolor(),
                    0.5,
                )
            )
            lower_style = Style(bgcolor=self.style.bgcolor, color=mid_shade)
            upper_style = Style(bgcolor=mid_shade, color=self.style.color)
            for value in self.values:
                cell = PlotCellRenderer.render(
                    value=value,
                    value_range=(lower, mid) if value < mid else (mid, upper),
                    cell_style=cell_style,
                )
                yield Segment(cell, style=lower_style if value < mid else upper_style)
        else:
            cells = ""
            for value in self.values:
                cells += PlotCellRenderer.render(
                    value=value, value_range=value_range, cell_style=cell_style
                )
            yield Segment(cells, self.style)
        yield Segment(self.end)

    def __rich_measure__(
        self, console: Console, options: ConsoleOptions
    ) -> Measurement:
        width = len(self.values)
        return Measurement(width, width)