vchart-svg-plugin is a plugin that converts vchart rendering content into an SVG string, mainly used for printing, server-side rendering, and other scenarios;
Basic Principles
Before reading the specific code, we first need to know some implementation principles of vchart. Vchart is rendered based on canvas and relies on the canvas rendering engine library @visactor/vrender at the bottom layer. Vrender generates a graphic scene tree stage based on the chart configuration of vchart:
The relationship of graphics-related classes in vrender is as follows:
In other words, through the vchart instance, we can obtain the root node stage of the scene tree. By recursively traversing the child nodes of stage, we can get all the rendered graphics. In the implementation of vrender, all graphics have an attribute property to maintain the display configuration of the graphics. Therefore, the task is broken down into two parts:
Construct the child nodes of the svg based on the child nodes of stage
Convert the attribute of vrender graphic elements into the corresponding attributes of svg nodes