wcharczuk / go-chart

go chart is a basic charting library in go.
MIT License
3.99k stars 326 forks source link

In-memory raster renderer #198

Closed ebudan closed 1 year ago

ebudan commented 2 years ago

Noticed while testing go-chart in a UI with fairly frequent re-render requirements: There is no in-memory renderer that would let the user simply acquire an image.Image for display purposes. Using the PNG in raster_renderer.go will incur overhead as it encodes the compressed image.

The simple change request here adds a new utility method (InMemory()), which internally extends the existing rasterRenderer to send the generated image.Image through a callback, rather than serializing to a compressed image format.

The setup of widgets' Render(RenderProvider, io.Writer) is a bit unwieldy here. I would further suggest splitting the .Save() from the .Render(), either explicitly (requiring the graph creator to call Save()) or perhaps by a similar callback for PNG as I use here with InMemory(). I have not looked too deeply into this yet.