Blazorise Chart Streaming component
Stream live data into Chart.js charts.
📊 The Community License has a limitation of 10 points per chart. Unlock full access to all features with
our premium plans.
Live streaming is made possible with the help of chartjs-plugin-streaming.
To use Chart Streaming, install the Blazorise.Charts.Streaming package. It depends on Blazorise.Charts.
Installation
NuGet
Install chart streaming extension from NuGet.Install-Package Blazorise.Charts.Streaming
Imports
In your main _Imports.razor add:
@using Blazorise.Charts
@using Blazorise.Charts.Streaming
Examples
Basic Example
<LineChart @ref="horizontalLineChart" TItem="LiveDataPoint" OptionsObject=""> <ChartStreaming TItem="LiveDataPoint" Options="new ChartStreamingOptions { Delay = 2000 }" Refreshed="" /> </LineChart>
@code { LineChart<LiveDataPoint> horizontalLineChart; Random random = new Random( DateTime.Now.Millisecond ); private static string[] Labels = { "A", "B", "C", "D", "E", "F" }; private static List<string> BackgroundColors = new() { ChartColor.FromRgba( 76, 110, 245, 0.25f ), // Indigo ChartColor.FromRgba( 18, 184, 134, 0.25f ), // Teal ChartColor.FromRgba( 245, 159, 0, 0.25f ), // Amber ChartColor.FromRgba( 240, 62, 62, 0.25f ), // Red ChartColor.FromRgba( 132, 94, 247, 0.25f ), // Purple ChartColor.FromRgba( 34, 139, 230, 0.25f ) // Blue }; private static List<string> BorderColors = new() { ChartColor.FromRgba( 76, 110, 245, 1f ), // Indigo ChartColor.FromRgba( 18, 184, 134, 1f ), // Teal ChartColor.FromRgba( 245, 159, 0, 1f ), // Amber ChartColor.FromRgba( 240, 62, 62, 1f ), // Red ChartColor.FromRgba( 132, 94, 247, 1f ), // Purple ChartColor.FromRgba( 34, 139, 230, 1f ) // Blue }; public struct LiveDataPoint { public object X { get; set; } public object Y { get; set; } } object horizontalLineChartOptions = new { Scales = new { Y = new { Title = new { Display = true, Text = "Value" } } }, Interaction = new { intersect = false } }; protected override async Task OnAfterRenderAsync( bool firstRender ) { if ( firstRender ) { await Task.WhenAll( HandleRedraw( horizontalLineChart, GetLineChartDataset1 ) ); } } async Task HandleRedraw<TDataSet, TItem, TOptions, TModel>( BaseChart<TDataSet, TItem, TOptions, TModel> chart, params Func<TDataSet>[] getDataSets ) where TDataSet : ChartDataset<TItem> where TOptions : ChartOptions where TModel : ChartModel { await chart.Clear(); await chart.AddLabelsDatasetsAndUpdate( Labels, getDataSets.Select( x => x.Invoke() ).ToArray() ); } LineChartDataset<LiveDataPoint> GetLineChartDataset1() { return new LineChartDataset<LiveDataPoint> { Data = new List<LiveDataPoint>(), Label = "Dataset 1 (linear interpolation)", BackgroundColor = BackgroundColors[0], BorderColor = BorderColors[0], Fill = false, Tension = 0, BorderDash = new List<int> { 8, 4 }, }; } Task OnHorizontalLineRefreshed( ChartStreamingData<LiveDataPoint> data ) { data.Value = new LiveDataPoint { X = DateTime.Now, Y = RandomScalingFactor(), }; return Task.CompletedTask; } double RandomScalingFactor() { return ( random.NextDouble() > 0.5 ? 1.0 : -1.0 ) * Math.Round( random.NextDouble() * 100 ); } }
Pause/Play
Making the stream pause and play is very simple. You only need to add a@ref to the ChartStreaming and the call .Pause() or .Play() methods.
<LineChart @ref="" TItem="LiveDataPoint" OptionsObject=""> <ChartStreaming @ref="" TItem="LiveDataPoint" Options="new ChartStreamingOptions { Delay = 2000 }" Refreshed="" /> </LineChart> <Row> <Column> <Button Color="Color.Primary" Clicked="@(()=>chartStreaming.Pause())">Pause</Button> <Button Color="Color.Primary" Clicked="@(()=>chartStreaming.Play())">Play</Button> </Column> </Row>
@code{ LineChart<LiveDataPoint> horizontalLineChart; ChartStreaming<LiveDataPoint> chartStreaming; Random random = new Random( DateTime.Now.Millisecond ); string[] Labels = { "Red", "Blue", "Yellow", "Green", "Purple", "Orange" }; List<string> backgroundColors = new List<string> { ChartColor.FromRgba( 255, 99, 132, 0.2f ), ChartColor.FromRgba( 54, 162, 235, 0.2f ), ChartColor.FromRgba( 255, 206, 86, 0.2f ), ChartColor.FromRgba( 75, 192, 192, 0.2f ), ChartColor.FromRgba( 153, 102, 255, 0.2f ), ChartColor.FromRgba( 255, 159, 64, 0.2f ) }; List<string> borderColors = new List<string> { ChartColor.FromRgba( 255, 99, 132, 1f ), ChartColor.FromRgba( 54, 162, 235, 1f ), ChartColor.FromRgba( 255, 206, 86, 1f ), ChartColor.FromRgba( 75, 192, 192, 1f ), ChartColor.FromRgba( 153, 102, 255, 1f ), ChartColor.FromRgba( 255, 159, 64, 1f ) }; public struct LiveDataPoint { public object X { get; set; } public object Y { get; set; } } object horizontalLineChartOptions = new { Scales = new { Y = new { Title = new { Display = true, Text = "Value" } } }, Interaction = new { intersect = false } }; protected override async Task OnAfterRenderAsync( bool firstRender ) { if ( firstRender ) { await Task.WhenAll( HandleRedraw( horizontalLineChart, GetLineChartDataset1 ) ); } } async Task HandleRedraw<TDataSet, TItem, TOptions, TModel>( BaseChart<TDataSet, TItem, TOptions, TModel> chart, params Func<TDataSet>[] getDataSets ) where TDataSet : ChartDataset<TItem> where TOptions : ChartOptions where TModel : ChartModel { await chart.Clear(); await chart.AddLabelsDatasetsAndUpdate( Labels, getDataSets.Select( x => x.Invoke() ).ToArray() ); } LineChartDataset<LiveDataPoint> GetLineChartDataset1() { return new LineChartDataset<LiveDataPoint> { Data = new List<LiveDataPoint>(), Label = "Dataset 1 (linear interpolation)", BackgroundColor = backgroundColors[0], BorderColor = borderColors[0], Fill = false, Tension = 0, BorderDash = new List<int> { 8, 4 }, }; } Task OnHorizontalLineRefreshed( ChartStreamingData<LiveDataPoint> data ) { data.Value = new LiveDataPoint { X = DateTime.Now, Y = RandomScalingFactor(), }; return Task.CompletedTask; } double RandomScalingFactor() { return ( random.NextDouble() > 0.5 ? 1.0 : -1.0 ) * Math.Round( random.NextDouble() * 100 ); } }