Skip to content

Commit

Permalink
Merge pull request #154 from anmcgrath/number-rounding
Browse files Browse the repository at this point in the history
Number rounding
  • Loading branch information
anmcgrath authored Dec 11, 2024
2 parents 33e41d7 + fd301f6 commit 35d714e
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 148 deletions.
1 change: 1 addition & 0 deletions src/BlazorDatasheet/Datasheet.razor
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
Context="vr">
<RowTemplate>
<DatasheetGridRow
NumberPrecisionDisplay="_numberPrecisionDisplay"
Row="vr.Row"
VisibleColIndices="vr.VisibleColIndices"
Sheet="_sheet"
Expand Down
17 changes: 15 additions & 2 deletions src/BlazorDatasheet/Datasheet.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ public partial class Datasheet : SheetComponentBase, IAsyncDisposable
[Parameter]
public bool Virtualise { get; set; } = true;

/// <summary>
/// The number of decimal places to round a number to. Default is 15.
/// </summary>
[Parameter]
public int NumberPrecisionDisplay { get; set; } = 15;

private int _numberPrecisionDisplay = 15;

/// <summary>
/// The datasheet keyboard shortcut manager
/// </summary>
Expand Down Expand Up @@ -310,6 +318,11 @@ protected override async Task OnParametersSetAsync()
requireRender = true;
}

if (NumberPrecisionDisplay != _numberPrecisionDisplay)
{
_numberPrecisionDisplay = Math.Min(15, NumberPrecisionDisplay);
}

