Skip to content

graphical.bar

Bar

A console renderable to draw a horizontal bar.

Parameters:

Name Type Description Default
value float

Value of the bar.

required
value_range Tuple[float, float]

Range of values of the bar.

required
color Union[Color, str]

Color of the bar. Defaults to "default".

'default'
bgcolor Union[Color, str]

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

'default'
width int

Width representing the full value range. Defaults to 50.

WIDTH
bar_style BarStyle

Data representation syle. Defaults to BarStyle.BLOCK.

BLOCK
end str

End character of bar string. Defaults to newline.

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

    Args:
        value (float): Value of the bar.
        value_range (Tuple[float, float]): Range of values of the bar.
        color (Union[Color, str], optional): Color of the bar. Defaults to "default".
        bgcolor (Union[Color, str]): Background color of the bar. Defaults to "default".
        width (int, optional): Width representing the full value range. Defaults to 50.
        bar_style (BarStyle, optional): Data representation syle. Defaults to BarStyle.BLOCK.
        end (str, optional): End character of bar string. Defaults to newline.
    """

    def __init__(
        self,
        value: float,
        value_range: Tuple[float, float],
        color: Union[Color, str] = "default",
        bgcolor: Union[Color, str] = "default",
        width: int = WIDTH,
        bar_style: BarStyle = BarStyle.BLOCK,
        end: str = "\n",
    ):
        self.value = value
        self.value_range = value_range
        self.width = width
        self.style = Style(color=color, bgcolor=bgcolor)
        self.bar_style = bar_style
        self.end = end

    def __rich_console__(
        self, console: Console, options: ConsoleOptions
    ) -> RenderResult:
        cell_style = self.bar_style.horizontal
        step = self.value_range[1] / self.width
        cells = ""
        for idx in range(self.width):
            cell_range = idx * step, (idx + 1) * step
            cells += PlotCellRenderer.render(
                self.value, value_range=cell_range, cell_style=cell_style
            )
        yield Segment(cells, style=self.style)
        yield Segment(self.end)

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

BarChart

A console renderable to draw a bar chart.

Parameters:

Name Type Description Default
title str

Ttile of the bar chart. Appears at the top.

required
value_range Tuple[float, float]

Range of values of the bar chart.

required
color Union[Color, str]

Color of the bars in the chart. Defaults to "default".

'default'
bgcolor Union[Color, str]

Background color of the bar chart. Defaults to "default".

'default'
width int

Width of the chart. Defaults to 50.

WIDTH
bar_style BarStyle

Data representation syle. Defaults to BarStyle.BLOCK.

BLOCK
box Box

Style of the containing box. Defaults to HEAVY.

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

    Args:
        title (str): Ttile of the bar chart. Appears at the top.
        value_range (Tuple[float, float]): Range of values of the bar chart.
        color (Union[Color, str], optional): Color of the bars in the chart. Defaults to "default".
        bgcolor (Union[Color, str]): Background color of the bar chart. Defaults to "default".
        width (int, optional): Width of the chart. Defaults to 50.
        bar_style (BarStyle, optional): Data representation syle. Defaults to BarStyle.BLOCK.
        box (Box, optional): Style of the containing box. Defaults to HEAVY.
    """

    def __init__(
        self,
        title: str,
        value_range: Tuple[float, float],
        color: Union[Color, str] = "default",
        bgcolor: Union[Color, str] = "default",
        width: int = WIDTH,
        bar_style: BarStyle = BarStyle.BLOCK,
        box: Box = HEAVY,
    ):
        self.title = title
        self.value_range = value_range
        self.color = color
        self.bgcolor = bgcolor
        self.width = width
        self.bar_style = bar_style
        self.box = box
        self.rows: List[BarChartRow] = []

    def add_row(
        self,
        label: str,
        value: float,
        color: Optional[Union[Color, str]] = None,
        bgcolor: Optional[Union[Color, str]] = None,
        bar_style: Optional[BarStyle] = None,
    ) -> BarChartRow:
        """Add a row to the bar chart.

        Args:
            label (str): Label for the bar chart row.
            value (float): Data value for the bar chart row.
            bgcolor (Union[Color, str], optional): Background color of the row. Defaults to "default".
            bar_style (Optional[BarStyle], optional): Data representation. Defaults to bar_style of chart.

        Returns:
            BarChartRow: The added row.
        """
        row = BarChartRow(
            label=label,
            value=value,
            color=color,
            bgcolor=bgcolor,
            bar_style=bar_style,
        )
        self.rows.append(row)
        return row

    def __rich_console__(
        self, console: Console, options: ConsoleOptions
    ) -> RenderResult:
        if self.value_range:
            value_range = 0, self.value_range[1]
        else:
            value_range = 0, max(d.value for d in self.rows)
        width_labels = max(len(d.label) for d in self.rows) + 1
        width_graphs = self.width - width_labels - 2
        chart = LabelChartRenderer(title=self.title, ticks=value_range, box=self.box)
        for row in self.rows:
            content = Bar(
                value=row.value,
                width=width_graphs,
                value_range=value_range,
                color=row.color or self.color,
                bgcolor=row.bgcolor or self.bgcolor,
                bar_style=row.bar_style or self.bar_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:
        return Measurement(self.width, self.width)

add_row(label, value, color=None, bgcolor=None, bar_style=None)

Add a row to the bar chart.

Parameters:

Name Type Description Default
label str

Label for the bar chart row.

required
value float

Data value for the bar chart row.

required
bgcolor Union[Color, str]

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

None
bar_style Optional[BarStyle]

Data representation. Defaults to bar_style of chart.

None

Returns:

Name Type Description
BarChartRow BarChartRow

The added row.

Source code in src/graphical/bar.py
def add_row(
    self,
    label: str,
    value: float,
    color: Optional[Union[Color, str]] = None,
    bgcolor: Optional[Union[Color, str]] = None,
    bar_style: Optional[BarStyle] = None,
) -> BarChartRow:
    """Add a row to the bar chart.

    Args:
        label (str): Label for the bar chart row.
        value (float): Data value for the bar chart row.
        bgcolor (Union[Color, str], optional): Background color of the row. Defaults to "default".
        bar_style (Optional[BarStyle], optional): Data representation. Defaults to bar_style of chart.

    Returns:
        BarChartRow: The added row.
    """
    row = BarChartRow(
        label=label,
        value=value,
        color=color,
        bgcolor=bgcolor,
        bar_style=bar_style,
    )
    self.rows.append(row)
    return row

BarChartRow dataclass

Row of a bar chart.

Source code in src/graphical/bar.py
@dataclass
class BarChartRow:
    """Row of a bar chart."""

    label: str
    value: float
    color: Union[Color, str] = "default"
    bgcolor: Union[Color, str] = "default"
    bar_style: Optional[BarStyle] = None

BarStyle

Bases: Enum

Bar styles for bar charts.

Parameters:

Name Type Description Default
horizontal PlotCellStyle

Data visualization style for horizontal bars.

required
vertical PlotCellStyle

Data visualization style for vertical bars.

required
Source code in src/graphical/bar.py
class BarStyle(Enum):
    """Bar styles for bar charts.

    Args:
        horizontal (PlotCellStyle): Data visualization style for horizontal bars.
        vertical (PlotCellStyle): Data visualization style for vertical bars.
    """

    LIGHT = PlotCellStyle.BAR_LIGHT_H, PlotCellStyle.BAR_LIGHT_V
    HEAVY = PlotCellStyle.BAR_HEAVY_H, PlotCellStyle.BAR_HEAVY_V
    BLOCK = PlotCellStyle.BLOCK_H, PlotCellStyle.BLOCK_V
    SHADE = PlotCellStyle.SHADE, PlotCellStyle.SHADE

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

    def __init__(self, horizontal: PlotCellStyle, vertical: PlotCellStyle):
        self.horizontal = horizontal
        self.vertical = vertical

DivergingBar

A console renderable to draw a diverging horizontal bar.

Parameters:

Name Type Description Default
value float

Value of the bar.

required
value_range Tuple[float, float]

Range of values of the bar.

required
color Union[Color, str]

Color of the bar. Defaults to "default".

'default'
color_negative Optional[Union[Color, str]]

Color of the bar for negative values. Defaults to None.

None
bgcolor Union[Color, str]

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

'default'
width int

Width representing the full value range. Defaults to 50.

WIDTH
bar_style BarStyle

Data representation syle. Defaults to BarStyle.BLOCK.

BLOCK
end str

End character of bar string. Defaults to newline.

'\n'
Source code in src/graphical/bar.py
class DivergingBar:
    """A console renderable to draw a diverging horizontal bar.

    Args:
        value (float): Value of the bar.
        value_range (Tuple[float, float]): Range of values of the bar.
        color (Union[Color, str], optional): Color of the bar. Defaults to "default".
        color_negative (Optional[Union[Color, str]], optional): Color of the bar for negative values. Defaults to None.
        bgcolor (Union[Color, str]): Background color of the bar. Defaults to "default".
        width (int, optional): Width representing the full value range. Defaults to 50.
        bar_style (BarStyle, optional): Data representation syle. Defaults to BarStyle.BLOCK.
        end (str, optional): End character of bar string. Defaults to newline.
    """

    def __init__(
        self,
        value: float,
        value_range: Tuple[float, float],
        color: Union[Color, str] = "default",
        color_negative: Optional[Union[Color, str]] = None,
        bgcolor: Union[Color, str] = "default",
        width: int = WIDTH,
        bar_style: BarStyle = BarStyle.BLOCK,
        end: str = "\n",
    ):
        self.value = value
        self.value_range = value_range
        self.style = Style(color=color, bgcolor=bgcolor)
        self.style_negative = Style(color=color_negative or color, bgcolor=bgcolor)
        self.width = width - width % 2
        self.bar_style = bar_style
        self.end = end

    def __rich_console__(
        self, console: Console, options: ConsoleOptions
    ) -> RenderResult:
        max_abs_value = max(abs(d) for d in self.value_range)
        value_range = -max_abs_value, max_abs_value
        cell_style = self.bar_style.horizontal
        step = (value_range[1] * 2) / self.width
        cells = ""
        for idx in range(self.width):
            cell_range = value_range[0] + idx * step, value_range[0] + (idx + 1) * step
            negative_value = self.value <= 0 and cell_range[0] < 0
            negative_range = self.value > 0 > cell_range[0]
            cells += PlotCellRenderer.render(
                self.value,
                value_range=cell_range,
                cell_style=cell_style,
                invert=negative_value or negative_range,
            )
        style = self.style if self.value > 0 else self.style_negative
        yield Segment(cells, style=style)
        yield Segment(self.end)

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

DivergingBarChart

A console renderable to draw a diverging bar chart.

Parameters:

Name Type Description Default
title str

Ttile of the bar chart. Appears at the top.

required
value_range Tuple[float, float]

Range of values of the bar chart.

required
color Union[Color, str]

Color of the bars in the chart. Defaults to "default".

'default'
color_negative Optional[Union[Color, str]]

Color of the bars for negative values. Defaults to None.

None
bgcolor Union[Color, str]

Background color of the bar chart. Defaults to "default".

'default'
width int

Width of the chart. Defaults to 50.

WIDTH
bar_style BarStyle

Data representation syle. Defaults to BarStyle.BLOCK.

BLOCK
box Box

Style of the containing box. Defaults to HEAVY.

HEAVY
Source code in src/graphical/bar.py
class DivergingBarChart:
    """A console renderable to draw a diverging bar chart.

    Args:
        title (str): Ttile of the bar chart. Appears at the top.
        value_range (Tuple[float, float]): Range of values of the bar chart.
        color (Union[Color, str], optional): Color of the bars in the chart. Defaults to "default".
        color_negative (Optional[Union[Color, str]], optional): Color of the bars for negative values. Defaults to None.
        bgcolor (Union[Color, str]): Background color of the bar chart. Defaults to "default".
        width (int, optional): Width of the chart. Defaults to 50.
        bar_style (BarStyle, optional): Data representation syle. Defaults to BarStyle.BLOCK.
        box (Box, optional): Style of the containing box. Defaults to HEAVY.
    """

    def __init__(
        self,
        title: str,
        value_range: Tuple[float, float],
        color: Union[Color, str] = "default",
        color_negative: Optional[Union[Color, str]] = None,
        bgcolor: Union[Color, str] = "default",
        width: int = WIDTH,
        bar_style: BarStyle = BarStyle.BLOCK,
        box: Box = HEAVY,
    ):
        self.title = title
        self.value_range = value_range
        self.color = color
        self.color_negative = color_negative
        self.bgcolor = bgcolor
        self.width = width
        self.bar_style = bar_style
        self.box = box
        self.rows: List[BarChartRow] = []

    def add_row(
        self,
        label: str,
        value: float,
        bgcolor: Optional[Union[Color, str]] = None,
        bar_style: Optional[BarStyle] = None,
    ) -> BarChartRow:
        """Add a row to the bar chart.

        Args:
            label (str): Label for the bar chart row.
            value (float): Data value for the bar chart row.
            bgcolor (Union[Color, str], optional): Background color of the row. Defaults to "default".
            bar_style (Optional[BarStyle], optional): Data representation. Defaults to bar_style of chart.

        Returns:
            BarChartRow: The added row.
        """
        row = BarChartRow(
            label=label,
            value=value,
            bgcolor=bgcolor,
            bar_style=bar_style,
        )
        self.rows.append(row)
        return row

    def __rich_console__(
        self, console: Console, options: ConsoleOptions
    ) -> RenderResult:
        if self.value_range:
            max_value = max(abs(d) for d in self.value_range)
        else:
            max_value = max(
                max(d.value for d in self.rows), abs(min(d.value for d in self.rows))
            )
        value_range = -max_value, max_value
        width_labels = max(len(d.label) for d in self.rows) + 1
        width_graphs = self.width - width_labels - 2
        width_graphs -= width_graphs % 2
        chart = LabelChartRenderer(title=self.title, ticks=value_range, box=self.box)
        for row in self.rows:
            content = DivergingBar(
                value=row.value,
                width=width_graphs,
                value_range=value_range,
                color=self.color,
                color_negative=self.color_negative,
                bgcolor=row.bgcolor or self.bgcolor,
                bar_style=row.bar_style or self.bar_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:
        return Measurement(self.width, self.width)

add_row(label, value, bgcolor=None, bar_style=None)

Add a row to the bar chart.

Parameters:

Name Type Description Default
label str

Label for the bar chart row.

required
value float

Data value for the bar chart row.

required
bgcolor Union[Color, str]

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

None
bar_style Optional[BarStyle]

Data representation. Defaults to bar_style of chart.

None

Returns:

Name Type Description
BarChartRow BarChartRow

The added row.

Source code in src/graphical/bar.py
def add_row(
    self,
    label: str,
    value: float,
    bgcolor: Optional[Union[Color, str]] = None,
    bar_style: Optional[BarStyle] = None,
) -> BarChartRow:
    """Add a row to the bar chart.

    Args:
        label (str): Label for the bar chart row.
        value (float): Data value for the bar chart row.
        bgcolor (Union[Color, str], optional): Background color of the row. Defaults to "default".
        bar_style (Optional[BarStyle], optional): Data representation. Defaults to bar_style of chart.

    Returns:
        BarChartRow: The added row.
    """
    row = BarChartRow(
        label=label,
        value=value,
        bgcolor=bgcolor,
        bar_style=bar_style,
    )
    self.rows.append(row)
    return row

DoubleBar

A console renderable to draw two horizontal bars.

Parameters:

Name Type Description Default
values List[float]

Values of the bars.

required
value_range Tuple[float, float]

Range of values of the bar.

required
colors List[Union[Color, str]]

Colors of the bars. Defaults to "default".

required
bgcolor Union[Color, str]

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

'default'
width int

Width representing the full value range. Defaults to 50.

WIDTH
end str

End character of bar string. Defaults to newline.

'\n'
Source code in src/graphical/bar.py
class DoubleBar:
    """A console renderable to draw two horizontal bars.

    Args:
        values (List[float]): Values of the bars.
        value_range (Tuple[float, float]): Range of values of the bar.
        colors (List[Union[Color, str]]): Colors of the bars. Defaults to "default".
        bgcolor (Union[Color, str]): Background color of the bars. Defaults to "default".
        width (int, optional): Width representing the full value range. Defaults to 50.
        end (str, optional): End character of bar string. Defaults to newline.
    """

    def __init__(
        self,
        values: List[float],
        value_range: Tuple[float, float],
        colors: List[Union[Color, str]],
        bgcolor: Union[Color, str] = "default",
        width: int = WIDTH,
        end: str = "\n",
    ):
        self.values = values
        self.value_range = value_range
        self.colors = colors
        self.bgcolor = bgcolor
        self.width = width
        self.end = end

    def __rich_console__(
        self, console: Console, options: ConsoleOptions
    ) -> RenderResult:
        step = self.value_range[1] / self.width
        for idx in range(self.width):
            cell_range = idx * step, (idx + 1) * step
            first_lower = self.values[0] > cell_range[1]
            first_match = cell_range[0] < self.values[0] < cell_range[1]
            second_lower = self.values[1] > cell_range[1]
            second_match = cell_range[0] < self.values[1] < cell_range[1]
            if first_lower or first_match:
                color = self.colors[0]
                cell = "â–€"
                if second_lower or second_match:
                    bgcolor = self.colors[1]
                else:
                    bgcolor = self.bgcolor
            else:
                color = self.colors[1]
                bgcolor = self.bgcolor
                if second_lower or second_match:
                    cell = "â–„"
                else:
                    cell = " "
            style = Style(color=color, bgcolor=bgcolor)
            yield Segment(cell, style=style)
        yield Segment(self.end)

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

DoubleBarChart

A console renderable to draw a double bar chart.

Parameters:

Name Type Description Default
title str

Ttile of the bar chart. Appears at the top.

required
value_range Tuple[float, float]

Range of values of the bar chart.

None
colors Union[Color, str]

Color of the bars in the chart. Defaults to "default".

'default'
bgcolor Union[Color, str]

Background color of the bar chart. Defaults to "default".

'default'
width int

Width of the chart. Defaults to 50.

WIDTH
box Box

Style of the containing box. Defaults to HEAVY.

HEAVY
Source code in src/graphical/bar.py
class DoubleBarChart:
    """A console renderable to draw a double bar chart.

    Args:
        title (str): Ttile of the bar chart. Appears at the top.
        value_range (Tuple[float, float]): Range of values of the bar chart.
        colors (Union[Color, str], optional): Color of the bars in the chart. Defaults to "default".
        bgcolor (Union[Color, str]): Background color of the bar chart. Defaults to "default".
        width (int, optional): Width of the chart. Defaults to 50.
        box (Box, optional): Style of the containing box. Defaults to HEAVY.
    """

    def __init__(
        self,
        title: str,
        value_range: Optional[Tuple[float, float]] = None,
        colors: List[Union[Color, str]] = "default",
        bgcolor: Union[Color, str] = "default",
        width: int = WIDTH,
        box: Box = HEAVY,
    ):
        self.title = title
        self.value_range = value_range
        self.colors = colors
        self.bgcolor = bgcolor
        self.width = width
        self.box = box
        self.rows: List[MultiBarChartRow] = []

    def add_row(
        self,
        label: str,
        values: List[float],
        bgcolor: Optional[Union[Color, str]] = None,
    ) -> MultiBarChartRow:
        """Add a row to the bar chart.

        Args:
            label (str): Label for the bar chart row.
            values (List[float]): Data valuse for the bars in the row.
            bgcolor (Union[Color, str], optional): Background color of the row. Defaults to "default".

        Returns:
            MultiChartRow: The added row.
        """
        row = MultiBarChartRow(
            label=label,
            values=values,
            bgcolor=bgcolor,
        )
        self.rows.append(row)
        return row

    def __rich_console__(
        self, console: Console, options: ConsoleOptions
    ) -> RenderResult:
        if self.value_range:
            value_range = 0, self.value_range[1]
        else:
            value_range = 0, max(max(d.values) for d in self.rows)
        width_labels = max(len(d.label) for d in self.rows) + 1
        width_graphs = self.width - width_labels - 2
        chart = LabelChartRenderer(title=self.title, ticks=value_range, box=self.box)
        for row in self.rows:
            content = DoubleBar(
                values=row.values,
                width=width_graphs,
                value_range=value_range,
                colors=row.colors or self.colors,
                bgcolor=row.bgcolor or self.bgcolor,
                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:
        return Measurement(self.width, self.width)

add_row(label, values, bgcolor=None)

Add a row to the bar chart.

Parameters:

Name Type Description Default
label str

Label for the bar chart row.

required
values List[float]

Data valuse for the bars in the row.

required
bgcolor Union[Color, str]

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

None

Returns:

Name Type Description
MultiChartRow MultiBarChartRow

The added row.

Source code in src/graphical/bar.py
def add_row(
    self,
    label: str,
    values: List[float],
    bgcolor: Optional[Union[Color, str]] = None,
) -> MultiBarChartRow:
    """Add a row to the bar chart.

    Args:
        label (str): Label for the bar chart row.
        values (List[float]): Data valuse for the bars in the row.
        bgcolor (Union[Color, str], optional): Background color of the row. Defaults to "default".

    Returns:
        MultiChartRow: The added row.
    """
    row = MultiBarChartRow(
        label=label,
        values=values,
        bgcolor=bgcolor,
    )
    self.rows.append(row)
    return row

MultiBarChartRow dataclass

Row of a multi-bar chart.

Source code in src/graphical/bar.py
@dataclass
class MultiBarChartRow:
    """Row of a multi-bar chart."""

    label: str
    values: List[float]
    colors: List[Union[Color, str]] = None
    bgcolor: Union[Color, str] = "default"
    bar_style: Optional[BarStyle] = None

StackedBar

A console renderable to draw stacked horizontal bars.

Parameters:

Name Type Description Default
values List[float]

Values of the bars.

required
value_range Tuple[float, float]

Range of values of the bar.

required
colors List[Union[Color, str]]

Colors of the bars. Defaults to "default".

required
bgcolor Union[Color, str]

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

'default'
width int

Width representing the full value range. Defaults to 50.

WIDTH
end str

End character of bar string. Defaults to newline.

'\n'
Source code in src/graphical/bar.py
class StackedBar:
    """A console renderable to draw stacked horizontal bars.

    Args:
        values (List[float]): Values of the bars.
        value_range (Tuple[float, float]): Range of values of the bar.
        colors (List[Union[Color, str]], optional): Colors of the bars. Defaults to "default".
        bgcolor (Union[Color, str]): Background color of the bar. Defaults to "default".
        width (int, optional): Width representing the full value range. Defaults to 50.
        end (str, optional): End character of bar string. Defaults to newline.
    """

    def __init__(
        self,
        values: List[float],
        value_range: Tuple[float, float],
        colors: List[Union[Color, str]],
        bgcolor: Union[Color, str] = "default",
        width: int = WIDTH,
        end: str = "\n",
    ):
        self.values = values
        self.value_range = value_range
        self.colors = colors
        self.bgcolor = bgcolor
        self.width = width
        self.end = end

    def __rich_console__(
        self, console: Console, options: ConsoleOptions
    ) -> RenderResult:
        step = self.value_range[1] / self.width
        values = _stacked(self.values)
        colors = [self.colors[d % len(self.colors)] for d in range(len(values))]
        colors.append(self.bgcolor)
        current = 0
        for idx in range(self.width):
            cell_range = idx * step, (idx + 1) * step
            if values[current] < cell_range[0] and current < len(values) - 1:
                current += 1
            cell = PlotCellRenderer.render(
                values[current],
                value_range=cell_range,
                cell_style=PlotCellStyle.BLOCK_H,
            )
            style = Style(color=colors[current], bgcolor=colors[current + 1])
            yield Segment(cell, style)
        yield Segment(self.end)

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

StackedBarChart

A console renderable to draw a stacked bar chart.

Parameters:

Name Type Description Default
title str

Ttile of the bar chart. Appears at the top.

required
value_range Tuple[float, float]

Range of values of the bar chart.

required
colors Union[Color, str]

Color of the bars in the chart. Defaults to "default".

'default'
bgcolor Union[Color, str]

Background color of the bar chart. Defaults to "default".

'default'
width int

Width of the chart. Defaults to 50.

WIDTH
box Box

Style of the containing box. Defaults to HEAVY.

HEAVY
Source code in src/graphical/bar.py
class StackedBarChart:
    """A console renderable to draw a stacked bar chart.

    Args:
        title (str): Ttile of the bar chart. Appears at the top.
        value_range (Tuple[float, float]): Range of values of the bar chart.
        colors (Union[Color, str], optional): Color of the bars in the chart. Defaults to "default".
        bgcolor (Union[Color, str]): Background color of the bar chart. Defaults to "default".
        width (int, optional): Width of the chart. Defaults to 50.
        box (Box, optional): Style of the containing box. Defaults to HEAVY.
    """

    def __init__(
        self,
        title: str,
        value_range: Tuple[float, float],
        colors: List[Union[Color, str]] = "default",
        bgcolor: Union[Color, str] = "default",
        width: int = WIDTH,
        box: Box = HEAVY,
    ):
        self.title = title
        self.value_range = value_range
        self.colors = colors
        self.bgcolor = bgcolor
        self.width = width
        self.box = box
        self.rows: List[MultiBarChartRow] = []

    def add_row(
        self,
        label: str,
        values: List[float],
        bgcolor: Optional[Union[Color, str]] = None,
    ) -> MultiBarChartRow:
        """Add a row to the bar chart.

        Args:
            label (str): Label for the bar chart row.
            values (List[float]): Data valuse for the bars in the row.
            bgcolor (Union[Color, str], optional): Background color of the row. Defaults to "default".

        Returns:
            MultiBarChartRow: The added row.
        """
        row = MultiBarChartRow(
            label=label,
            values=values,
            bgcolor=bgcolor,
        )
        self.rows.append(row)
        return row

    def __rich_console__(
        self, console: Console, options: ConsoleOptions
    ) -> RenderResult:
        if self.value_range:
            value_range = 0, self.value_range[1]
        else:
            value_range = 0, max(max(d.values) for d in self.rows)
        width_labels = max(len(d.label) for d in self.rows) + 1
        width_graphs = self.width - width_labels - 2
        chart = LabelChartRenderer(title=self.title, ticks=value_range, box=self.box)
        for row in self.rows:
            content = StackedBar(
                values=row.values,
                width=width_graphs,
                value_range=value_range,
                colors=row.colors or self.colors,
                bgcolor=row.bgcolor or self.bgcolor,
                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:
        return Measurement(self.width, self.width)

add_row(label, values, bgcolor=None)

Add a row to the bar chart.

Parameters:

Name Type Description Default
label str

Label for the bar chart row.

required
values List[float]

Data valuse for the bars in the row.

required
bgcolor Union[Color, str]

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

None

Returns:

Name Type Description
MultiBarChartRow MultiBarChartRow

The added row.

Source code in src/graphical/bar.py
def add_row(
    self,
    label: str,
    values: List[float],
    bgcolor: Optional[Union[Color, str]] = None,
) -> MultiBarChartRow:
    """Add a row to the bar chart.

    Args:
        label (str): Label for the bar chart row.
        values (List[float]): Data valuse for the bars in the row.
        bgcolor (Union[Color, str], optional): Background color of the row. Defaults to "default".

    Returns:
        MultiBarChartRow: The added row.
    """
    row = MultiBarChartRow(
        label=label,
        values=values,
        bgcolor=bgcolor,
    )
    self.rows.append(row)
    return row