@nestjsforge/echarts

Quick Start

Get up and running with @nestjsforge/echarts in minutes.

This guide gets you from installation to rendering your first chart in under 5 minutes.

1. Import EChartsModule

Register the module in your root or feature module using forRoot:

app.module.ts
import { Module } from '@nestjs/common';
import { EChartsModule } from '@nestjsforge/echarts';

@Module({
  imports: [
    EChartsModule.forRoot({
      poolSize: 2,
      timeout: 15_000,
    }),
  ],
})
export class AppModule {}

2. Inject EChartsService

Inject EChartsService into any service or controller:

charts.service.ts
import { Injectable } from '@nestjs/common';
import { EChartsService } from '@nestjsforge/echarts';

@Injectable()
export class ChartsService {
  constructor(private readonly echarts: EChartsService) {}

  async renderBarChart(): Promise<Buffer> {
    return this.echarts.renderToBuffer({
      backgroundColor: '#1a1a2e',
      xAxis: {
        type: 'category',
        data: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
      },
      yAxis: { type: 'value' },
      series: [
        {
          name: 'Revenue',
          type: 'bar',
          data: [820, 932, 901, 934, 1290, 1330],
          itemStyle: { color: '#E0234E' },
        },
      ],
    });
  }
}

3. Serve the chart via HTTP

charts.controller.ts
import { Controller, Get, Res } from '@nestjs/common';
import { Response } from 'express';
import { ChartsService } from './charts.service';

@Controller('charts')
export class ChartsController {
  constructor(private readonly charts: ChartsService) {}

  @Get('bar')
  async getBarChart(@Res() res: Response): Promise<void> {
    const buffer = await this.charts.renderBarChart();
    res.setHeader('Content-Type', 'image/png');
    res.setHeader('Content-Length', buffer.length);
    res.end(buffer);
  }
}

4. Test it

Test the endpoint
# Start your NestJS app
npm run start:dev

# Fetch the chart
curl http://localhost:3000/charts/bar -o chart.png
open chart.png

You can also use renderToBase64() for embedding in JSON responses, renderToDataUrl() for HTML img tags, or renderToFile() to write directly to disk.