//心电图的历史数据
var hisArray = [];
var hisFlag = false;
var dataList = null;
layui.use(['layer', 'form', 'laydate', 'admin', 'ax'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var laydate = layui.laydate;
    var admin = layui.admin;
    var layer = layui.layer;

    // 让当前iframe弹层高度适应
    //admin.iframeAuto();

    //军人信息
    var ajax = new $ax(Feng.ctxPath + "/mmhc/madetail/" + Feng.getUrlParam("id"));
    var result = ajax.start();
    result.birthday = result.birthday.substring(0, 10);
    console.log(result.tsTime);
    $("#tsTime").val(result.tsTime);
    $("#xyTime").val(result.xyTime);
    $("#hzTime").val(result.hzTime);
    $("#xtTime").val(result.xtTime);


    if (result.xyTime != null && result.xyTime !== undefined){
        gradeAdd();
    }
    if (result.xtTime != null && result.xtTime !== undefined){
        xtAdd();
    }

    form.val('militaryForm1', result);
    if (1 == result.isSurgery) {
        $('#is_surgery_code1').attr('checked', true);
        $("#is_surgery_code2").removeAttr('checked');
    } else if (2 == result.isSurgery) {
        $("#is_surgery_code1").removeAttr('checked');
        $('#is_surgery_code2').attr('checked', true);
    }

    //静态指标
    var ajaxIndex = new $ax(Feng.ctxPath + "/gunsApi/getStaticData/" + Feng.getUrlParam("id"));
    var resultIndex = ajaxIndex.start();
    form.val('inForm', resultIndex.data);

    //指标占比
    $('#ratioGrade').click(function () {
        admin.open({
            type: 2,
            title: '指标占比',
            shade:0,
            //offset: 't',
            area: ['60%', '60%'],
            content: Feng.ctxPath + '/grade/ratio?id=' + Feng.getUrlParam("id")

        });
    });

    //查询血糖数据
    var ajaxXt = new $ax(Feng.ctxPath + "/ypStat/detail/" + Feng.getUrlParam("id"));
    var resultXt = ajaxXt.start();
    form.val('yp_form', resultXt.data);
    form.render();

    // 渲染时间选择框
    laydate.render({
        elem: '#birthday',
        trigger: 'click', //添加这一行来处理
        done: function (value, date, endDate) {
            $self.form.birthday = value;
        }
    });

    var websocket = null;
    //判断当前浏览器是否支持WebSocket
    if ('WebSocket' in window) {
        websocket = new WebSocket("ws://180.76.164.148:9002?userId=" + result.id);
        //websocket = new WebSocket("ws://127.0.0.1:9002?userId=" + result.id);
        //websocket = new WebSocket("wss://zcpg.xazhcx.com/getStaticDatawss/?userId="+result.id);
        //websocket默认是传输字符串的，需要改为arraybuffer二进制传输类型
        //webOrTcpsocket.binaryType = "arraybuffer";
    } else {
        alert('当前浏览器 Not support websocket')
    }

    //连接发生错误的回调方法
    websocket.onerror = function () {
        // setMessageInnerHTML("WebSocket连接发生错误");
    };

    //连接成功建立的回调方法
    websocket.onopen = function () {
        setMessageInnerHTML("连接成功");
    };

    Highcharts.setOptions({
        global: {
            useUTC: false
        }
    });

    var staticData = [];
    // var _this = this;
    var timer1, timer2;
    // clearInterval(timer1)

    //接收到消息的回调方法
    websocket.onmessage = function (event) {
        // setMessageInnerHTML(event.data);
        if (event.data.indexOf("_d") !== -1){
            var df = event.data.substring(0, event.data.indexOf("_"));
            $('#df').html(df);
            //$('#jf').html(df);
        }else if (event.data.indexOf("=") !== -1){
            var df = event.data.substring(0, event.data.indexOf("="));
            $('#jf').html(df);
            ratio(1, df);
        }else if (event.data.indexOf("_z") !== -1) {
            var data = event.data;
            realRatio(event.data);
        }else {
            var data = JSON.parse(event.data);
            //console.log(data.spo2Value);
            if (data.NA !== null && data.NA !== undefined) {
                form.val('yp_form', data);
                xtGrade(data);
            }
            if (data.systValue !== null && data.systValue !== undefined) {
                var dias = data.diasValue;
                var syst = data.systValue;
                var icp = data.icpValue;
                var avgValue = Math.round((dias - syst)/3 + dias);
                var ngzyValue = avgValue - icp;
                $("#avgValue").val(avgValue);
                $("#ngzyValue").val(ngzyValue);
                form.val('indexForm', data);
                pressGrade(icp, avgValue, ngzyValue);
            }
            if (data.heartValue !== null && data.heartValue !== undefined) {
                form.val('heart_form', data);
                heartGrade(data);
            }
            if (data.spo2Value !== null && data.spo2Value !== undefined) {
                form.val('v_form', data);
                spo2Grade(data);
            }
            if (data.potlI !== null && data.potlI !== undefined) {
                var potlIs = data.potlI.split('^');
                for (var i=0; i<potlIs.length; i++) {
                    staticData.push(potlIs[i]);
                }
                var potlIis = data.potlIi.split('^');
                for (var i=0; i<potlIis.length; i++) {
                    staticData.push(potlIis[i]);
                }
                var potlIiis = data.potlIii.split('^');
                for (var i=0; i<potlIiis.length; i++) {
                    staticData.push(potlIiis[i]);
                }
                var potlAvrs = data.potlAvr.split('^');
                for (var i=0; i<potlAvrs.length; i++) {
                    staticData.push(potlAvrs[i]);
                }
                var potlAvls = data.potlAvl.split('^');
                for (var i=0; i<potlAvls.length; i++) {
                    staticData.push(potlAvls[i]);
                }
                var potlAvfs = data.potlAvf.split('^');
                for (var i=0; i<potlAvfs.length; i++) {
                    staticData.push(potlAvfs[i]);
                }
                var potlVs = data.potlV.split('^');
                for (var i=0; i<potlVs.length; i++) {
                    staticData.push(potlVs[i]);
                }
            }
        }

    };

    function setMessageInnerHTML(datas) {
        console.log(datas);

    }

    var myChart = echarts.init(document.getElementById('container'));

    function randomData(val) {
        now = new Date(+now + oneDay);
        value = val ? val : 128;
        return {
            name: now.toString(),
            value: [
                [now.getFullYear(), now.getMonth() + 1, now.getDate()].join('/'),
                // time,
                // Math.round(value)
                value
            ]
        }
    }

    var data = [];
    var now = +new Date(1997, 9, 3);
    var oneDay = 24 * 3600 * 1000;
    var value = Math.random() * 1000;
    for (var i = 0; i < 1000; i++) {
        data.push(randomData());
    }

    option = {
        title: {
            text: '心电图数据'
        },
        tooltip: {
            trigger: 'axis',
            formatter: function (params) {
                params = params[0];
                var date = new Date(params.name);
                return date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear() + ' : ' + params.value[1];
            },
            axisPointer: {
                animation: false
            }
        },
        xAxis: {
            type: 'time',
            splitLine: {
                show: false
            }
        },
        yAxis: {
            type: 'value',
            boundaryGap: [0, '100%'],
            splitLine: {
                show: false
            },
            max: 2000
        },
        series: [{
            name: '模拟数据',
            type: 'line',
            showSymbol: false,
            hoverAnimation: false,
            data: data
        }]
    };

    setInterval(function () {
        console.log(staticData.length);
        if(staticData.length > 0){
            for(var i=0;i<staticData.length;i++){
                data.shift();
                data.push(randomData(staticData[i]));
                staticData.shift()
            }
        }
        myChart.setOption({
            series: [{
                data: data
            }]
        });
    }, 1000);


    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);

    //连接关闭的回调方法
    websocket.onclose = function () {
        //setMessageInnerHTML("WebSocket连接关闭");
    };

    //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
    window.onbeforeunload = function () {
        closeWebSocket();
    };


    function activeLastPointToolip(chart) {
        var points = chart.series[0].points;
        chart.tooltip.refresh(points[points.length - 1]);
    }

    //关闭WebSocket连接
    function closeWebSocket() {
        websocket.close();
    }

    //发送消息
    function send() {
        var message = document.getElementById('text').value;

        //将字符串转换为byte数组
        var bytesArr = stringToByte(message);
        var bytes = new Uint8Array(bytesArr.length);
        for (var i = 0; i < bytes.length; i++) {
            bytes[i] = bytesArr[i];
        }
        console.log(bytes)
        websocket.send(bytes);
    }


    //将字符串转为 Array byte数组
    function stringToByte(str) {
        var bytes = new Array();
        var len, c;
        len = str.length;
        for (var i = 0; i < len; i++) {
            c = str.charCodeAt(i);
            if (c >= 0x010000 && c <= 0x10FFFF) {
                bytes.push(((c >> 18) & 0x07) | 0xF0);
                bytes.push(((c >> 12) & 0x3F) | 0x80);
                bytes.push(((c >> 6) & 0x3F) | 0x80);
                bytes.push((c & 0x3F) | 0x80);
            } else if (c >= 0x000800 && c <= 0x00FFFF) {
                bytes.push(((c >> 12) & 0x0F) | 0xE0);
                bytes.push(((c >> 6) & 0x3F) | 0x80);
                bytes.push((c & 0x3F) | 0x80);
            } else if (c >= 0x000080 && c <= 0x0007FF) {
                bytes.push(((c >> 6) & 0x1F) | 0xC0);
                bytes.push((c & 0x3F) | 0x80);
            } else {
                bytes.push(c & 0xFF);
            }
        }
        return bytes;


    }

    //byte数组转字符串
    function byteToString(arr) {
        if (typeof arr === 'string') {
            return arr;
        }
        var str = '',
            _arr = arr;
        for (var i = 0; i < _arr.length; i++) {
            var one = _arr[i].toString(2),
                v = one.match(/^1+?(?=0)/);
            if (v && one.length == 8) {
                var bytesLength = v[0].length;
                var store = _arr[i].toString(2).slice(7 - bytesLength);
                for (var st = 1; st < bytesLength; st++) {
                    store += _arr[st + i].toString(2).slice(2);
                }
                str += String.fromCharCode(parseInt(store, 2));
                i += bytesLength - 1;
            } else {
                str += String.fromCharCode(_arr[i]);
            }
        }
        return str;
    }

});

