DEV Community

Cover image for Embed Chart.js in Go
Go4WebDev
Go4WebDev

Posted on • Updated on

Embed Chart.js in Go

My goals with this "chart demo" was to

  1. Find a solution that can use local API on the server.
  2. Make it as fast as possible.
  3. Work more dynamically using JSON.

Normally you use Javascript fetch() in the browser and hence have to use a "public" API to get data from the database. Creating the charts on the server makes it possible to use "hidden" API requests. This makes it both safer and faster.

Chart.js is one of the smallest Javascript chart libraries available (as far as I know) - about 60KB. So in order to load faster I skipped the CDN part and downloaded it to my server. To further increase the speed I embedded Chart.js into the Go binary.

The ability to fetch JSON from within Go and populate the chart before rendering also makes it more dynamic and way safer than fetch() in the browser.

Here is my attempt to make charts faster using Go. I welcome any clue how to make it even faster.

Top comments (0)