if (requireRender)
{
_sheetIsDirty = true;
Expand Down Expand Up @@ -466,9 +479,9 @@ private void MakeRegionsDirty(IEnumerable<IRegion?> dirtyRegions)
foreach (var col in _sheet.Columns.GetVisibleIndices(boundedRegion.Left, boundedRegion.Right))
{
var position = new CellPosition(row, col);
if (!_visualCellCache.TryAdd(position, new VisualCell(row, col, _sheet)))
if (!_visualCellCache.TryAdd(position, new VisualCell(row, col, _sheet, _numberPrecisionDisplay)))
{
_visualCellCache[position] = new VisualCell(row, col, _sheet);
_visualCellCache[position] = new VisualCell(row, col, _sheet, _numberPrecisionDisplay);
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/BlazorDatasheet/DatasheetGridRow.razor
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
var dataRow = Row;

if (!Cache.TryGetValue(new CellPosition(Row, col), out var visualCell))
visualCell = new VisualCell(Row, col, Sheet);
visualCell = new VisualCell(Row, col, Sheet, NumberPrecisionDisplay);

if (visualCell.Merge != null)
{
Expand Down Expand Up @@ -86,6 +86,8 @@
[CascadingParameter(Name = "CustomCellTypeDefinitions")]
public Dictionary<string, CellTypeDefinition> CustomCellTypeDefinitions { get; set; } = default!;

[Parameter, EditorRequired] public required int NumberPrecisionDisplay { get; set; }

protected override bool ShouldRender() => IsDirty || _colIndicesChanged;
private bool _colIndicesChanged = false;

Expand All @@ -97,6 +99,7 @@
{
_colIndicesChanged = CompareIndices(_visibleColIndices, VisibleColIndices);
}

_visibleColIndices = VisibleColIndices;
}

Expand Down
2 changes: 1 addition & 1 deletion src/BlazorDatasheet/Edit/EditorLayer.razor
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
private string GetEditorDisplayStyling()
{
var sb = new StyleBuilder();
var visualCell = new VisualCell(_sheet.Editor.EditCell!.Row, _sheet.Editor.EditCell!.Col, Sheet);
var visualCell = new VisualCell(_sheet.Editor.EditCell!.Row, _sheet.Editor.EditCell!.Col, Sheet, 15);
var format = visualCell.Format;

sb.AddStyleNotNull("background", format?.BackgroundColor);
Expand Down
4 changes: 3 additions & 1 deletion src/BlazorDatasheet/Render/Layers/VirtualMergesLayer.razor
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
var col = merge.Left;

if (!Cache.TryGetValue(new CellPosition(row, col), out var visualCell))
visualCell = new VisualCell(row, col, Sheet);
visualCell = new VisualCell(row, col, Sheet, NumberPrecisionDisplay);

if (visualCell.VisibleColSpan <= 1 && visualCell.VisibleRowSpan <= 1)
{
Expand Down Expand Up @@ -75,6 +75,8 @@
[CascadingParameter(Name = "CustomCellTypeDefinitions")]
public Dictionary<string, CellTypeDefinition> CustomCellTypeDefinitions { get; set; } = default!;

[Parameter, EditorRequired] public required int NumberPrecisionDisplay { get; set; }

protected override void OnSheetChange(Sheet newSheet, Sheet oldSheet)
{
oldSheet.Cells.CellsChanged -= CellsOnCellsChanged;
Expand Down
30 changes: 21 additions & 9 deletions src/BlazorDatasheet/Render/VisualCell.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Text;
using BlazorDatasheet.Core.Commands.Formatting;
using System.Globalization;
using BlazorDatasheet.Core.Data;
using BlazorDatasheet.Core.Formats;
using BlazorDatasheet.DataStructures.Geometry;
Expand All @@ -11,7 +10,7 @@ namespace BlazorDatasheet.Render;
public class VisualCell
{
public object? Value { get; private set; }
public string FormattedString { get; private set; }
public string FormattedString { get; private set; } = string.Empty;
public int Row { get; private set; }
public int Col { get; private set; }
public IRegion? Merge { get; private set; }
Expand All @@ -22,14 +21,21 @@ public class VisualCell
public bool IsVisible { get; set; }
public int VisibleRowSpan { get; set; } = 1;
public int VisibleColSpan { get; set; } = 1;
public bool IsMergeStart { get; set; } = false;
public bool IsMergeStart { get; set; }
public int VisibleMergeRowStart { get; set; }
public int VisibleMergeColStart { get; set; }
public double Height { get; set; }
public double Width { get; set; }


public VisualCell(int row, int col, Sheet sheet)
/// <summary>
/// Create a visual cell, which has formatting properties calculated for the cell.
/// </summary>
/// <param name="row">The row of the cell</param>
/// <param name="col">The column of the cell</param>
/// <param name="sheet">The sheet that the cell is inside.</param>
/// <param name="numberOfSignificantDigits">The number of digits to round the displayed number to.</param>
internal VisualCell(int row, int col, Sheet sheet, int numberOfSignificantDigits)
{
Merge = sheet.Cells.GetMerge(row, col)?.GetIntersection(sheet.Region);

Expand All @@ -50,8 +56,14 @@ public VisualCell(int row, int col, Sheet sheet)
var cellValue = sheet.Cells.GetCellValue(row, col);
Value = cellValue.Data;

if (cellValue.ValueType == CellValueType.Number && format.NumberFormat != null)
FormattedString = (cellValue.GetValue<double>()).ToString(format.NumberFormat);
if (cellValue.ValueType == CellValueType.Number)
{
var roundedNumber = Math.Round(cellValue.GetValue<double>(), numberOfSignificantDigits);
if (format.NumberFormat != null)
FormattedString = roundedNumber.ToString(format.NumberFormat);
else
FormattedString = roundedNumber.ToString(CultureInfo.InvariantCulture);
}
else if (cellValue.ValueType == CellValueType.Date && format.NumberFormat != null)
FormattedString = (cellValue.GetValue<DateTime>()).ToString(format.NumberFormat);
else
Expand All @@ -70,7 +82,7 @@ public VisualCell(int row, int col, Sheet sheet)
IsVisible = cell.IsVisible;

FormatStyleString = GetCellFormatStyleString(Row, Col, format, cell.IsValid, cellValue.ValueType);
Icon = format?.Icon;
Icon = format.Icon;
CellType = cell.Type;
Format = format;
}
Expand Down Expand Up @@ -126,7 +138,7 @@ private static string GetCellFormatStyleString(int row, int col, CellFormat? for
else if (format.HorizontalTextAlign == TextAlign.Center)
sb.AddStyle("justify-content", "center");
}

if (format.VerticalTextAlign != null)
{
if (format.VerticalTextAlign == TextAlign.Start)
Expand Down
134 changes: 0 additions & 134 deletions src/BlazorDatasheet/Render/VisualSheet.cs

This file was deleted.

0 comments on commit 35d714e

Please sign in to comment.