React VChart
- Repository Address: https://github.com/VisActor/VChart/tree/main/packages/react-vchart
To make it easier for React users to use VChart, we provide a React wrapper package for VChart: @visactor/react-vchart
. This component mainly encapsulates VChart's chart components as React components, and its configuration items are consistent with VChart.
In this tutorial, we will explain in detail how to use VChart in a React project and create a simple bar chart. For more detailed API documentation, please refer to the @visactor/react-chart
repository documentation.
Quick Start
How to Install
To start using React VChart, you'll first need to install the @visactor/react-vchart
package in your React project. In the project root directory, use the following command to install the package:
npm install @visactor/react-vchart
or use yarn to install:
yarn add @visactor/react-vchart
Creating a Chart
Let's take creating a simple bar chart as an example. In your React component, import the <BarChart>
component from visactor/react-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 thebar
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 asorient
,type
to adjust the display of the axis, or modify thelabel
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 React-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 React 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 React-VChart encapsulating the chart container and various components as React components exported to developers. Developers can define charts in a more semantic and native React-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, React-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 React-VChart, you can use the unified chart component as a fallback solution.
On-demand loading
React-VChart inherently supports on-demand loading. There are two ways to achieve on-demand loading with VChart:
- Use the
<VChartSimple />
tag to implement custom on-demand loading.
The <VChartSimple />
component and the <VChart />
component are almost identical in usage. The only difference is that users need to import the VChart
constructor class from @viasctor/vchart
and pass it to <VChartSimple />
.
- Use semantic tags, from version 1.11.0, all semantic tags support on-demand loading by default, with the default registered components for each type of chart as follows:
Chart | Category | Additional Registered Components |
---|---|---|
<LineChart/> | Cartesian Charts | registerLabel |
<AreaChart/> | Cartesian Charts | registerLabel , registerTotalLabel |
<BarChart/> | Cartesian Charts | registerLabel , registerTotalLabel |
<Bar3dChart/> | Cartesian Charts | registerLabel , registerTotalLabel |
<BoxPlotChart/> | Cartesian Charts | registerLabel , |
<HeatmapChart/> | Cartesian Charts | registerLabel |
<Histogram3dChart/> | Cartesian Charts | registerLabel |
<HistogramChart/> | Cartesian Charts | registerLabel |
<LinearProgressChart/> | Cartesian Charts | registerLabel |
<RangeColumnChart/> | Cartesian Charts | registerLabel |
<RangeColumn3dChart/> | Cartesian Charts | registerLabel |
<ScatterChart/> | Cartesian Charts | registerLabel |
<SequenceChart/> | Cartesian Charts | registerLabel |
<WaterfallChart/> | Cartesian Charts | registerLabel , registerTotalLabel |
<RadarChart/> | Polar Charts | registerLabel |
<RoseChart/> | Polar Charts | registerLabel |
<CircularProgressChart/> | Polar Charts | registerLabel , registerIndicator |
<Pie3dChart/> | General Charts | registerLabel , registerIndicator |
<PieChart/> | General Charts | registerLabel , registerIndicator |
<CirclePackingChart/> | General Charts | None |
<FunnelChart/> | General Charts | registerLabel |
<Funnel3dChart/> | General Charts | registerLabel |
<GaugeChart/> | General Charts | None |
<MapChart/> | General Charts | registerLabel |
<SankeyChart/> | General Charts | None |
<SunburstChart/> | General Charts | None |
<TreemapChart/> | General Charts | None |
<VennChart/> | General Charts | None |
<WordCloud3dChart/> | General Charts | None |
<WordCloudChart/> | General Charts | None |
<LiquidChart/> | General Charts | registerIndicator |
For Cartesian charts, the default registered components are as follows:
registerCartesianLinearAxis
registerCartesianBandAxis
registerCartesianTimeAxis
registerCartesianLogAxis
registerCartesianCrossHair
registerBrush
registerContinuousLegend
registerDataZoom
registerDiscreteLegend
registerCustomMark
registerAllMarks
registerMarkArea
registerMarkLine
registerMarkPoint
registerScrollBar
registerTitle
registerTooltip
registerDomTooltipHandler
For Polar charts, the default registered components are as follows:
registerPolarLinearAxis
registerPolarBandAxis
registerPolarCrossHair
registerBrush
registerContinuousLegend
registerDataZoom
registerDiscreteLegend
registerCustomMark
registerAllMarks
registerScrollBar
registerTitle
registerTooltip
registerDomTooltipHandler
For General charts, the default registered components are as follows:
registerDiscreteLegend
registerContinuousLegend
registerCustomMark
registerAllMarks
registerTitle
registerTooltip
registerDomTooltipHandler
When using semantic tags, if you need components that are not loaded by default, you only need to register the missing components.
For reference on on-demand loading of VChart, see related documentation.
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:
All basic events support event filters. For example, to add an event filter to onClick
, you can pass the props
as follows:
The filters for other events are configured similarly. For more information about event filters, please refer to the event documentation.
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 - Chart Events & Component Events
Event Usage Example - Chart Event Filtering
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 React-VChart, the VChart base class is exposed as VChartCore
in React-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 React projects to use custom legend components implemented with React components. In such cases, the updateState API can be used to establish event associations between React 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.
API
Parameter | Description | Type | Default | Version |
---|---|---|---|---|
type | Chart type, only applicable to <VChart /> and <VChartSimple /> | string | None | |
spec | Chart spec configuration, same as vchart options | ISpec | None | |
data | Chart data | IData | IHierarchyData | None | |
width | Canvas width | number | None | |
height | Canvas height | number | None | |
options | Chart options, refer to vchart API documentation | Omit<IInitOption, 'dom'> | None | |
skipFunctionDiff | Skip all function checks when props update, i.e., all functions are considered unchanged | boolean | false | 1.6.5 |
morphConfig | Morph configuration | IMorphConfig | None | 1.12.7 |
onReady | Chart rendering completion event | (instance: VChart, isInitial: boolean) => void | None | |
onError | Error callback when the chart encounters an error | (err: Error) => void | None | |
onLegendItemHover | Legend item hover event | (e: any) => void | None | |
onLegendItemUnHover | Legend item unhover event | (e: any) => void | None | |
onLegendItemClick | Legend item click event | (e: any) => void | None | |
onLegendFilter | Legend item filter event | (e: any) => void | None | |
onLegendSelectedDataChange | Legend selected data change event | (e: any) => void | None | |
onBrushStart | Brush start event | (e: any) => void | None | |
onBrushChange | Brush update event | (e: any) => void | None | |
onBrushEnd | Brush end event | (e: any) => void | None | |
onDataZoomChange | DataZoom update event | (e: any) => void | None | |
onPlayerPlay | Player play event | (e: any) => void | None | |
onPlayerPause | Player pause event | (e: any) => void | None | |
onPlayerEnd | Player end event | (e: any) => void | None | |
onPlayerChange | Player change event | (e: any) => void | None | |
onPlayerForward | Player forward event | (e: any) => void | None | |
onPlayerBackward | Player backward event | (e: any) => void | None | |
onScrollBarChange | ScrollBar update event | (e: any) => void | None | |
onDimensionHover | Dimension hover event | (e: any) => void | None | |
onDimensionClick | Dimension click event | (e: any) => void | None | |
onDrill | Hierarchical chart event | (e: any) => void | None | 1.12.7 |
onInitialized | Initialization completion event | (e: any) => void | None | |
onRendered | Rendering completion event | (e: any) => void | None | |
onRenderFinished | Render finished event | (e: any) => void | None | |
onAnimationFinished | Animation finished event | (e: any) => void | None | |
onLayoutStart | Layout start event | (e: any) => void | None | |
onLayoutEnd | Layout end event | (e: any) => void | None | |
onPointerDown | PointerDown event | (e: any) => boolean | void; | None | |
onPointerUp | PointerUp event | (e: any) => boolean | void; | None | |
onPointerUpOutside | PointerUpOutside event | (e: any) => boolean | void; | None | |
onPointerTap | PointerTap event | (e: any) => boolean | void; | None | |
onPointerOver | PointerOver event | (e: any) => boolean | void; | None | |
onPointerMove | PointerMove event | (e: any) => boolean | void; | None | |
onPointerEnter | PointerEnter event | (e: any) => boolean | void; | None | |
onPointerLeave | PointerLeave event | (e: any) => boolean | void; | None | |
onPointerOut | PointerOut event | (e: any) => boolean | void; | None | |
onMouseDown | MouseDown event | (e: any) => boolean | void; | None | |
onMouseUp | MouseUp event | (e: any) => boolean | void; | None | |
onMouseUpOutside | MouseUpOutside event | (e: any) => boolean | void; | None | |
onMouseMove | MouseMove event | (e: any) => boolean | void; | None | |
onMouseOver | MouseOver event | (e: any) => boolean | void; | None | |
onMouseOut | MouseOut event | (e: any) => boolean | void; | None | |
onMouseEnter | MouseEnter event | (e: any) => boolean | void; | None | |
onMouseLeave | MouseLeave event | (e: any) => boolean | void; | None | |
onPinch | Pinch event | (e: any) => boolean | void; | None | |
onPinchStart | PinchStart event | (e: any) => boolean | void; | None | |
onPinchEnd | PinchEnd event | (e: any) => boolean | void; | None | |
onPan | Pan event | (e: any) => boolean | void; | None | |
onPanStart | PanStart event | (e: any) => boolean | void; | None | |
onPanEnd | PanEnd event | (e: any) => boolean | void; | None | |
onDrag | Drag event | (e: any) => boolean | void; | None | |
onDragStart | DragStart event | (e: any) => boolean | void; | None | |
onDragEnter | DragEnter event | (e: any) => boolean | void; | None | |
onDragLeave | DragLeave event | (e: any) => boolean | void; | None | |
onDragOver | DragOver event | (e: any) => boolean | void; | None | |
onDragEnd | DragEnd event | (e: any) => boolean | void; | None | |
onRightDown | RightDown event | (e: any) => boolean | void; | None | |
onRightUp | RightUp event | (e: any) => boolean | void; | None | |
onRightUpOutside | RightUpOutside event | (e: any) => boolean | void; | None | |
onTouchStart | TouchStart event | (e: any) => boolean | void; | None | |
onTouchEnd | TouchEnd event | (e: any) => boolean | void; | None | |
onTouchEndOutside | TouchEndOutside event | (e: any) => boolean | void; | None | |
onTouchMove | TouchMove event | (e: any) => boolean | void; | None | |
onTouchCancel | TouchCancel event | (e: any) => boolean | void; | None | |
onPress | Press event | (e: any) => boolean | void; | None | |
onPressUp | PressUp event | (e: any) => boolean | void; | None | |
onPressEnd | PressEnd event | (e: any) => boolean | void; | None | |
onSwipe | Swipe event | (e: any) => boolean | void; | None | |
onDrop | Drop event | (e: any) => boolean | void; | None | |
onWeel | Weel event | (e: any) => boolean | void; | None | |
onClick | Click event | (e: any) => boolean | void; | None | |
onDblClick | DblClick event | (e: any) => boolean | void; | None | |
onPointerDownFilter | PointerDown event filter, refer to Event API documentation | EventFilter | None | |
onPointerUpFilter | PointerUp event filter, refer to Event API documentation | EventFilter | None | |
onPointerUpOutsideFilter | PointerUpOutside event filter, refer to Event API documentation | EventFilter | None | |
onPointerTapFilter | PointerTap event filter, refer to Event API documentation | EventFilter | None | |
onPointerOverFilter | PointerOver event filter, refer to Event API documentation | EventFilter | None | |
onPointerMoveFilter | PointerMove event filter, refer to Event API documentation | EventFilter | None | |
onPointerEnterFilter | PointerEnter event filter, refer to Event API documentation | EventFilter | None | |
onPointerLeaveFilter | PointerLeave event filter, refer to Event API documentation | EventFilter | None | |
onPointerOutFilter | PointerOut event filter, refer to Event API documentation | EventFilter | None | |
onMouseDownFilter | MouseDown event filter, refer to Event API documentation | EventFilter | None | |
onMouseUpFilter | MouseUp event filter, refer to Event API documentation | EventFilter | None | |
onMouseUpOutsideFilter | MouseUpOutside event filter, refer to Event API documentation | EventFilter | None | |
onMouseMoveFilter | MouseMove event filter, refer to Event API documentation | EventFilter | None | |
onMouseOverFilter | MouseOver event filter, refer to Event API documentation | EventFilter | None | |
onMouseOutFilter | MouseOut event filter, refer to Event API documentation | EventFilter | None | |
onMouseEnterFilter | MouseEnter event filter, refer to Event API documentation | EventFilter | None | |
onMouseLeaveFilter | MouseLeave event filter, refer to Event API documentation | EventFilter | None | |
onPinchFilter | Pinch event filter, refer to Event API documentation | EventFilter | None | |
onPinchStartFilter | PinchStart event filter, refer to Event API documentation | EventFilter | None | |
onPinchEndFilter | PinchEnd event filter, refer to Event API documentation | EventFilter | None | |
onPanFilter | Pan event filter, refer to Event API documentation | EventFilter | None | |
onPanStartFilter | PanStart event filter, refer to Event API documentation | EventFilter | None | |
onPanEndFilter | PanEnd event filter, refer to Event API documentation | EventFilter | None | |
onDragFilter | Drag event filter, refer to Event API documentation | EventFilter | None | |
onDragStartFilter | DragStart event filter, refer to Event API documentation | EventFilter | None | |
onDragEnterFilter | DragEnter event filter, refer to Event API documentation | EventFilter | None | |
onDragLeaveFilter | DragLeave event filter, refer to Event API documentation | EventFilter | None | |
onDragOverFilter | DragOver event filter, refer to Event API documentation | EventFilter | None | |
onDragEndFilter | DragEnd event filter, refer to Event API documentation | EventFilter | None | |
onRightDownFilter | RightDown event filter, refer to Event API documentation | EventFilter | None | |
onRightUpFilter | RightUp event filter, refer to Event API documentation | EventFilter | None | |
onRightUpOutsideFilter | RightUpOutside event filter, refer to Event API documentation | EventFilter | None | |
onTouchStartFilter | TouchStart event filter, refer to Event API documentation | EventFilter | None | |
onTouchEndFilter | TouchEnd event filter, refer to Event API documentation | EventFilter | None | |
onTouchEndOutsideFilter | TouchEndOutside event filter, refer to Event API documentation | EventFilter | None | |
onTouchMoveFilter | TouchMove event filter, refer to Event API documentation | EventFilter | None | |
onTouchCancelFilter | TouchCancel event filter, refer to Event API documentation | EventFilter | None | |
onPressFilter | Press event filter, refer to Event API documentation | EventFilter | None | |
onPressUpFilter | PressUp event filter, refer to Event API documentation | EventFilter | None | |
onPressEndFilter | PressEnd event filter, refer to Event API documentation | EventFilter | None | |
onSwipeFilter | Swipe event filter, refer to Event API documentation | EventFilter | None | |
onDropFilter | Drop event filter, refer to Event API documentation | EventFilter | None | |
onWeelFilter | Weel event filter, refer to Event API documentation | EventFilter | None | |
onClickFilter | Click event filter, refer to Event API documentation | EventFilter | None | |
onDblClickFilter | DblClick event filter, refer to Event API documentation | EventFilter | None |
Conclusion
Through this tutorial, you should have learned how to use VChart to create a simple bar chart in a React 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!