Compare commits

..

42 Commits

Author SHA1 Message Date
lvwang2002 17054bc5a5 更新 src/components/Echarts/index.js 2024-04-29 20:55:10 +00:00
lvwang2002 425d304879 更新 package.json 2024-04-29 19:14:38 +00:00
lvwang2002 d4214a14f2 更新 src/index.js 2024-03-28 17:29:16 +00:00
lvwang2002 b9176c2157 更新 package.json 2024-03-28 16:54:26 +00:00
lvwang2002 099fe797ad 更新 src/components/Echarts/index.js 2024-03-28 16:50:02 +00:00
guiqi.zh 0ae9a6386c release new version 2018-12-20 16:54:48 +08:00
Arron Zhu 6bbb09917e
Merge pull request #136 from hydraZty/webview
@bugfix, Webview does not show in the release version
2018-12-20 16:48:24 +08:00
hydraZhang 0e592c52e7 @bugfix, Webview does not show in the release version 2018-11-15 14:46:47 +08:00
Arron Zhu 3767deeae3
Merge pull request #86 from RebelBIrd/master
添加一个主动刷新option的方法
2018-02-22 15:13:40 +08:00
Arron Zhu 656a2d3838
Merge pull request #85 from wxsms/fix-android
Fix echarts displayed incorrect width in Android
2018-02-22 15:11:54 +08:00
wxsm 9d435a296c
Update tpl.html 2018-01-16 11:06:41 +08:00
wxsm ea6de46c49
Fix favicon request 404 error
This is causing chart not displaying in Android release bundle sometimes.
2018-01-09 16:50:11 +08:00
大黄 78133f76f5 添加一个主动刷新option的方法,界面不会闪动 2018-01-03 08:27:48 +08:00
wxsm 4e18c203b7 Fix echarts overflow on Android 2017-12-25 21:48:44 +08:00
wxsm 405e9f7a55 Fix echarts overflow on Android 2017-12-25 21:33:48 +08:00
somonus 9ee6a04c9d release 0.4.0 2017-12-08 16:46:36 +08:00
Arron Zhu 533b572df6
Merge pull request #64 from martinblanchette/transparent-as-default-background-color
Added a default transparent background color to the Echarts WebView.
2017-12-08 16:42:08 +08:00
Arron Zhu 7d18dbe76c
Merge pull request #73 from MessiQS/master
处理ios11 高度只有设定height的一半
2017-12-08 16:37:49 +08:00
mike 228a9bd675 ios11 高度只有设定height的一半
处理上述bug
2017-11-17 20:32:34 +08:00
Arron Zhu eeeaead411 Merge pull request #42 from superandrew213/add-onPress
Add onPress
2017-10-26 04:42:00 -05:00
Arron Zhu c9efc0634f Merge pull request #44 from superandrew213/add-width-to-echart-container
Add width
2017-10-26 04:38:47 -05:00
Arron Zhu 3865741478 Merge pull request #57 from will86/master
option中包含多个function的情况
2017-10-26 04:38:03 -05:00
Martin Blanchette 17872dcf22 Added a default transparent background color to the Echarts WebView. 2017-10-02 11:54:21 -04:00
Will bf8ae64d7e Merge pull request #1 from will86/will86-patch-1
Update toString.js
2017-09-13 09:39:17 +08:00
Will 778e6cef1b Update toString.js
option中包含多个function的情况
2017-09-11 18:07:15 +08:00
Andrew Shini 474e77fbc7 Add width 2017-05-15 16:48:47 +10:00
Andrew Shini b55bab0516 Remove double stringify 2017-05-13 11:24:48 +10:00
Andrew Shini 7067d0417f Add onPress 2017-05-13 11:15:33 +10:00
Andrew Shini 52461fdb6f Listen for click 2017-05-13 11:13:47 +10:00
Arron Zhu 9fc5b29aec Update README.md 2016-11-18 16:08:30 +08:00
Arron Zhu 8835b6cd3a Update README.md 2016-11-18 16:04:16 +08:00
somonus 2b56077490 update 2016-11-11 10:34:30 +08:00
Arron Zhu 9b7428c5ef Update tpl.html 2016-11-03 13:44:05 +08:00
somonus cd12da9679 update readme 2016-11-03 10:55:11 +08:00
somonus 73570100e3 fixed #4 2016-11-03 10:43:21 +08:00
somonus 171d8a3519 update 2016-11-02 15:42:30 +08:00
somonus 1aa33b9c13 fixed #2 2016-11-01 17:31:14 +08:00
somonus 40752bbacf update 2016-10-27 14:16:39 +08:00
somonus b3152edbf2 fix the bug when option contains function 2016-10-27 14:14:35 +08:00
Arron Zhu d7577f445d Update README.md 2016-10-24 13:41:55 +08:00
somonus 6ee1269df5 set scrollEnabled false 2016-10-21 18:37:32 +08:00
somonus c6c58d2442 update read 2016-10-21 16:43:02 +08:00
12 changed files with 301 additions and 95 deletions

