Skip to content

Commit

Permalink
Merge pull request #404 from technyon/master
Browse files Browse the repository at this point in the history
Add AppendDataBySeriesNameAsync() method
  • Loading branch information
joadan authored Feb 9, 2024
2 parents bef2c1c + 47c77ff commit da77b9b
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/Blazor-ApexCharts/ApexChart.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,45 @@ public virtual async Task AppendDataAsync(IEnumerable<TItem> items)
await InvokeVoidJsAsync("blazor_apexchart.appendData", Options.Chart.Id, json);
}

/// <summary>
/// This method allows you to append new data to the series arrays, referencing each series by name
/// </summary>
/// <param name="items">The dictinoary with the data array to append the existing series datasets, using the key as the series name.</param>
/// <remarks>
/// Links:
///
/// <see href="https://apexcharts.github.io/Blazor-ApexCharts/methods/append-data">Blazor Example</see>,
/// <see href="https://apexcharts.com/docs/methods/#appendData">JavaScript Documentation</see>
/// </remarks>
public virtual async Task AppendDataBySeriesNameAsync(Dictionary<string, IEnumerable<TItem>> items)
{
if (IsNoAxisChart)
{
throw new Exception($"{typeof(ApexChart<TItem>)}.{nameof(AppendDataBySeriesNameAsync)}: Operation not valid for no axis charts.");
}

var seriesList = new List<AppendData<TItem>>();

foreach (var apxSeries in Options.Series)
{
if(items.ContainsKey(apxSeries.Name))
{
var data = apxSeries.ApexSeries.GenerateDataPoints(items[apxSeries.Name]);
var updatedData = apxSeries.Data.ToList();
updatedData.AddRange(data);
apxSeries.Data = updatedData;

seriesList.Add(new AppendData<TItem>
{
Data = data
}); ;
}
}

var json = Serialize(seriesList);
await InvokeVoidJsAsync("blazor_apexchart.appendData", Options.Chart.Id, json);
}

/// <summary>
/// Manually zoom into the chart with the start and end X values.
/// </summary>
Expand Down

0 comments on commit da77b9b

Please sign in to comment.