using System.Globalization; using Connected.Annotations; using Connected.Extensions; using Connected.Utilities; using Microsoft.AspNetCore.Components; namespace Connected.Components; public partial class Chart : UIComponent { [Parameter] [Category(CategoryTypes.Chart.Behavior)] public double[] InputData { get; set; } = Array.Empty(); [Parameter] [Category(CategoryTypes.Chart.Behavior)] public string[] InputLabels { get; set; } = Array.Empty(); [Parameter] [Category(CategoryTypes.Chart.Behavior)] public string[] XAxisLabels { get; set; } = Array.Empty(); [Parameter] [Category(CategoryTypes.Chart.Behavior)] public List ChartSeries { get; set; } = new(); [Parameter] [Category(CategoryTypes.Chart.Appearance)] public ChartOptions ChartOptions { get; set; } = new(); /// /// RenderFragment for costumization inside the chart's svg. /// [Parameter] [Category(CategoryTypes.Chart.Appearance)] public RenderFragment CustomGraphics { get; set; } protected string Classname => new CssBuilder("mud-chart") .AddClass($"mud-chart-legend-{ConvertLegendPosition(LegendPosition).ToDescriptionString()}") .AddClass(Class) .Build(); [CascadingParameter(Name = "RightToLeft")] public bool RightToLeft { get; set; } /// /// The Type of the chart. /// [Parameter] [Category(CategoryTypes.Chart.Behavior)] public ChartType ChartType { get; set; } /// /// The Width of the chart, end with % or px. /// [Parameter] [Category(CategoryTypes.Chart.Appearance)] public string Width { get; set; } = "80%"; /// /// The Height of the chart, end with % or px. /// [Parameter] [Category(CategoryTypes.Chart.Appearance)] public string Height { get; set; } = "80%"; /// /// The placement direction of the legend if used. /// [Parameter] [Category(CategoryTypes.Chart.Appearance)] public Position LegendPosition { get; set; } = Position.Bottom; private Position ConvertLegendPosition(Position position) { return position switch { Position.Start => RightToLeft ? Position.Right : Position.Left, Position.End => RightToLeft ? Position.Left : Position.Right, _ => position }; } private int _selectedIndex; /// /// Selected index of a portion of the chart. /// [Parameter] [Category(CategoryTypes.Chart.Behavior)] public int SelectedIndex { get => _selectedIndex; set { if (value != _selectedIndex) { _selectedIndex = value; SelectedIndexChanged.InvokeAsync(value); } } } /// /// Selected index of a portion of the chart. /// [Parameter] public EventCallback SelectedIndexChanged { get; set; } /// /// Scales the input data to the range between 0 and 1 /// protected double[] GetNormalizedData() { if (InputData == null) return Array.Empty(); var total = InputData.Sum(); return InputData.Select(x => Math.Abs(x) / total).ToArray(); } protected string ToS(double d, string format = null) { if (string.IsNullOrEmpty(format)) return d.ToString(CultureInfo.InvariantCulture); return d.ToString(format); } } public enum ChartType { Donut, Line, Pie, Bar }