ChartView.js 5.41 KB
'use strict';

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

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

    render() {


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

        console.log(xString);

        const HTML = getHTMLString(xString,this.props.yData.toString());
        console.log(this.props.yData);

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

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

function getHTMLString(xData,yData) {
    const HTML = `
        <!DOCTYPE html>
        <html style="height: 100%">
           <head>
               <meta charset="utf-8">
           </head>
           <body style="height: 100%; margin: 0">
               <div id="container" style="height: 100%"></div>
               <script type="text/javascript" src="http://cdn.bootcss.com/echarts/3.0.0/echarts.min.js"></script>
               <script type="text/javascript">
                   var dom = document.getElementById("container");
                   var myChart = echarts.init(dom);
                   var app = {};
                   option = null;
                   option = {
                       title: {
                           show: false,
                       },
                       xAxis:  {
                           type: 'category',
                           boundaryGap: true,
                           axisLine: {
                               show: true,
                               onZero: false,
                               lineStyle: {
                                   width: 1,
                                   color: 'gray',
                               },
                           },
                           axisTick: {
                               show:false,
                           },
                           axisLabel: {
                               textStyle: {
                                   color: '#b1b1b1',
                                   fontSize: 24,
                               },
                           },
                           splitLine: {
                               show: false,
                           },
                           data:[`+xData+`]
                       },
                       yAxis: {
                           type: 'value',
                           axisLine: {
                               show: false,
                               onZero: false,
                           },
                           axisTick: {
                               show:false,
                           },
                           axisLabel: {
                               show: false,
                           },
                       },
                       tooltip: {
                           show: true,
                           showContent: true,
                           trigger: 'axis',
                           alwaysShowContent: 'true',
                           position: function (point,params,dom) {
                               return [point[0]/7*7-40,0];
                           },
                           backgroundColor: 'orange',
                           formatter: '{c}',
                           textStyle: {
                               fontSize: 24,
                           },
                           axisPointer: {
                               type: 'line',
                               lineStyle: {
                                   opacity: 1,
                               },
                           },
                           extraCssText: 'box-shadow: 0 0 3px rgba(0, 0, 0, 0.3);',
                           padding:[10,20],
                       },
                       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;
}