百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
A

ant-design-charts-blazor

> 编程语言
开源

基于 G2Plot 的 Blazor 图表库

185 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

基于 G2Plot 的 Blazor 图表库

ant-design-charts-blazor

A Blazor chart library, based on G2Plot

English | 简体中文

Current Version

  • Release:
  • Development:

Installation Guide

  • Go to the project folder of the application and install the Nuget package reference

    bash
    $ dotnet add package AntDesign.Charts
    • Add namespace in _Imports.razor
    csharp
    @using AntDesign.Charts
  • Finally, it can be referenced in the `.razor' component!

…

Using JavaScript Functions in Configuration

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.

Supported Function Syntax

The following JavaScript function syntaxes are supported:

  1. Standard function declarations:

    csharp
    config.Tooltip.Formatter = "function(datum) { return { name: datum.year, value: '$' + datum.value.toFixed(2) }; }";
  2. Arrow functions:

    csharp
    config.Tooltip.Formatter = "(datum) => { return { name: datum.year, value: '$' + datum.value.toFixed(2) }; }";
  3. Concise arrow functions (implicit return):

    csharp
    config.Tooltip.Formatter = "datum => '$' + datum.value.toFixed(2)";
  4. Simple expressions:

    csharp
    config.Meta.Value.Formatter = "'$' + datum * 100";

Using Functions with Array Properties

JavaScript functions can also be defined within objects inside arrays:

csharp
config.Annotations = new[]
{
    new
    {
        type = "text",
        position = new[] { "min", "median" },
        content = "Middle point",
        formatter = "function(item) { return item.value.toFixed(2); }"
    }
};

Event Handling

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.

Basic Event Usage

…

Available Events

Common chart events include:

  • plot:click - Triggered when clicking on the chart
  • plot:dblclick - Triggered when double-clicking on the chart
  • plot:mousemove - Triggered when moving the mouse over the chart
  • plot:mouseenter - Triggered when the mouse enters the chart area
  • plot:mouseleave - Triggered when the mouse leaves the chart area
  • element:click - Triggered when clicking on a chart element (e.g., line, bar)
  • legend-item:click - Triggered when clicking on a legend item

One-time Event Handling

Use Once to subscribe to an event that should only be handled once:

csharp
await chartRef.Once("plot:click", (data) =>
{
    // This handler will only be called once
    Console.WriteLine($"First click at x: {data.X}, y: {data.Y}");
});

Event Data

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:

csharp
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}");
});

Unsubscribing from Events

You can unsubscribe from events using the Off method. There are two ways to use it:

  1. Unsubscribe from a specific event handler:
csharp
@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}");
    }
}
  1. Unsubscribe from all handlers of a specific event or all events:
csharp
// 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:

…

Links

  • Official Blazor Documentation

Contributing

If you would like to contribute, feel free to create a Pull Request, or give us Bug Report.

Donation

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:

  • OpenCollective
  • Wechat
  • Alipay

We will put the detailed donation records on the backer list.

❓ Community Support

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.

  • (Chinese & English)

  • (Chinese)

    Scan QR Code with DingTalk

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C#blazorblazor-componentscharts

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言