LCLA Context
An LclaContext is the shared charting environment for a client application. It owns the connection to the renderer, the available datasets, and any charts created from it. Create one context, then create as many charts as your application needs. This lets charts use the same data without copying it for every view.
Every context requires an LclaLicense. Supply a LightningChart JS license key when creating it. Application deployment licenses can also include the application title and company information.
Flutter currently uses a different model: create charts directly, with each chart acting as its own context. Consequently, Flutter can only support up to 16 charts at once.
Creating a context
- Blazor
- MAUI
- Uno
- Flutter
Create the context in your .razor component after the chart container has rendered.
This example reads the license key from the application's LCJS_LICENSE_KEY configuration setting:
@using LightningChart.LA.Api
@using LightningChart.LA.Blazor
@using Microsoft.JSInterop
@inject IJSRuntime JSRuntime
@inject IConfiguration Configuration
@code {
private LclaContext? _context;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (!firstRender) return;
var license = new LclaLicense
{
Key = Configuration["LCJS_LICENSE_KEY"]!,
};
_context = new LclaContext(new BlazorTransport(JSRuntime), license);
}
}
Continue to Adding charts to create a chart.
Use a WebView to display charts. Start its transport when the page loads, then create the context after navigation succeeds.
This example reads the license key from the LCJS_LICENSE_KEY environment variable supplied to the application:
using LightningChart.LA.Api;
using LightningChart.LA.WebView;
using Microsoft.Maui.Controls;
public sealed class SignalPage : ContentPage
{
private readonly WebView _webView = new();
private WebViewTransport? _transport;
private LclaContext? _context;
public SignalPage()
{
Content = _webView;
_webView.Navigated += async (_, args) =>
{
if (args.Result != WebNavigationResult.Success ||
_transport is null ||
_context is not null)
{
return;
}
var licenseKey = Environment.GetEnvironmentVariable("LCJS_LICENSE_KEY")
?? throw new InvalidOperationException("LCJS_LICENSE_KEY is not configured.");
var license = new LclaLicense
{
Key = licenseKey,
};
_context = new LclaContext(_transport, license);
};
Loaded += async (_, _) =>
{
if (_transport is not null) return;
_transport = await WebViewTransport.StartAsync();
_webView.Source = _transport.Uri.AbsoluteUri;
};
}
}
Continue to Adding charts to add chart creation inside the navigation handler, after context initialization.
Use a WebView2 to display charts. Start its transport when the page loads, then create the context after navigation succeeds.
This example reads the license key from the LCJS_LICENSE_KEY environment variable supplied to the application:
using LightningChart.LA.Api;
using LightningChart.LA.WebView;
using Microsoft.UI.Xaml.Controls;
public sealed class SignalPage : Page
{
private readonly WebView2 _webView = new();
private WebViewTransport? _transport;
private LclaContext? _context;
public SignalPage()
{
Content = _webView;
_webView.NavigationCompleted += async (_, args) =>
{
if (!args.IsSuccess ||
_transport is null ||
_context is not null)
{
return;
}
var licenseKey = Environment.GetEnvironmentVariable("LCJS_LICENSE_KEY")
?? throw new InvalidOperationException("LCJS_LICENSE_KEY is not configured.");
var license = new LclaLicense
{
Key = licenseKey,
};
_context = new LclaContext(_transport, license);
};
Loaded += async (_, _) =>
{
if (_transport is not null) return;
_transport = await WebViewTransport.StartAsync();
_webView.Source = _transport.Uri;
};
}
}
Continue to Adding charts to add chart creation inside the navigation handler, after context initialization.
Flutter creates the context through the chart widget. You do not construct an LclaContext separately.
Prepare the license configuration to pass to the widget:
import 'package:lightning_chart_flutter/lightning_chart_flutter.dart';
const license = LclaLicense(
key: String.fromEnvironment('LCJS_LICENSE_KEY'),
);
Supply the key when running the application:
flutter run -d chrome --dart-define=LCJS_LICENSE_KEY=your-license-key
Continue to Adding charts to create the widget and receive its chart controller through onChartCreated.
Disposing a context
- Blazor
- MAUI
- Uno
- Flutter
Add this directive at the top of your .razor component:
@implements IAsyncDisposable
Add the following method inside the component's existing @code block:
public async ValueTask DisposeAsync()
{
if (_context is not null)
await _context.DisposeAsync();
}
Blazor calls this method when the component is removed. Disposing _context releases the charts it owns.
Make the page implement IAsyncDisposable:
public sealed class SignalPage : ContentPage, IAsyncDisposable
Add the following method inside the page. Dispose the context before disposing its transport:
public async ValueTask DisposeAsync()
{
if (_context is not null)
{
await _context.DisposeAsync();
_context = null;
}
if (_transport is not null)
{
await _transport.DisposeAsync();
_transport = null;
}
}
Add this event subscription inside the page's constructor to release the resources when the page unloads:
Unloaded += async (_, _) => await DisposeAsync();
If your application streams data, stop that operation before disposing the context.
Make the page implement IAsyncDisposable:
public sealed class SignalPage : Page, IAsyncDisposable
Add the following method inside the page. Dispose the context before disposing its transport:
public async ValueTask DisposeAsync()
{
if (_context is not null)
{
await _context.DisposeAsync();
_context = null;
}
if (_transport is not null)
{
await _transport.DisposeAsync();
_transport = null;
}
}
Add this event subscription inside the page's constructor to release the resources when the page unloads:
Unloaded += async (_, _) => await DisposeAsync();
If your application streams data, stop that operation before disposing the context.
The chart widget releases its chart and context resources when removed from the widget tree. You do not dispose the context separately.
Cancel any application-owned data subscriptions in the surrounding widget's dispose method.