Skip to content

[Feature]: Create BarChart component #159

@TeseySTD

Description

@TeseySTD

Feature Description

Create component wrapper for BarChart renderable.

Problem Statement

No response

Proposed Solution

Component with this api (not implemented XML docs yet):

@namespace RazorConsole.Components

@using Spectre.Console

<barchart
    data-items="@ItemsJson"
    data-width="@WidthAttribute"
    data-label="@Label"
    data-label-style="@LabelStyleAttributes"
    data-label-alignment="@LabelAlignmentAttribute"
    data-max-value="@MaxValueAttribute"
    data-show-values="@ShowValuesAttribute"
/>


@code{
    [Parameter]
    [EditorRequired]
    public required List<IBarChartItem> BarChartItems { get; set; }

    [Parameter]
    public int? Width { get; set; }

    [Parameter]
    public string? Label { get; set; }

    [Parameter]
    public Color LabelForeground { get; set; } = Style.Plain.Foreground;

    [Parameter]
    public Color LabelBackground { get; set; } = Style.Plain.Background;

    [Parameter]
    public Decoration LabelDecoration { get; set; } = Decoration.None;


    [Parameter]
    public Justify? LabelAlignment { get; set; }

    [Parameter]
    public double? MaxValue { get; set; }

    [Parameter]
    public bool ShowValues { get; set; }

    private string ItemsJson => System.Text.Json.JsonSerializer.Serialize(
        BarChartItems.Select(item => new
            {
                item.Label,
                item.Value,
                Color = item.Color?.ToHex() ?? Color.White.ToHex()
            }
        ).ToList<object>()
    );

    private string WidthAttribute => Width?.ToString() ?? string.Empty;

    private string LabelStyleAttributes => new Style(LabelForeground, LabelBackground, LabelDecoration).ToMarkup();

    private string LabelAlignmentAttribute => LabelAlignment?.ToString().ToLowerInvariant() ?? string.Empty;

    private string MaxValueAttribute => MaxValue?.ToString() ?? string.Empty;

    private string ShowValuesAttribute => ShowValues ? "true" : "false";
}

Alternatives Considered

No response

Additional Context

No response

Contribution

  • I'd be willing to contribute this feature

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions