A Blazor chart library, based on G2Plot
A Blazor chart library, based on G2Plot
A Blazor chart library, based on G2Plot
English | 简体中文
Go to the project folder of the application and install the Nuget package reference
$ dotnet add package AntDesign.Charts_Imports.razor@using AntDesign.ChartsFinally, it can be referenced in the `.razor' component!
…When configuring charts, you can define JavaScript functions for properties like formatter or properties ending with Func. These functions will be properly converted from string representations to actual JavaScript functions.
The following JavaScript function syntaxes are supported:
Standard function declarations:
config.Tooltip.Formatter = "function(datum) { return { name: datum.year, value: '$' + datum.value.toFixed(2) }; }";Arrow functions:
config.Tooltip.Formatter = "(datum) => { return { name: datum.year, value: '$' + datum.value.toFixed(2) }; }";Concise arrow functions (implicit return):
config.Tooltip.Formatter = "datum => '$' + datum.value.toFixed(2)";Simple expressions:
config.Meta.Value.Formatter = "'$' + datum * 100";JavaScript functions can also be defined within objects inside arrays:
config.Annotations = new[]
{
new
{
type = "text",
position = new[] { "min", "median" },
content = "Middle point",
formatter = "function(item) { return item.value.toFixed(2); }"
}
};Charts support various events that you can handle in your Blazor code. You can use the On and Once methods to subscribe to chart events.
…Common chart events include:
plot:click - Triggered when clicking on the chartplot:dblclick - Triggered when double-clicking on the chartplot:mousemove - Triggered when moving the mouse over the chartplot:mouseenter - Triggered when the mouse enters the chart areaplot:mouseleave - Triggered when the mouse leaves the chart areaelement:click - Triggered when clicking on a chart element (e.g., line, bar)legend-item:click - Triggered when clicking on a legend itemUse Once to subscribe to an event that should only be handled once:
await chartRef.Once("plot:click", (data) =>
{
// This handler will only be called once
Console.WriteLine($"First click at x: {data.X}, y: {data.Y}");
});The event data is passed as System.Text.Json.JsonElement by default, which you can handle directly or deserialize into a specific type using the generic methods. The actual structure of the event data depends on the event type and chart type.
A typical event data structure looks like this:
…Example of handling event data:
chartRef.On("element:click", (data) =>
{
Console.WriteLine($"Clicked element: Type={data.Data.Type}, Sales={data.Data.Sales}");
Console.WriteLine($"Position: X={data.X}, Y={data.Y}");
Console.WriteLine($"Style: Color={data.Color}, Shape={data.Shape}");
});You can unsubscribe from events using the Off method. There are two ways to use it:
@code {
IChartComponent chartRef;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
// Subscribe to the event with a method
chartRef.On("plot:click", HandleChartClick);
// Later, unsubscribe this specific handler
chartRef.Off("plot:click", HandleChartClick);
}
}
private void HandleChartClick(JsonElement data)
{
Console.WriteLine($"Clicked: {data}");
}
}// Unsubscribe from all handlers of a specific event
await chartRef.Off("plot:click");
// Unsubscribe from all event handlers
await chartRef.Off();It's good practice to unsubscribe from events when they are no longer needed or when the component is being disposed:
…If you would like to contribute, feel free to create a Pull Request, or give us Bug Report.
This project is an MIT-licensed open source project. In order to achieve better and sustainable development of the project, we expect to gain more backers. We will use the proceeds for community operations and promotion. You can support us in any of the following ways:
We will put the detailed donation records on the backer list.
If you encounter any problems in the process, feel free to ask for help via following channels. We also encourage experienced users to help newcomers.
No open issues yet, or sync has not completed.