VChart theme switching is a common operation: for example, according to different seasons, holidays, or internationalization, personalized color schemes, and the common night mode. Users can manually or listen to the user's system to switch different styles of themes to adapt to different usage environments.
Theme Update Examples
For example, registration and switching of night mode:
Source Code Location and Content Related to Themes
package/vchart/scr/core/vchart.ts: The theme updater for a single chart instance, implementing the specific theme application logic, transforming the global theme into actual style changes for the chart. The main update logic of the chart is here.
package/vchart/src/core/instance-manager.ts: The central hub for chart instance registration and management, providing the infrastructure for traversing and locating theme instance updates, ensuring that each chart can receive theme updates.
package/vchart/src/theme/theme-manager.ts: The global theme scheduling center, responsible for theme registration, retrieval, and global updates, providing a unified theme management entry and coordination mechanism.
Through the methods defined in the `VChart` class, the core rendering and update logic of the chart is implemented. `ThemeManager` and `InstanceManager` are responsible for the global management of themes and instances, respectively, forming a decoupled, flexible, and extensible chart library architecture; `VChart` provides a unified update entry, implementing most of the update operation logic; while `ThemeManager` and `InstanceManager` achieve global theme updates through instance registration and traversal mechanisms.
# In-depth Analysis of the Theme Update Process
The VChart official website divides theme updates into two dimensions, namely
Updating the theme of a single chart instance
Updating the theme of all charts globally through ThemeManager.
The specific approach can be viewed at 🎁 VisActor Data Visualization Competition. Both methods use the same setCurrentTheme call to switch themes. The former is called by an instance generated by the VChart object, updating a single chart; the latter is called through ThemeManager, updating the global chart theme. Therefore, my approach to reading the source code is based on the declaration and definition of the setCurrentTheme method, delving deeper layer by layer.
Example:
Updating a single instance:
const vchart = new VChart(spec, { dom: CONTAINER_ID });
//单个theme实例的更新vchart.setCurrentTheme('userTheme');
First, analyze the internal private method _setCurrentTheme, first trigger _updateCurrentTheme, enter the theme merging and parsing process explained in section 11-1, then reinitialize the chart specification (spec). chart is the core rendering instance of the chart, responsible for specific rendering and interaction logic. Here, the method setCurrentTheme is called, which will be analyzed in detail below.
Finally, the returned { change: true, reMake: false } indicates: change means the configuration has changed, triggering a re-render, informing the rendering engine that an update is needed. reMake means a complete rebuild of the chart is not necessary, only a partial update is required. This structure is returned to trigger the chart update behavior in the subsequent setCurrentThemeSync in updateCustomConfigAndRerender.
setCurrentThemeSync()
/**
* **同步方法** 设置当前主题。
* **注意,如果在 spec 上配置了 theme,则 spec 上的 theme 优先级更高。**
* @param name 主题名称
* @returns */setCurrentThemeSync(name: string) {
if (!ThemeManager.themeExist(name)) {
returnthisas unknown as IVChart;
}
const result = this._setCurrentTheme(name);
this._setFontFamilyTheme(this._currentTheme?.fontFamily asstring);
this.updateCustomConfigAndRerender(result, true, {
transformSpec: false,
actionSource: 'setCurrentTheme' });
returnthisas unknown as IVChart;
}
After checking for null, we first get the agreed object { change: true, reMake: false }, which means the theme is updated and must trigger a re-render, but there is no need to completely rebuild the table, just a partial update is sufficient.
When reMake is true, the chart will be completely reset through releaseData, initDataSet, and release, releasing all related resources to prepare for re-rendering. As mentioned earlier, theme updates do not completely reset the chart.
When reMake is false, re-compilation and chart resizing will be performed based on the values of reCompile and reSize, respectively. Operations are implemented through methods on instances such as _chart and _compiler.
After reading the source code, it is known that theme updates do not trigger reCompile operations. Generally, reCompile is needed only when there are additions or deletions in the graphics.
This is a synchronous rendering method, which prepares and checks before rendering through _beforeRender to ensure that the rendering conditions are met; it calls the render method of _compiler to perform the actual chart drawing, and can pass in transformation configurations; after completing the drawing, _afterRender performs post-rendering cleanup and state updates, and returns the current instance.
Principle of Global Update
Theme Scheduling Center theme-manager
As mentioned earlier, the VChart instance updates the theme for a single chart, while the themeManager updates the global theme
https://www.visactor.io/vchart/guide/tutorial_docs/Theme/Customize_Theme
After registering the theme in `ThemeManager`, you can use `ThemeManager.setCurrentTheme` to hot-update the registered theme by theme name. Note: This method will affect all chart instances on the page.
It is not difficult to see that this method globally sets the current theme name, then iterates over all registered chart instances and calls `setCurrentTheme` on each instance, thereby achieving a global update of the theme for all instances.
### Reason for Theme Instance Operations instance-manager
The operation of the instance on the chart is actually because, within the constructor of the VChart class, the current VChart instance is registered in `InstanceManager.instances`, thereby supporting global operations such as unified theme updates.
```xml
export class VChart implements IVChart {
constructor(spec: ISpec, options: IInitOption) {
//......其他
InstanceManager.registerInstance(this);
}
}
Conclusion
In summary, most of the update operations in vchart.ts are implemented in the VChart class, not only involving theme updates but also other situations that require updates. Theme updates are just a part of it; the theme-manager and instance-manager allow developers to manage global theme updates through the registration and traversal of instances, achieving both single instance updates and global updates of themes.