@nestjsforge/echarts

Functions in Options

Use JavaScript functions inside ECharts option objects via function serialization.

ECharts supports JavaScript functions in options (e.g., formatter, symbolSize, color callbacks). Since options are serialized to the headless browser, @nestjsforge/echarts provides a utility to safely embed functions.

Only pass functions you control. Function code is serialized and executed in the headless Chromium context. Never pass user-supplied function strings.

fn() helper

import { EChartsService, fn } from '@nestjsforge/echarts';

const option = {
  series: [{
    type: 'pie',
    label: {
      formatter: fn((params: any) => `${params.name}: ${params.percent}%`),
    },
    symbolSize: fn((val: number[]) => Math.sqrt(val[2]) * 2),
    data: [
      { value: 335, name: 'Direct' },
      { value: 310, name: 'Email' },
    ],
  }],
};

const buffer = await this.echarts.renderToBuffer(option);

The fn() helper serializes the function to a string token that the renderer recognizes and deserializes before passing to ECharts. Arrow functions and regular functions are both supported.