VSeed, an elegant data composer, transforming complexity into simplicity.
!!!###!!!title=VChart On-demand Loading Source Code Analysis——VisActor/VChart Contributing Documents!!!###!!!!!!###!!!description=---title: 13.2 VChart On-Demand Loading Source Code Explanation
key words: VisActor,VChart,VTable,VStrory,VMind,VGrammar,VRender,Visualization,Chart,Data,Table,Graph,Gis,LLM---
Based on the previous articles, we have introduced the composition of VChart and the concept of Tree Shaking. Based on these two preliminary concepts, we will now elaborate on the specific implementation of VChart's on-demand loading.
!!!###!!!
Based on the previous articles, we have introduced the composition of VChart and the concept of Tree Shaking. Based on these two preliminary concepts, we will now elaborate on the specific implementation of VChart's on-demand loading.
Core Creation Process of VChart Charts
Factory
The on-demand loading of VChart mainly relies on the Factory class to achieve this. This class plays an important role, responsible for registering and creating various charts, series, components, graphics, Regions, layouts, and plugins. Below, we will conduct an in-depth analysis from the aspects of the registration mechanism, creation mechanism, and on-demand registration of charts.
Registration Mechanism
The Factory class provides a series of static methods for registering different types of modules. Through these methods, modules are registered into the static properties of Factory, thereby forming a registry. The specific code is as follows:
The Factory class also provides a series of static methods for creating module instances as needed. These methods look up the corresponding constructor from the registry based on the type and create an instance. The sample code is as follows:
Taking the line chart as an example, let's look at the specific implementation of on-demand registration. In packages/vchart/src/chart/line/line.ts, there is the following code:
By implementing the above code, when using VChart, if you call registerLineChart to register a line chart, it will automatically register the necessary elements such as the line series, line chart elements, and point chart elements that the line chart depends on. The specific usage is as follows:
import { VChart } from './core';
import { registerLineChart } from './chart/line';
VChart.useRegisters([
registerLineChart,
// 其他需要的模块
]);
In packages/vchart/src/core/vchart.ts, the chart instance is not created by direct path reference, but by using Factory.createChart. In this way, the core class vchart can create instances based on the charts registered by the user without referencing all chart implementations. The relevant code is as follows:
So method one is equivalent to the following reference, where the referenced VChart is the VChart class exported by vchart-all, and method two is equivalent to the VChart class exported from core/vchart.ts.
import { default as VChart } from '@visactor/vchart';
The main purpose of the file vchart-all.ts is to register and export all functional modules of VChart:
Since vchart - all is declared as a file with side effects, when VChart is referenced using method one, all files that vchart-all depends on will be packaged; whereas when VChart is referenced using method two, only core/vchart.ts will be packaged.
This document was revised and organized by the following personnel