function timeAdd() {
    var tsTime = $("#tsTime").val();
    var id = $("#id").val();
    $.ajax({
        url: Feng.ctxPath + '/mmhc/updateTs',
        type: 'post',
        dataType: 'json',
        data: {
            id : id,
            tsTime : tsTime
        },
        success: function (result) {
            Feng.success("保存成功");
        }
    });

}

function gradeAdd() {
    var xyTime = $("#xyTime").val();
    var id = $("#id").val();
    $.ajax({
        url: Feng.ctxPath + '/mmhc/updateTs',
        type: 'post',
        dataType: 'json',
        data: {
            id : id,
            xyTime : xyTime
        },
        success: function (result) {
            Feng.success("保存成功");
        }
    });
}

function hzAdd() {
    var hzTime = $("#hzTime").val();
    var id = $("#id").val();
    $.ajax({
        url: Feng.ctxPath + '/mmhc/updateTs',
        type: 'post',
        dataType: 'json',
        data: {
            id : id,
            hzTime : hzTime
        },
        success: function (result) {
            Feng.success("保存成功");
        }
    });
}

function xtAdd() {
    var xtTime = $("#xtTime").val();
    var id = $("#id").val();
    $.ajax({
        url: Feng.ctxPath + '/mmhc/updateTs',
        type: 'post',
        dataType: 'json',
        data: {
            id : id,
            xtTime : xtTime
        },
        success: function (result) {
            Feng.success("保存成功");
        }
    });
}