View File

@ -1,20 +1,22 @@
# native-echarts
[![NPM Version](https://img.shields.io/npm/v/native-echarts.svg?style=flat)](https://www.npmjs.org/package/native-echarts)
[![npm](https://img.shields.io/npm/dm/native-echarts.svg?style=flat)](https://www.npmjs.org/package/native-echarts)
[![License](http://img.shields.io/npm/l/native-echarts.svg?style=flat)](https://raw.githubusercontent.com/somonus/react-native-echarts/master/LICENSE.md)
## install
$ npm install native-echarts --save
## Usage
###用法
The Usage is complete consistent with Echarts
用法完全和echarts一致将echarts的option传给组件。option详细属性详见[文档](http://echarts.baidu.com/option.html#title)
component props:
native-echarts共暴露了三个属性:
* *option* (object): echarts图表的配置项无默认值。
* *width* (number): 图表的宽度,默认值为其外层容器的宽度。
* *height* (number): 图表的高度默认值为400。
* *option* (object): The option for echarts: [Documentation](http://echarts.baidu.com/option.html#title)。
* *width* (number): The width of the chart. The default value is the outer container width.
* *height* (number): The height of the chart. The default value is 400.
```js
@ -31,7 +33,7 @@ export default class app extends Component {
render() {
const option = {
title: {
text: 'ECharts 入门示例'
text: 'ECharts demo'
},
tooltip: {},
legend: {
@ -68,7 +70,22 @@ cd example
npm install
npm start
```
打开ios目录下的xcode工程点击run
### IOS
Open the xcode project in the ios directory and click run
screenshots
![image](https://github.com/somonus/react-native-echarts/blob/master/example/demoIOS.png)
### Android
Open the Android project in the android directory with Android Studio and click run.
screenshots
![image](https://github.com/somonus/react-native-echarts/blob/master/example/demoAndroid.png)
## License

BIN
example/demoAndroid.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 80 KiB

BIN
example/demoIOS.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

View File

@ -9,13 +9,17 @@ import {
AppRegistry,
StyleSheet,
Text,
View
View,
TouchableOpacity
} from 'react-native';
import Echarts from 'native-echarts';
export default class app2 extends Component {
render() {
const option = {
constructor(props) {
super(props);
this.state = {
option : {
title: {
text: 'ECharts 入门示例'
},
@ -32,13 +36,44 @@ export default class app2 extends Component {
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
},
text: 'test'
};
}
changeOption() {
this.setState({
option: {
title: {
text: 'New Chart'
},
tooltip: {},
legend: {
data:['销量']
},
xAxis: {
data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
},
yAxis: {},
series: [{
name: '销量',
type: 'line',
data: [5, 20, 36, 10, 10, 20]
}]
}
})
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native Echarts!
</Text>
<Echarts option={option} height={300} />
<TouchableOpacity style={styles.button} onPress={this.changeOption.bind(this)}>
<Text style={{color: '#fff'}}>change state</Text>
</TouchableOpacity>
<Echarts option={this.state.option} height={300} />
</View>
);
}
@ -56,6 +91,12 @@ const styles = StyleSheet.create({
textAlign: 'center',
margin: 30,
},
button: {
backgroundColor: '#d9534f',
padding: 8,
borderRadius: 4,
marginBottom: 20
}
});
AppRegistry.registerComponent('app2', () => app2);

View File

@ -9,13 +9,17 @@ import {
AppRegistry,
StyleSheet,
Text,
View
View,
TouchableOpacity
} from 'react-native';
import Echarts from 'native-echarts';
export default class app2 extends Component {
render() {
const option = {
constructor(props) {
super(props);
this.state = {
option : {
title: {
text: 'ECharts 入门示例'
},
@ -32,13 +36,44 @@ export default class app2 extends Component {
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
},
text: 'test'
};
}
changeOption() {
this.setState({
option: {
title: {
text: 'New Chart'
},
tooltip: {},
legend: {
data:['销量']
},
xAxis: {
data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
},
yAxis: {},
series: [{
name: '销量',
type: 'line',
data: [5, 20, 36, 10, 10, 20]
}]
}
})
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native Echarts!
</Text>
<Echarts option={option} height={300} />
<TouchableOpacity style={styles.button} onPress={this.changeOption.bind(this)}>
<Text style={{color: '#fff'}}>change state</Text>
</TouchableOpacity>
<Echarts option={this.state.option} height={300} />
</View>
);
}
@ -56,6 +91,12 @@ const styles = StyleSheet.create({
textAlign: 'center',
margin: 30,
},
button: {
backgroundColor: '#d9534f',
padding: 8,
borderRadius: 4,
marginBottom: 20
}
});
AppRegistry.registerComponent('app2', () => app2);

View File

@ -9,7 +9,7 @@
"dependencies": {
"react": "15.3.2",
"react-native": "0.35.0",
"native-echarts": "0.1.0"
"native-echarts": "file:../"
},
"jest": {
"preset": "jest-react-native"

View File

@ -1,6 +1,6 @@
{
"name": "native-echarts",
"version": "0.1.0",
"version": "0.5.0",
"description": "echarts for react-native",
"main": "src/index.js",
"directories": {
@ -15,7 +15,9 @@
},
"keywords": [
"react-native",
"echarts"
"echarts",
"chart",
"charts"
],
"author": "somonus",
"license": "MIT",
@ -24,6 +26,7 @@
},
"homepage": "https://github.com/somonus/react-native-echarts#readme",
"dependencies": {
"echarts": "3.2.3"
"echarts": "3.2.3",
"react-native-webview":"13.8.7"
}
}

View File

@ -1,19 +1,48 @@
import React, { Component } from 'react';
import { WebView } from 'react-native';
import { View, StyleSheet, Platform } from 'react-native';
import WebView from 'react-native-webview';
import renderChart from './renderChart';
import echarts from './echarts.min';
export default class App extends Component {
constructor(props) {
super(props);
this.setNewOption = this.setNewOption.bind(this);
}
UNSAFE_componentWillReceiveProps(nextProps) {
if(nextProps.option !== this.props.option) {
this.refs.chart.reload();
}
}
setNewOption(option) {
this.refs.chart.postMessage(JSON.stringify(option));
}
render() {
const source = Platform.OS === 'ios'
? require('./tpl.html') // iOS使用require方式加载
: { uri: 'file:///android_asset/tpl.html' }; // Android从assets目录加载
return (
<View style={{flex: 1, height: this.props.height || 400,}}>
<WebView
ref="chart"
scrollEnabled = {false}
injectedJavaScript = {renderChart(this.props)}
style={{
height: this.props.height || 400,
backgroundColor: this.props.backgroundColor || 'transparent'
}}
source={{html: renderChart(this.props.option)}}
scalesPageToFit={Platform.OS !== 'ios'}
originWhitelist={['*']}
source={source}
onMessage={event => this.props.onPress ? this.props.onPress(JSON.parse(event.nativeEvent.data)) : null}
/>
</View>
);
}
}

View File

@ -1,38 +1,30 @@
import echarts from './echarts.min';
import toString from '../../util/toString';
export default function renderChart(option) {
export default function renderChart(props) {
const height = `${props.height || 400}px`;
const width = props.width ? `${props.width}px` : 'auto';
return `
<!DOCTYPE html>\n
<html>
<head>
<title>echarts</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<style type="text/css">
html,body {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
#main {
height: 100%;
}
</style>
<script>
${echarts}
</script>
</head>
<body>
<div id="main" ></div>
<script>
// 基于准备好的dom初始化echarts实例
document.getElementById('main').style.height = "${height}";
document.getElementById('main').style.width = "${width}";
var myChart = echarts.init(document.getElementById('main'));
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(${JSON.stringify(option)});
</script>
<body>
<html>
myChart.setOption(${toString(props.option)});
window.document.addEventListener('message', function(e) {
var option = JSON.parse(e.data);
myChart.setOption(option);
});
myChart.on('click', function(params) {
var seen = [];
var paramsString = JSON.stringify(params, function(key, val) {
if (val != null && typeof val == "object") {
if (seen.indexOf(val) >= 0) {
return;
}
seen.push(val);
}
return val;
});
window.postMessage(paramsString);
});
`
}

File diff suppressed because one or more lines are too long

View File

@ -1,12 +1,19 @@
import React, { Component } from 'react';
import { WebView, View } from 'react-native';
import { View } from 'react-native';
import WebView from 'react-native-webview';
import { Container, Echarts } from './components'
export default class App extends Component {
setNewOption(option) {
this.chart.setNewOption(option);
}
render() {
return (
<Container width={this.props.width}>
<Echarts {...this.props} />
<Echarts {...this.props} ref={e => this.chart = e}/>
</Container>
);
}

13
src/util/toString.js Normal file
View File

@ -0,0 +1,13 @@
export default function toString(obj) {
let result = JSON.stringify(obj, function(key, val) {
if (typeof val === 'function') {
return `~--demo--~${val}~--demo--~`;
}
return val;
});
do {
result = result.replace('\"~--demo--~', '').replace('~--demo--~\"', '').replace(/\\n/g, '').replace(/\\\"/g,"\"");//最后一个replace将release模式中莫名生成的\"转换成"
} while (result.indexOf('~--demo--~') >= 0);
return result;
}