ChartView.js 6.29 KB
'use strict';

import React, {Component} from 'react';
import {
    WebView,
    StyleSheet,
    View,
    Text,
    Platform,
    NativeModules,
    Dimensions,
} from 'react-native';

export default class ChartView extends Component {
    constructor(props) {
        super(props)
    }

    render() {

        let xString = '';
        if (this.props.xData.length!==0) {
            xString = this.props.xData.reduce((previous, current, index, array) => {
                if (index==1) {
                    previous = `\'` + previous + `\'`;
                }
                return previous  + ','+ `\'` + current+ `\'`;
            });
        }

        let jsPath = NativeModules.RNNativeConfig.echartsPath;
        if (jsPath === undefined || jsPath === '') {
            jsPath = 'http://cdn.bootcss.com/echarts/3.0.0/echarts.min.js';
        }
        const HTML = getHTMLString(xString, this.props.yData.toString(), jsPath);

        return(
            <View style={styles.container}>
                <Text style={styles.titleStyle}>{this.props.chartTitle}</Text>
                <WebView
                    style={styles.chartStyle}
                    bounces={false}
                    automaticallyAdjustContentInsets={true}
                    source={{html:HTML, baseUrl:'/'}}
                    scalesPageToFit={true}
                    startInLoadingState={false}
                    javaScriptEnabled={true}
                    scrollEnabled={false}
                />
            </View>
        );
    }
}

const fontSize = (Platform.OS === 'android') ? 14 : 14;
const width = Dimensions.get('window').width;
const initRatio = 465 / 250;
const height = width / initRatio;

const chartHeight = height + 75;

const styles = StyleSheet.create({
    container: {
        backgroundColor: 'white',
    },
    titleStyle: {
        fontSize: 14,
        color: '#b1b1b1',
        marginTop: 15,
        marginLeft: 15,
    },
    chartStyle: {
        height: chartHeight,
        // marginLeft: -45,
        // marginRight: -45,
        marginBottom: 15,
    },
});



function getHTMLString(xData, yData, jsPath) {
    const HTML = `
        <!DOCTYPE html>
        <html style="width: ` + width + `; height: ` + height + `; margin: 0px;">
           <head>
               <meta charset="utf-8">
               <meta name="viewport" content="width=` + width + `,initial-scale=1.0,user-scalable=no"/>
           </head>
           <body style="width: ` + width + `; height: ` + height + `; margin: 0px;">
               <div id="container" style="width: ` + width + `; height: ` + height + `; margin: 0px; min-height: 250px;"></div>
               <script type="text/javascript" src="`+ jsPath +`"></script>
               <script type="text/javascript">
                   var dom = document.getElementById("container");
                   var myChart = echarts.init(dom);
                   var app = {};
                   option = null;
                   option = {
                       title: {
                           show: false,
                       },
                       grid: {
                           left: 0,
                           top: 60,
                           right: 0,
                           width: ` + width + `,
                           height: '65%',
                       },
                       xAxis:  {
                           type: 'category',
                           boundaryGap: true,
                           axisLine: {
                               show: true,
                               onZero: false,
                           },
                           axisTick: {
                               show:false,
                           },
                           axisLabel: {
                               textStyle: {
                                   color: '#b1b1b1',
                                   fontSize: ` + fontSize + `,
                               },
                           },
                           splitLine: {
                               show: false,
                           },
                           data:[` + xData + `]
                       },
                       yAxis: {
                           type: 'value',
                           axisLine: {
                               show: false,
                               onZero: false,
                           },
                           axisTick: {
                               show:false,
                           },
                           axisLabel: {
                               show: false,
                           },
                           splitLine: {
                               show: true,
                           },
                       },
                       tooltip: {
                           show: true,
                           showContent: true,
                           trigger: 'axis',
                           alwaysShowContent: 'true',
                           position: function (point,params,dom) {
                               return [point[0] / 7 * 7 - 20, 20];
                           },
                           backgroundColor: 'orange',
                           formatter: '{c}',
                           textStyle: {
                               fontSize:  ` + fontSize + `,
                           },
                           axisPointer: {
                               type: 'line',
                               lineStyle: {
                                   opacity: 1,
                               },
                           },
                           padding:[5, 10],
                       },
                       series: [
                           {
                               type:'line',
                               lineStyle: {
                                   normal: {
                                       color: 'orange',
                                   },
                               },
                               smooth: true,
                               data:[` + yData + `],
                           },
                       ]
                   };
                   if (option && typeof option === "object") {
                       myChart.setOption(option, true);
                   }
               </script>
           </body>
        </html>
    `;
    return HTML;
}