function calculateGrad() {
    var tsTime = $("#tsTime").val();
    var hzTime = $("#hzTime").val();
    var id = $("#id").val();
    $.ajax({
        url: Feng.ctxPath + '/ecg/calculateGrad',
        type: 'post',
        dataType: 'json',
        data: {
            userId : id,
            num : tsTime,
            hzTime : hzTime
        },
        success: function (result) {
           $("#pf").html(result.data);
            ratio(0, result.data);
        }
    });
}

function staticAdd() {
    var indexMovement = $("#indexMovement").val();
    var tongReflect = $("#tongReflect").val();
    var tongChange = $("#tongChange").val();
    var indexXz = $("#indexXz").val();
    var indexNzx = $("#indexNzx").val();
    var indexZwm = $("#indexZwm").val();
    var indexHckd = $("#indexHckd").val();
    var indexXt = $("#indexXt").val();
    //根据出生日期计算出年龄
    var birthday = $("#birthday").val();
    var birthday_time=birthday.split("-");
    var birthYear = birthday_time[0];
    var dTime= new Date();
    var nowYear=dTime.getFullYear();
    var ag=nowYear-birthYear;

    //获取是否手术
    var surgery = $("input[name='isSurgery']:checked").val();

    //既往慢性病史
    var medicalHistory = $("#medicalHistory").val();
    $.ajax({
        url: Feng.ctxPath + '/index/add?hisId=' + Feng.getUrlParam("id"),
        type: 'post',
        dataType: 'json',
        data: {
            indexMovement: indexMovement,
            tongReflect: tongReflect,
            tongChange: tongChange,
            indexXz: indexXz,
            indexNzx: indexNzx,
            indexZwm: indexZwm,
            indexHckd: indexHckd,
            indexXt: indexXt,
            age : ag,
            surgery : surgery,
            medicalHistory : medicalHistory
        },
        success: function (result) {
            Feng.success("保存成功");
        }
    });
}

