Table of Contents

Class BarGraphControl

Namespace
SharpConsoleUI.Controls
Assembly
SharpConsoleUI.dll

A horizontal bar graph control for visualizing percentage-based data. Displays a filled/unfilled bar with optional label, value, and custom colors.

public class BarGraphControl : BaseControl, IWindowControl, IDisposable, IDOMPaintable, INotifyPropertyChanged
Inheritance
BarGraphControl
Implements
Inherited Members
Extension Methods

Constructors

BarGraphControl()

Initializes a new instance of the BarGraphControl class.

public BarGraphControl()

Properties

BackgroundColor

Gets or sets the background color of the control. When null, inherits from the container.

public Color? BackgroundColor { get; set; }

Property Value

Color?

BarWidth

Gets or sets the width of the bar in characters.

public int BarWidth { get; set; }

Property Value

int

ColorThresholds

Gets the current color thresholds for gradient effect.

public IReadOnlyList<ColorThreshold>? ColorThresholds { get; }

Property Value

IReadOnlyList<ColorThreshold>

Container

Gets or sets the parent container that hosts this control.

public override IContainer? Container { get; set; }

Property Value

IContainer

ContentWidth

Gets the minimum width needed to display the control's content, including margins. Returns null if width cannot be determined. This is calculated based on content (text length, child controls, etc.) and represents the natural/intrinsic size.

public override int? ContentWidth { get; }

Property Value

int?

FilledColor

Gets or sets the color for the filled portion of the bar.

public Color FilledColor { get; set; }

Property Value

Color

ForegroundColor

Gets or sets the foreground color for labels and values. When null, inherits from the container.

public Color? ForegroundColor { get; set; }

Property Value

Color?

Label

Gets or sets the label text displayed before the bar.

public string Label { get; set; }

Property Value

string

LabelWidth

Gets or sets the fixed width for the label column in characters. When set, labels are padded or truncated to this width, ensuring bars align vertically. When null (default), the label uses its natural length.

public int? LabelWidth { get; set; }

Property Value

int?

Margin

Gets or sets the margin (spacing) around the control.

public override Margin Margin { get; set; }

Property Value

Margin

MaxValue

Gets or sets the maximum value for the bar (100% fill).

public double MaxValue { get; set; }

Property Value

double

ShowLabel

Gets or sets whether to show the label.

public bool ShowLabel { get; set; }

Property Value

bool

ShowValue

Gets or sets whether to show the value after the bar.

public bool ShowValue { get; set; }

Property Value

bool

SmoothGradient

Gets or sets the smooth color gradient for horizontal color interpolation. When set, the bar smoothly transitions from start to end color based on fill percentage. This is in addition to (not replacement of) threshold-based gradients. Threshold gradients take precedence when both are set.

public ColorGradient? SmoothGradient { get; set; }

Property Value

ColorGradient

UnfilledColor

Gets or sets the color for the unfilled portion of the bar.

public Color UnfilledColor { get; set; }

Property Value

Color

Value

Gets or sets the current value to display.

public double Value { get; set; }

Property Value

double

ValueFormat

Gets or sets the format string for displaying the value (e.g., "F1", "F2", "0").

public string ValueFormat { get; set; }

Property Value

string

Width

Gets or sets the explicit width of the control, or null for automatic sizing.

public override int? Width { get; set; }

Property Value

int?

Methods

ClearColorThresholds()

Clears all color thresholds, reverting to single FilledColor.

public void ClearColorThresholds()

GetLogicalContentSize()

Gets the logical size of the control's content without rendering.

public override Size GetLogicalContentSize()

Returns

Size

The size representing the content's natural dimensions.

MeasureDOM(LayoutConstraints)

Measures the control's desired size given the available constraints.

public override LayoutSize MeasureDOM(LayoutConstraints constraints)

Parameters

constraints LayoutConstraints

The layout constraints (min/max width/height).

Returns

LayoutSize

The desired size of the control.

PaintDOM(CharacterBuffer, LayoutRect, LayoutRect, Color, Color)

Paints the control's content directly to a CharacterBuffer.

public override void PaintDOM(CharacterBuffer buffer, LayoutRect bounds, LayoutRect clipRect, Color defaultFg, Color defaultBg)

Parameters

buffer CharacterBuffer

The buffer to paint to.

bounds LayoutRect

The absolute bounds where the control should paint.

clipRect LayoutRect

The clipping rectangle (visible area).

defaultFg Color
defaultBg Color

SetColorThresholds(params ColorThreshold[])

Sets color thresholds for gradient effect. Colors apply when the bar percentage is greater than or equal to the threshold. Example: (0, Green), (50, Yellow), (80, Red) shows green 0-49%, yellow 50-79%, red 80%+.

public void SetColorThresholds(params ColorThreshold[] thresholds)

Parameters

thresholds ColorThreshold[]

Color thresholds ordered by percentage. Will be auto-sorted.