Handling external data for Blazorise Charts

47 Views Asked by At

I am currently facing a problem where I pretty much don't know a next step. I am trying to load data that comes from a database. I load the data in OnInitializedAsync (see below). According to the Blazorise docs, you call the HandleRedraw in the OnAfterRenderAsync method with a check if it is the first render. However, when I try it this way my data is always NULL. Here is the code snippet described.

protected override async Task OnInitializedAsync()
{
    stateContainer.PropertyChanged += StateContainer_PropertyChanged;

    using var dbHelper = new TelemetryDataHelper(dbContextFactory.CreateDbContext(), appCache);
    eventNameCountByNameData = await dbHelper.GetEventNameCountByNameAsync().ConfigureAwait(false);
    eventNameCountByVersionData = await dbHelper.GetEventNameCountByVersionAsync().ConfigureAwait(false);
    await UpdateDatasetCountByNameAsync().ConfigureAwait(false);

    //Extract primary Versions
    eventNameCountByVersion = eventNameCountByVersionData
                                .Select(x => new ItemCount<string>(x.Key.Substring(0, x.Key.IndexOf('.', x.Key.IndexOf('.') + 1)), x.Count))
                                .GroupBy(x => x.Key, (key, group) => new ItemCount<string>(key, group.Sum(x => x.Count)))
                                .ToList();
    await AddPercentageToCountByVersionAsync().ConfigureAwait(false);
    isLoading = false;
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
    await base.OnAfterRenderAsync(firstRender).ConfigureAwait(false);
    if (firstRender)
    {
        await HandleRedrawCountByNameAsync().ConfigureAwait(false);
        await HandleRedrawCountByVersionAsync().ConfigureAwait(false);
    }
}
private async Task HandleRedrawCountByNameAsync()
{
    await barChartCountByName.Clear().ConfigureAwait(false);
    await barChartCountByName.SetOptions(chartService.GetBarChartOptionsBeginAtZero()).ConfigureAwait(false);
    await barChartCountByName.AddLabelsDatasetsAndUpdate(GetLabelsCountByName(), GetBarChartDatasetCountByName()).ConfigureAwait(false);
}
private async Task UpdateDatasetCountByNameAsync()
{
    await Task.Run(() =>
    {
        if (stateContainer.Version == "All")
        {
            eventNameCountByName = eventNameCountByNameData
                .GroupBy(x => x.Key, (key, group) => new ItemCount<string>(key, group.Sum(x => x.Count)))
                .ToList();
        }
        else
        {
            eventNameCountByName = eventNameCountByNameData
                .Where(x => x.Version.StartsWith(stateContainer.Version))
                .GroupBy(x => x.Key, (key, group) => new ItemCount<string>(key, group.Sum(x => x.Count)))
                .ToList();
        }
    }).ConfigureAwait(false);
}

I am looking for a solution to fix this issue that the data is loaded before the redraw but I don't have an idea how I should/can do it.

0

There are 0 best solutions below