function xtGrade(data) {
    if (data.PH !== undefined && data.PH !== null){
        var value = data.PH;
        if (value >= 7.7 || value <=7.1){
            $('#div23').css('color', 'red');
        }else if(value >= 7.65 || value <=7.15){
            $('#div23').css('color', 'orangered');
        }else if(value >= 7.6 || value <=7.2){
            $('#div23').css('color', 'yellow');
        }else if(value >= 7.55 || value <=7.25){
            $('#div23').css('color', 'olive');
        }else if(value >= 7.5 || value <=7.3){
            $('#div23').css('color', 'green');
        }
    }
    if (data.K !== undefined && data.K !== null){
        var value = data.K;
        if (value >= 6.1 || value <= 2.5){
            $('#div24').css('color', 'red');
        }else if(value >= 5.8 || value <= 2.6){
            $('#div24').css('color', 'orangered');
        }else if(value >= 5.6 || value <= 2.8){
            $('#div24').css('color', 'yellow');
        }else if(value >= 5.3 || value <= 3){
            $('#div24').css('color', 'olive');
        }else if(value >= 5 || value <= 3.2){
            $('#div24').css('color', 'green');
        }
    }
    if (data.GLU !== undefined && data.GLU !== null){
        var value = data.GLU;
        if (value >= 25 || value <= 1){
            $('#div17').css('color', 'red');
        }else if(value >= 13 || value <= 1.5){
            $('#div17').css('color', 'orangered');
        }else if(value >= 9 || value <= 2){
            $('#div17').css('color', 'yellow');
        }else if(value >= 7 || value <= 2.2){
            $('#div17').css('color', 'olive');
        }else if(value >= 6 || value <= 3){
            $('#div17').css('color', 'green');
        }
    }
    if (data.LAC !== undefined && data.LAC !== null){
        var value = data.LAC;
        if (value >= 5.5 || value <= 0.05){
            $('#div18').css('color', 'red');
        }else if(value >= 5 || value <= 0.1){
            $('#div18').css('color', 'orangered');
        }else if(value >= 4.5 || value <= 0.2){
            $('#div18').css('color', 'yellow');
        }else if(value >= 4 || value <= 0.3){
            $('#div18').css('color', 'olive');
        }else if(value >= 3.5 || value <= 0.4){
            $('#div18').css('color', 'green');
        }
    }
    if (data.CL !== undefined && data.CL !== null){
        var value = data.CL;
        if (value >= 130 || value <= 73){
            $('#div14').css('color', 'red');
        }else if(value >= 125 || value <= 78){
            $('#div14').css('color', 'orangered');
        }else if(value >= 120 || value <= 83){
            $('#div14').css('color', 'yellow');
        }else if(value >= 115 || value <= 88){
            $('#div14').css('color', 'olive');
        }else if(value >= 110 || value <= 93){
            $('#div14').css('color', 'green');
        }
    }
    if (data.HCO3 !== undefined && data.HCO3 !== null){
        var value = data.HCO3;
        if (value >= 50 || value <= 6){
            $('#div26').css('color', 'red');
        }else if(value >= 48 || value <= 8){
            $('#div26').css('color', 'orangered');
        }else if(value >= 43 || value <= 10){
            $('#div26').css('color', 'yellow');
        }else if(value >= 38 || value <= 12){
            $('#div26').css('color', 'olive');
        }else if(value >= 33 || value <= 17){
            $('#div26').css('color', 'green');
        }
    }
    if (data.SO2 !== undefined && data.SO2 !== null){
        var value = data.SO2;
        if (value >= 105 || value <= 60){
            $('#div30').css('color', 'red');
        }else if(value >= 104 || value <= 70){
            $('#div30').css('color', 'orangered');
        }else if(value >= 103 || value <= 80){
            $('#div30').css('color', 'yellow');
        }else if(value >= 102 || value <= 85){
            $('#div30').css('color', 'olive');
        }else if(value >= 101 || value <= 90){
            $('#div30').css('color', 'green');
        }
    }
    if (data.BE !== undefined && data.BE !== null){
        var value = data.BE;
        if (value >= 8 || value <= -8){
            $('#div29').css('color', 'red');
        }else if(value >= 7 || value <= -7){
            $('#div29').css('color', 'orangered');
        }else if(value >= 6 || value <= -6){
            $('#div29').css('color', 'yellow');
        }else if(value >= 5 || value <= -5){
            $('#div29').css('color', 'olive');
        }else if(value >= 4 || value <= -4){
            $('#div29').css('color', 'green');
        }
    }
    if (data.TCO2 !== undefined && data.TCO2 !== null){
        var value = data.TCO2;
        if (value >= 53 || value <= 8){
            $('#div28').css('color', 'red');
        }else if(value >= 48 || value <= 10){
            $('#div28').css('color', 'orangered');
        }else if(value >= 43 || value <= 13){
            $('#div28').css('color', 'yellow');
        }else if(value >= 38 || value <= 16){
            $('#div28').css('color', 'olive');
        }else if(value >= 33 || value <= 19){
            $('#div28').css('color', 'green');
        }
    }
    if (data.PO2 !== undefined && data.PO2 !== null){
        var value = data.TCO2;
        if (value >= 55 || value <= 11){
            $('#div25').css('color', 'red');
        }else if(value >= 50 || value <= 12){
            $('#div25').css('color', 'orangered');
        }else if(value >= 45 || value <= 13){
            $('#div25').css('color', 'yellow');
        }else if(value >= 40 || value <= 15){
            $('#div25').css('color', 'olive');
        }else if(value >= 35 || value <= 18){
            $('#div25').css('color', 'green');
        }
    }
    if (data.ANGAP !== undefined && data.ANGAP !== null){
        var value = data.ANGAP;
        if (value >= 28 || value <= 3){
            $('#div19').css('color', 'red');
        }else if(value >= 26 || value <= 3.5){
            $('#div19').css('color', 'orangered');
        }else if(value >= 24 || value <= 4){
            $('#div19').css('color', 'yellow');
        }else if(value >= 22 || value <= 5){
            $('#div19').css('color', 'olive');
        }else if(value >= 20 || value <= 6){
            $('#div19').css('color', 'green');
        }
    }
}

