Home > Web Front-end > JS Tutorial > body text

Introduction to responsive React Native Echarts components

不言
Release: 2018-07-14 11:17:56
Original
2129 people have browsed it

This article mainly introduces the introduction of responsive React Native Echarts components. It has certain reference value. Now I share it with you. Friends in need can refer to it.

In recent years, with the mobile terminal The requirements for data visualization are getting higher and higher, and traditional chart libraries like MPAndroidChart can no longer meet the increasingly abnormal needs of product managers. The development of data visualization in the front-end field is relatively prosperous. Using a powerful front-end data visualization library like Echarts on the mobile terminal through WebView is a good way to solve the problem.

In React Native development, since the same JavaScript language is used as the front-end, the work of connecting Echarts is relatively smooth, but some necessary component encapsulation can still greatly improve development efficiency.

Echarts has officially recommended a third-party packaging library: react-native-echarts (note: its corresponding nmp package name is native-echarts). It currently has 400 stars and 100 weekly downloads. It can be seen that it is still used. widely used. However, after our investigation, we found that react-native-echarts has the following problems:

  • The library has not been updated for more than half a year, the Echarts version is stuck at 3.0, and Android-side packaging requires manually adding assets. The problem has not been solved yet

  • The library's interface flexibility is low, for example, the size can only be set through width and height; the Echarts expansion package cannot be used; event registration, WebView communication, etc. cannot be performed

Since using WebView to encapsulate Echarts involves local html, it is not a pure JavaScript language level function, and there is no native code, so making it an nmp package is not a good choice. It is written in the project Internal components, configuring it yourself is a more convenient and flexible solution.

So we decided not to use the third-party Echarts packaging library and write a universal component WebChart ourselves. In order to facilitate use in development, this component has the following characteristics:

  • is designed according to responsiveness. You only need to configure the data source in the option. The chart will automatically refresh after the data changes, and the chart will be updated. In line with React's style.

    Our solution is to determine whether the incoming option parameter has changed every time the component updates. If the change occurs, pass webView.postMessage and pass in the new option in the form of JSON to notify Echarts to setOption again.

    Although Echarts itself will compare options, pre-judgement can reduce frequent communication with WebView caused by updates. This is still obvious when there are a large number of asynchronous requests in the container parent component; inside WebView, update It uses the setOption of Echarts itself without reloading the entire WebView

  • Using the postMessage and onMessage interfaces of WebView, event communication between charts and other React Native components can be realized

  • Through the exScript parameter of the component, you can add any script to WebView, which is flexible to use

  • Because it is a self-written component, echarts version, expansion package, svg/canvas, data Incremental loading can be set by yourself

Demo and usage

For usage and examples, please see: react-native-echarts-demo, if you need to use it directly, You can transplant according to the following steps:

  1. Copy the WebChart component folder in the root directory to the appropriate place in your project

  2. Copy /android Copy the /app/src/main/assets/web folder to the same location as your project. There is no assets folder that needs to be created manually.

You only need the above two steps to use the WebChart component in your project.

If you need further customization, the Echarts code is in the

Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template