A demo app implementing custom charts using ECharts, Satori and Next.js route handler
- Setup Next.js.
- Install packages
echarts
andecharts-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.ts
that 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
satori
andintl-segmenter-polyfill
. - Extend the global
window
interface with__resource
typings.d.ts. - Add the font file using by satori to the
public
directory.
- Creaye a
reactToSVG
function 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
reactToSVG
to 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.