function pressGrade(icp, avgValue, ngzyValue) {
    if (avgValue !== undefined && avgValue !== null){
        var value = avgValue;
        if (value >= 200 || value <= 25){
            $('#div4').css('color', 'red');
        }else if(value >= 190 || value <= 40){
            $('#div4').css('color', 'yellow');
        }else if(value >= 180 || value <= 55){
            $('#div4').css('color', 'olive');
        }else if(value >= 160 || value <= 70){
            $('#div4').css('color', 'green');
        }
    }
    if (icp !== undefined && icp !== null){
        var value = icp;
        if (value >= 35){
            $('#div5').css('color', 'red');
        }else if(value >= 30){
            $('#div5').css('color', 'orangered');
        }else if(value >= 25){
            $('#div5').css('color', 'yellow');
        }else if(value >= 22){
            $('#div5').css('color', 'olive');
        }else if(value >= 18){
            $('#div5').css('color', 'green');
        }
    }
    if (ngzyValue !== undefined && ngzyValue !== null){
        var value = ngzyValue;
        if (value <= 20){
            $('#div6').css('color', 'red');
        }else if(value <= 25){
            $('#div6').css('color', 'orangered');
        }else if(value <= 30){
            $('#div6').css('color', 'yellow');
        }else if(value <= 40){
            $('#div6').css('color', 'olive');
        }else if(value <= 60){
            $('#div6').css('color', 'green');
        }
    }
}
function heartGrade(date) {
    if (date.heartValue !== undefined && date.heartValue !== null){
        var value = date.heartValue;
        if (value >= 180 || value <= 40){
            $('#div10').css('color', 'red');
        }else if(value >= 160 || value <= 45){
            $('#div10').css('color', 'orangered');
        }else if(value >= 150 || value <= 50){
            $('#div10').css('color', 'yellow');
        }else if(value >= 140 || value <= 60){
            $('#div10').css('color', 'olive');
        }else if(value >= 110 || value <= 70){
            $('#div10').css('color', 'green');
        }
    }
    if (date.respValue !== undefined && date.respValue !== null){
        var value = date.respValue;
        if (value >= 10){
            $('#div11').css('color', 'red');
        }else if(value >= 9.5){
            $('#div11').css('color', 'orangered');
        }else if(value >= 9){
            $('#div11').css('color', 'yellow');
        }else if(value >= 8){
            $('#div11').css('color', 'olive');
        }else if(value >= 6){
            $('#div11').css('color', 'green');
        }
    }
}
function spo2Grade(date) {
    if (date.spo2Value !== undefined && date.spo2Value !== null){
        var value = date.spo2Value;
        if (value <= 65){
            $('#div7').css('color', 'red');
        }else if(value <= 70){
            $('#div7').css('color', 'orangered');
        }else if(value <= 75){
            $('#div7').css('color', 'yellow');
        }else if(value <= 80){
            $('#div7').css('color', 'olive');
        }else if(value <= 85){
            $('#div7').css('color', 'green');
        }
    }
}

