It is not only a fully functional graph visualization library, but also an explorer of data relationships.
!!!###!!!title=Openinula VChart——VisActor/VChart tutorial documents!!!###!!!!!!###!!!description=- Repository Address: [https://github.com/VisActor/VChart/tree/main/packages/openinula-vchart](https://github.com/VisActor/VChart/tree/main/packages/openinula-vchart)To make it easier for Openinula users to use VChart, we provide a Openinula wrapper package for VChart: `@visactor/openinula-vchart`. This component mainly encapsulates VChart's chart components as Openinula components, and its configuration items are consistent with VChart.In this tutorial, we will explain in detail how to use VChart in a Openinula project and create a simple bar chart. For more detailed API documentation, please refer to the [`@visactor/openinula-vchart`](https://github.com/VisActor/VChart/blob/main/packages/openinula-vchart/docs/2.1%20API%E8%AE%BE%E8%AE%A1.md) repository documentation.!!!###!!!
To make it easier for Openinula users to use VChart, we provide a Openinula wrapper package for VChart: @visactor/openinula-vchart. This component mainly encapsulates VChart's chart components as Openinula components, and its configuration items are consistent with VChart.
In this tutorial, we will explain in detail how to use VChart in a Openinula project and create a simple bar chart. For more detailed API documentation, please refer to the @visactor/openinula-vchart repository documentation.
Quick Start
How to Install
To start using Openinula VChart, you'll first need to install the @visactor/openinula-vchart package in your Openinula project. In the project root directory, use the following command to install the package:
npm install @visactor/openinula-vchart
or use yarn to install:
yarn add @visactor/openinula-vchart
Creating a Chart
Let's take creating a simple bar chart as an example. In your Openinula component, import the <BarChart> component from visactor/openinula-vchart and use them in the component. Here's an example code to create a bar chart:
In this example, we created a simple bar chart with some basic components and configurations.
Component Selection
To accommodate different user scenarios and usage habits, VChart offers two label styles:
<VChart />: A large and unified entry label that encapsulates the chart specifications, provides updates and unloads for the specifications. Suitable for toB pages, product pages with page-building elements, and business users who encapsulate VChart themselves.
<BarChart />, <LineChart />, <CommonChart />, and other syntax labels: Suitable for simple pages, written by developers. Convenient for implementing on-demand loading of packages.
The above bar chart example uses the syntax label <BarChart />, and you can choose the appropriate label style according to your needs. Next, we'll introduce how to adjust the chart according to your requirements.
Chart Configuration and Optimization
After creating a simple bar chart, you may need to make some adjustments based on the requirements of the project. VChart provides a wealth of configuration options to meet different scenarios.
Chart style: You can set the style of the bar chart by configuring the style in the bar attribute. For example, changing the color, stroke, width, etc., of the bar.
Custom legend: By setting the visible, position, orient, and other attributes of the <Legend> component, you can customize the display and position of the legend.
Adjust the axis: In the <Axis> component, you can set properties such as orient, type to adjust the display of the axis, or modify the label attribute to adjust the display of labels on the axis.
Here is a modified bar chart according to the requirements:
By adjusting the configuration, we have created a bar chart that is more suitable for use in actual projects.
Unified Chart Component
<VChart /> receives a complete spec as the chart definition. The data structure of its spec is exactly the same as the definition in VChart, so developers can input any valid VChart spec into Openinula-VChart for chart rendering.
Props
If you already have chart spec information, using the unified chart component is a quick way. Simply import the VChart component:
The VChart component is a encapsulated Openinula component, and its props are defined as follows:
The definition of EventsProps refers to the event interaction section.
onReady is a built-in callback event that is triggered when the chart is rendered or updated. Its parameters represent the chart instance object and whether it is the initial rendering.
For example, developers can register the required callback events on the chart instance during the initial rendering to implement chart interaction functionality.
Syntactic Tags
Syntactic tags refer to Openinula-VChart encapsulating the chart container and various components as Openinula components exported to developers. Developers can define charts in a more semantic and native Openinula-like way. It should be noted that the definition content of syntactic tags can be mutually converted with chart spec in most scenarios. The main content of this article also explains the relationship between syntactic tags and spec.
Tag Classification
Currently, Openinula-VChart exports three types of component tags: chart tags, component tags, and series tags.
Chart Tags
Tags for chart types, including the following:
In general, using these tags directly determines the type of chart. Among them, CommonChart is a special type of chart tag, which can be used to implement combined charts, dual-axis charts, and other types of charts. For more information on combined charts, please refer to: Combination Chart
The Props definition of these chart tags is as follows:
Refer to the event interaction section for the definition of EventsProps
onReady is a built-in callback event that is triggered when the chart is rendered or updated. Its parameters represent the chart instance object and whether it is the initial rendering.
Component Tags
Component tags refer to the visual components inside the VChart, including the following:
These components do not actually exist in the DOM structure. This notation is just to clearly show the structure of the chart. The configuration of these components completely aligns with the definition of the corresponding components in VChart. The difference is that the original data structure definition can now be used as props parameters for configuration.
Series Components
Series components refer to the components defined for the element series corresponding to the chart type. If the type of the chart component is specified, the series components corresponding to it should be used for defining the elements. The series components include the following:
Usage of Syntactic Tags
First, it is important to clarify the basic principles of syntactic tag prop definitions: the props of syntactic tags are essentially equivalent to the API definitions of corresponding components in the spec. In addition, more callback entry points have been added to syntactic tags to facilitate event mounting.
For example, consider the following spec definition for a line chart:
The corresponding syntactic tag definition is as follows:
Components not covered by syntactic tags
If there are components not covered by syntactic tags when using Openinula-VChart, you can use the unified chart component as a fallback solution.
On-demand loading
Openinula-VChart itself supports on-demand loading. When VChart needs to be loaded on demand, it is recommended to use the <VChartSimple /> tag,
The <VChartSimple /> component and the <VChart /> component are used in almost the same way. The only difference is that users need to import the VChart constructor class from @viasctor/vchart/esm/core and pass it to <VChartSimple />;
Reference for on-demand import of VChart related documents
Event Interaction
Basic Events
Both the unified chart tag (VChart) and the syntactic chart tags (BarChart, etc.) support the scene tree events thrown by the underlying rendering layer on their Props.
The definition of EventsProps is as follows:
Component Tag Events
In addition to the scene tree events, the chart components also support custom events. Custom events can be listened to on semantic component tags, as well as on the outermost Chart component.
<Legend /> Custom Events
<Brush /> Custom Events
<DataZoom /> Custom Events
<Player /> Custom Events
<ScrollBar /> Custom Events
Series Component Events
Series components (such as Bar, Line, etc.) also inherit the EventsProps events, for details please refer to the previous section.
Other Events
In addition to the above events, the following events can also be listened to in the Chart
Custom Dimension Events
Hierarchy Chart Events
Lifecycle-related events
Event Usage Example
Theme Styles
If you want to use custom themes in VChart, there are two ways to achieve this: defining themes in the spec and registering themes through ThemeManager. Since you don't need to import the VChart npm package in Openinula-VChart, the VChart base class is exposed as VChartCore in Openinula-VChart, making it convenient for developers to register custom themes using static methods on the VChart base class.
Please refer to VChart Theme for VChart theme configuration.
Note that for on-demand use of VChart, it is recommended to directly call the VChart API to use themes.
Example
typical scenarios
customized legend component
When the built-in legend component of VChart cannot meet the business requirements, it is common in Openinula projects to use custom legend components implemented with Openinula components. In such cases, the updateState API can be used to establish event associations between Openinula components and charts. You can refer to the specific implementation example here.
The core code is as follows:
The key point is to obtain the VChart instance through the ref and use the updateState to update the filter corresponding to the custom state.
Conclusion
Through this tutorial, you should have learned how to use VChart to create a simple bar chart in a Openinula project. At the same time, you have learned how to configure the chart according to requirements to meet different scenarios in the project. VChart provides a wealth of options and components, which will undoubtedly play a more significant role in your actual projects. We hope you enjoy using the VChart chart library in your projects!