A demo app implementing custom charts using ECharts, Satori and Next.js route handler
- Setup Next.js.
- Install packages
echartsandecharts-for-react. - Create a component for Echarts that accepts an chartOptions prop.
- Setup the Echart in the page route.
- Create a route handler at
api/chart/route.tsthat returns an SVG string of the chart. - Implement a Download component that performs a POST request using the
chartOptions. - Convert the SVG string to either an SVG URI or PNG URI based on the use case.
- Setup Satori:
- Install package
satoriandintl-segmenter-polyfill. - Extend the global
windowinterface with__resourcetypings.d.ts. - Add the font file using by satori to the
publicdirectory.
- Creaye a
reactToSVGfunction that converts a React component to an SVG string satori - Add a React Template to customize the chart ChartTemplate.
- Pass the template to Satori using
reactToSVGto generate an SVG string of the customized chart. - Convert the SVG string to a data URI and download it.
- Instead of returning an SVG string from route handler, you can return a data URI directly so that we can skip one function call.