function realRatio(dates) {
    var values = dates.substring(0, dates.indexOf("_"))
    var datas = JSON.parse(values);
    var arrayX = [];
    var arrayY = [];
    var value = datas[0].grade;
    for (var i=1; i<7; i++){
        var a = datas[i];
        for (var key in a){
            var v1 = a[key];
            var v2 = v1/value;
            arrayX[i] = key;
            arrayY[i] = v2.toFixed(2);
        }
    }

    var myChart = echarts.init(document.getElementById('container1'));

    // 指定图表的配置项和数据
    var option = {
        color: ['#3398DB'],
        tooltip: {
            trigger: 'axis',
            axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
            }
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: [
            {
                type: 'category',
                data: arrayX,
                axisTick: {
                    alignWithLabel: true
                }
            }
        ],
        yAxis: [
            {
                type: 'value'
            }
        ],
        series: [
            {
                name: '直接访问',
                type: 'bar',
                barWidth: '60%',
                data: arrayY
            }
        ]
    };

    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);
}

function ratio(flag, value) {
    var id = $("#id").val();
    $.ajax({
        url: Feng.ctxPath + '/grade/getRatio/?id='+id,
        type: 'post',
        dataType: 'json',
        success: function (result) {
            //Feng.success("保存成功");
            var dates = result.data;

            var arrayX = [];
            var arrayY = [];
            if (dates.length !== 0){
                for (var i=0; i<6; i++){
                    var a = dates[i];
                    for (var key in a){
                        var v1 = a[key];
                        var v2 = v1/value;
                        arrayX[i] = key;
                        arrayY[i] = v2.toFixed(2);
                    }
                }
            }
            var myChart;
            if (flag === 0){
                myChart = echarts.init(document.getElementById('container0'));
            }else{
                myChart = echarts.init(document.getElementById('container2'));
            }

            // 指定图表的配置项和数据
            var option = {
                color: ['#3398DB'],
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                        type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                    }
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: [
                    {
                        type: 'category',
                        data: arrayX,
                        axisTick: {
                            alignWithLabel: true
                        }
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: [
                    {
                        name: 'name',
                        type: 'bar',
                        barWidth: '60%',
                        data: arrayY
                    }
                ]
            };

            // 使用刚指定的配置项和数据显示图表。
            myChart.setOption(option);
        }
    });

}