
document.write("<script type='text/javascript' src='../static/bootstrap/bootstrap/js/bootstrap.js'></script>");
document.write("<link rel='stylesheet' href='../static/bootstrap/bootstrap/css/bootstrap.css'  media='all'>");
document.write("<script type='text/javascript' src='../public/public.js'></script>");

document.write("<script type='text/javascript' src='../public/interface.js'></script>");

document.write("<script type='text/javascript' src='../static/layui/layui.all.js'></script>");
document.write("<link rel='stylesheet' href='../static/layui/css/layui.css'  media='all'>");

document.write("<script type='text/javascript' src='../static/jquery/colorpicker.js'></script>");




$(function(){

    var comm = new Interface(1);

    var data_flag = 0;
    var TaktTimeDelay = 50;
    var OverTimeDelay = 200;
    var WriteTimeDelay = 400;
    var obj = {cnt:0, delay:0, delay1:500, delay2:500, overDelay:1200, set_delay:1, get_delay:100, InstrumentModel:null};

    $("#Text_Start").css("display", "inline");   $("#Text_Start").css("visibility", "inherit");
    $("#Button_On").css("display", "inline");   $("#Button_On").css("visibility", "inherit");
    $("#Button_Off").css("display", "none");   $("#Button_Off").css("visibility", "hidden");

    $("#Button_On").click(function () {//display: none;
        obj.cnt = -1;
        var dat = comm.setSerialSend(0, "SYST:KEYL OFF", "keyl_off", obj.delay1, obj.delay2, obj.overDelay);
        window.keyl_off = function(a){
            comm.setTimeDelay("t2", "0");
            layer.msg("<h1>暂停啦</h1>");
        }
    });
    $("#Button_Off").click(function () {//display: none;
        obj.cnt = 0;
        var dat = comm.setSerialSend(0, "disp:page TEST", "disp_test", obj.delay1, obj.delay2, obj.overDelay);
        window.disp_test = function(a){
            layer.msg("<h1>开始啦</h1>");
        }
    });

// +1.0000e+20,OFF  大数
// -5.6105e-06,OFF  小数
    function toUnit(num) {
        var yUnit = $("#select_YAxisUnit_input").val();
        if(yUnit == "u"){num =  num*1000000;}
        else if(yUnit == "m"){num =  num*1000;}
        else if(yUnit == "k"){num =  num/1000;}
        else if(yUnit == "M"){num =  num/1000000;}
        return num.toFixed(4);
    }
    function toOhm(num) {
        var yUnit = $("#select_YAxisUnit_input").val();
        if(yUnit == "u"){num = num/1000000;}
        else if(yUnit == "m"){num = num/1000;}
        else if(yUnit == "k"){num = num*1000;}
        else if(yUnit == "M"){num = num*1000000;}
        return num;
    }


    function StDev(arrData) //计算标准偏差
    {
        var xSum = 0;
        var xAvg = 0;
        var sSum = 0;
        var tmpStDev = 0;
        var arrNum = arrData.length;
        for (var i = 0; i < arrNum; i++)
        {
            xSum += arrData[i];
        }
        xAvg = xSum / arrNum;
        for (var j = 0; j < arrNum; j++)
        {
            sSum += ((arrData[j] - xAvg) * (arrData[j] - xAvg));
        }
        // tmpStDev = Convert.ToSingle(Math.sqrt((sSum / (arrNum - 1))).ToString());
        return Math.sqrt((sSum / (arrNum - 1)));
    }
    function get_NOMINAL(arrData) {//获取中心值
        var xSum = 0;
        var arrNum = arrData.length;
        for (var i = 0; i < arrNum; i++)
        {
            xSum += arrData[i];
        }
        return xSum / arrNum;
    }
    function get_USL(arrData) {//获取上限值
        var select = $("#select_ComparatorLevel_input").val().substr(1, 1);
        var max = $("#Input_ResistanceMax" + select + "_input").val();
        return max;
    }
    function get_LSL(arrData) {//获取下限值
        var select = $("#select_ComparatorLevel_input").val().substr(1, 1);
        var min = $("#Input_ResistanceMin" + select + "_input").val();
        return min;
    }
    function get_CP(arrData) {//Cp 离散趋势精确度(USL-LSL) / 6σ
        var max = $("#text_USL_text").text();
        var min = $("#text_LSL_text").text();
        var std = $("#text_STDEV_text").text();
        var cp = (max - min) / (6*std);
        return cp;
    }
    function get_CPK(arrData) {//CPK
        var max = $("#text_USL_text").text();
        var min = $("#text_LSL_text").text();
        var ave = $("#text_AVERAGE_text").text();//平均值
        var std = $("#text_STDEV_text").text();//标准差σ
        var cpku = (max - ave) / (3*std); // ( USL-ˉx ) / 3σ
        var cpkl = (ave - min) / (3*std); //  ( ˉx -LSL )/ 3σ
        var cpk = Math.min.apply(null, [cpku, cpkl]);
        return cpk;
    }

    window.t1 =  function() {
        if(++obj.delay < 3){

        }
        else if (obj.cnt == 0) {//查询控制方式
            if(comm.getSerialComm(0, "FETC?", "fetc", TaktTimeDelay, OverTimeDelay) != "ok"){}
            window.fetc = function (i, str) {
                if (str.length > 0) {
                    var ohm = Number(str.substring(0, str.indexOf(",")));
                    if (ohm >= 2500000) {
                        ohm = 2500000;
                    }
                    var num = toUnit(ohm);
                    my_chart.add_data(0, 0, ohm);

                    data_flag = 1;

                    var mode = $("#select_ComparatorMode_input").val();    //比较器模式
                    if (mode == "PER") {
                        var aaa = toOhm(num);
                        var nomi = toOhm($("#Input_NominalResistance1_input").val());  //电阻标称值
                        var bbb = ((aaa - nomi) / nomi * 100).toString();
                        if (bbb.indexOf(".") >= 0) {
                            bbb = bbb.substr(0, bbb.indexOf(".") + 3);
                        }
                        $("#Text_Percentage_text").text(bbb.toString() + "%");
                    }
                    else if (mode == "ABS") {
                        var nomi = $("#Input_NominalResistance1_input").val();  //电阻标称值
                        var value = (num - nomi).toString();
                        if (value.length > 8) {
                            value = value.substr(0, 8);
                        }
                        $("#Text_Percentage_text").text(value);
                    } else {
                        $("#Text_Percentage_text").text("无");
                    }

                    var value = num;
                    var Unit = $("#select_YAxisUnit_input").val();
                    if (Unit == "u") {
                        value += "uΩ";
                    }
                    else if (Unit == "m") {
                        value += "mΩ";
                    }
                    else if (Unit == "k") {
                        value += "KΩ";
                    }
                    else if (Unit == "M") {
                        value += "MΩ";
                    }
                    $("#Text_Resistance_text").text(value);

                    if (str.indexOf("OFF") >= 0) {
                        $("#Text_Pass_text").text("");
                    }
                    else if (str.indexOf("BIN0") >= 0) {
                        $("#Text_Pass_text").text("FAIL");
                    }
                    else {
                        $("#Text_Pass_text").text("PASS");
                    }
                    set_cpk();
                    set_cpk_chart();
                }
                obj.cnt = 1;
                obj.delay = 0;
            }
        }
        else if (obj.cnt == 1) {
            if(comm.getSerialComm(0, "COMP:BEEP?", "comp_beep", TaktTimeDelay, OverTimeDelay) != "ok"){}
            window.comp_beep = function (i, str) {
                if (str.length > 0) {
                    if (str == "OFF") {
                        $("#select_SetSound_input").val("关闭");
                    }
                    else if (str == "PASS") {
                        $("#select_SetSound_input").val("合格");
                    }
                    else if (str == "FAIL") {
                        $("#select_SetSound_input").val("不合格");
                    }
                }
                obj.delay = 0;
                if (obj.InstrumentModel == null) {obj.cnt++;}else{obj.cnt = 0;}
            }
        }
        else if (obj.cnt == 2) {
            if(comm.getSerialComm(0, "*IDN?", "idn", TaktTimeDelay, OverTimeDelay) != "ok"){}
            window.idn = function (i, str) {
                if (str.length > 0) {
                    obj.InstrumentModel = str;
                    $("#Text_Model_text").text(str);
                }
                obj.cnt = 0;
                obj.delay = 0;
            }
        }
    }

    $("#select_SetSound_input").change(function () {
        var select = $("#select_SetSound_input").val();
              if(select == "关闭")  {comm.setSerialSend(0, "COMP:BEEP OFF", "", obj.delay1, obj.delay2, obj.overDelay);}
        else if(select == "合格")  {comm.setSerialSend(0, "COMP:BEEP OK", "", obj.delay1, obj.delay2, obj.overDelay);}
        else if(select == "不合格"){comm.setSerialSend(0, "COMP:BEEP NG", "", obj.delay1, obj.delay2, obj.overDelay);}
    });

//设置比较器
    $("#Button_ComparatorSeting").click(function () {
        var Num = 0;
        var nomi = "";
        let loading = layer.load(2, {time: 10000});
        var mode = $("#select_ComparatorMode_input").val();    //比较器模式
        if(obj.InstrumentModel.indexOf("3513") >= 0){
            for(var i=1; i<7; i++){
                var enable = $("#select_Level" + i + "_input").val();
                if(enable == "ON"){
                    Num++;
                    var max = $("#Input_ResistanceMax" + i + "_input").val();       //电阻值上限
                    var min = $("#Input_ResistanceMin" + i + "_input").val();       //电阻值下限
                    if(mode != "PER"){
                        max = toOhm(max); min = toOhm(min);
                    }
                    comm.setSerialSend(0, "COMP:BIN 1" + "," + min + "," + max,   "", obj.delay1, obj.delay2, obj.overDelay);
                    if(nomi == ""){
                        nomi = toOhm($("#Input_NominalResistance" + i + "_input").val());  //电阻标称值
                    }
                    break;
                }
            }
            if(Num > 0){
                comm.setSerialSend(0, "COMP:STAT ON", "", obj.delay1, obj.delay2, obj.overDelay);
                comm.setSerialSend(0, "COMP:NOM " + nomi, "", obj.delay1, obj.delay2, obj.overDelay);
                comm.setSerialSend(0, "COMP:MODE " + mode, "doneSet", obj.delay1, obj.delay2, obj.overDelay);
            }else{
                comm.setSerialSend(0, "COMP:STAT OFF", "doneSet", obj.delay1, obj.delay2, obj.overDelay);
            }
        }
        else{
            for(var i=1; i<7; i++){
                var enable = $("#select_Level" + i + "_input").val();
                if(enable == "ON"){
                    Num++;
                    var max = $("#Input_ResistanceMax" + i + "_input").val();       //电阻值上限
                    var min = $("#Input_ResistanceMin" + i + "_input").val();       //电阻值下限
                    if(mode != "PER"){
                        max = toOhm(max); min = toOhm(min);
                    }
                    comm.setSerialSend(0, "COMP:BIN " + i + "," + min + "," + max, "", obj.delay1, obj.delay2, obj.overDelay);
                    if(nomi == ""){
                        nomi = toOhm($("#Input_NominalResistance" + i + "_input").val());  //电阻标称值
                    }
                }
            }
            if(Num > 0){
                comm.setSerialSend(0, "COMP:STAT " + Num + "-BIN", "", obj.delay1, obj.delay2, obj.overDelay);
                comm.setSerialSend(0, "COMP:NOM " + nomi, "", obj.delay1, obj.delay2, obj.overDelay);
                comm.setSerialSend(0, "COMP:MODE " + mode, "doneSet", obj.delay1, obj.delay2, obj.overDelay);
            }else{
                comm.setSerialSend(0, "COMP:STAT OFF", "doneSet", obj.delay1, obj.delay2, obj.overDelay);
            }
        }
        window.doneSet = function(a){
            layer.close(loading);
            layer.msg("<h1>完成设置</h1>");
        }
    });

    $("#Button_ResetComm").click(function () {
        $(location).attr('href', "../index/index.html");
    });

//列表显示测试结果
    {
        //初始化表头
        {
            InitTable();
            function InitTable() {
                var str_html1 = "\
                <table id='TableCell_List_ListTestTable' class='table table-bordered table-condensed'  style='background: white'>\
                    <tr>\
                        <th style='text-align: center; width: auto'>实际时间</th>\
                        <th style='text-align: center; width: auto'>间隔时间(s)</th>\
                        <th style='text-align: center; width: auto'>电阻</th>\
                        <th style='text-align: center; width: auto'>%</th>\
                        <th style='text-align: center; width: auto'>结果</th>\
                    </tr>\
                </table>";
                $("#ListTable_Test").html(str_html1);
                $("#ListTable_Test").css("left", $("#ListTable_Test").data("left"));
                $("#ListTable_Test").css("width", $("#ListTable_Test").data("width"));
                $("#ListTable_Test").css("height", $("#ListTable_Test").data("height"));
                $("#ListTable_Test").css("margin-top", $("#ListTable_Test").data("top"));
                $("#ListTable_Test").css("overflow", "scroll");
            }
        }
        function add_row(RealTime, Intervals, Resistance, Percentage, Result){
            var newRow = "\
                <tr style='background: white'>\
                    <td style=''>" + RealTime + "</td>\
                    <td style=''>" + Intervals + "</td>\
                    <td style='text-align: left'>" + Resistance + "</td>\
                    <td style=''>" + Percentage + "</td>\
                    <td style=''>" + Result + "</td>\
                </tr>";
            $("#TableCell_List_ListTestTable tr:last").after(newRow);//追加
            $("th").css({'padding':'0px', 'vertical-align': 'middle', 'margin': '0 0 0 0'});
            $("td").css({'padding':'0px', 'vertical-align': 'middle', 'margin': '0 0 0 0'});
            $("#ListTable_Test").scrollTop($("#ListTable_Test").prop('scrollHeight'));//滚屏到最后
        };

        function get_list_data() {
            var dat = [];
            $("#TableCell_List_ListTestTable tr").each(function(row){
                var row_dat = []
                $(this).find("td").each(function(col){
                    row_dat.push(["col" + col, $(this).text()]);
                });
                dat.push(row_dat);
            });
            return dat;
        }


        $("#Input_Auto").click(function () {
            t2();
            comm.setTimeDelay("t2", (parseFloat($("#Select_cycle_input").val()) * 1000).toString());

            // clearInterval(t2);
            // t2 = window.setInterval(TimeLog, parseInt((parseFloat($("#Select_cycle_input").val()) * 1000)));
            layer.msg("<h1>自动记录</h1>");
        });
        $("#Input_Manual").click(function () {
            t2();
            comm.setTimeDelay("t2", "0");
            layer.msg("<h1>手动记录</h1>");
        });
        $("#Input_New").click(function () {
            InitTable();
            comm.setTimeDelay("t2", "0");// clearInterval(t2);
            layer.msg("<h1>新建完成</h1>");
        });

        // 计算两个时间差 dateBegin 开始时间
        function timeFn(start) {
            var date1= start;  //开始时间
            // var date1= '2020/05/22 00:00:00';  //开始时间
            // var date2 = stop;    //结束时间
            var date2 = new Date();    //结束时间
            var date3 = date2.getTime() - new Date(date1).getTime();   //时间差的毫秒数
            //计算出相差天数
            var days = Math.floor(date3/(24*3600*1000));
            //计算出小时数
            var leave1 = date3%(24*3600*1000);    //计算天数后剩余的毫秒数
            var hours = Math.floor(leave1/(3600*1000));
            //计算相差分钟数
            var leave2 = leave1%(3600*1000);        //计算小时数后剩余的毫秒数
            var minutes = Math.floor(leave2/(60*1000));
            //计算相差秒数
            var leave3 = leave2%(60*1000);      //计算分钟数后剩余的毫秒数
            var seconds = parseInt(leave3/1000);//Math.round(leave3/1000);
            // return seconds;
            return seconds + (minutes*60) + (hours*60*60) + (days*60*60*24);
            // alert(" 相差 " + days + "天 " + hours + "小时 " + minutes + " 分钟" + seconds + " 秒")
        }
        function get_time() {
            var myDate = new Date();       //实例一个时间对象;
            var Y = myDate.getFullYear();  //获取系统的年;
            var M = myDate.getMonth()+1;   //获取系统月份,由于月份是从0开始计算,所以要加1
            var D = myDate.getDate();       //获取系统日,
            var h = myDate.getHours();      //获取系统时,
            var m = myDate.getMinutes();    //分
            var s = myDate.getSeconds();    //秒
            // return PrefixInteger(h,2) + ":" + PrefixInteger(m,2) + ":" + PrefixInteger(s,2);
            return Y + "/" + PrefixInteger(M,2) + "/" + PrefixInteger(D,2) + " " + PrefixInteger(h,2) + ":" + PrefixInteger(m,2) + ":" + PrefixInteger(s,2);
        }


        window.t2 = function() {
            var time = get_time();
            var date = get_list_data();
            var P = $("#Text_Pass_text").text();
            var R = $("#Text_Resistance_text").text();
            var B = $("#Text_Percentage_text").text();
            if(data_flag != 1){

            }
            else if(date.length > 1){
                add_row(get_time(), timeFn(date[date.length - 1][0][1]), R, B, P);
            }else{
                add_row(time, "0", R, B, P);
            }
            // data_flag = 0;
        };

        $("#Button_Archive").click(function () {
            var str = "";
            var date = get_list_data();
            for(var row=1; row<date.length; row++){
                str += row.toString() + ": ";
                str += "时间:" + date[row][0][1] + ", ";
                str += "间隔:" + date[row][1][1] + ", ";
                str += "阻值:" + date[row][2][1] + ", ";
                str += "偏差:" + date[row][3][1] + ", ";
                str += "结果:" + date[row][4][1] + "\r\n";
            }
            comm.saveFile(str, "Result");
        });
    }



//input值恢复与定时备份
    {
        var userAgent = myBrowser().indexOf("WOW64");
        function stringToBool(str) {
            if(str == "True" || str == "true" || str == "TRUE"){
                return true;
            }
            return false;
        }

        var id_list = [
            "select_YAxisUnit_input",                   //0
            "Input_BackgroundColor_input",             //1
            "Input_CurveColor_input",                   //2
            "Input_LabelColor_input",                   //3
            "select_CurveStyle_input",                  //4

            "select_Level1_input",                      //5
            "select_Level2_input",                      //6
            "select_Level3_input",                      //7
            "select_Level4_input",                      //8
            "select_Level5_input",                      //9
            "select_Level6_input",                      //10
            "Input_NominalResistance1_input",          //11
            "Input_NominalResistance2_input",          //12
            "Input_NominalResistance3_input",          //13
            "Input_NominalResistance4_input",          //14
            "Input_NominalResistance5_input",          //15
            "Input_NominalResistance6_input",          //16
            "Input_ResistanceMax1_input",               //17
            "Input_ResistanceMax2_input",               //18
            "Input_ResistanceMax3_input",               //19
            "Input_ResistanceMax4_input",               //20
            "Input_ResistanceMax5_input",               //21
            "Input_ResistanceMax6_input",               //22
            "Input_ResistanceMin1_input",               //23
            "Input_ResistanceMin2_input",               //24
            "Input_ResistanceMin3_input",               //25
            "Input_ResistanceMin4_input",               //26
            "Input_ResistanceMin5_input",               //27
            "Input_ResistanceMin6_input",               //28

            "select_ComparatorMode_input",              //29
            "Select_cycle_input",                        //30
            "select_ComparatorLevel_input",            //31
        ];

        if(userAgent >= 0){
            $("#" + id_list[0]).val(comm.getIni("dcresistancetester", id_list[0],  "u"));
            $("#" + id_list[1]).val(comm.getIni("dcresistancetester", id_list[1],  "#CCCCCC"));        $("#" + id_list[1]).attr("maxlength",7);
            $("#" + id_list[2]).val(comm.getIni("dcresistancetester", id_list[2],  "#0000FF"));        $("#" + id_list[2]).attr("maxlength",7);
            $("#" + id_list[3]).val(comm.getIni("dcresistancetester", id_list[3],  "#FF0000"));        $("#" + id_list[3]).attr("maxlength",7);
            $("#" + id_list[4]).val(comm.getIni("dcresistancetester", id_list[4],  "Differential"));

            for(var i=5; i<11; i++){
                $("#" + id_list[i]).val(comm.getIni("dcresistancetester", id_list[i],  "OFF"));
            }
            for(var i=11; i<29; i++){
                $("#" + id_list[i]).val(comm.getIni("dcresistancetester", id_list[i],  "0.00001"));    $("#" + id_list[i]).attr("maxlength",7);
            }
            $("#" + id_list[29]).val(comm.getIni("dcresistancetester", id_list[29],  "ABS"));
            $("#" + id_list[30]).val(comm.getIni("dcresistancetester", id_list[30],  "1"));             $("#" + id_list[30]).attr("maxlength",2);
            $("#" + id_list[31]).val(comm.getIni("dcresistancetester", id_list[31],  "档1"));

            window.t3 = function () {
                // var t_save = window.setInterval(function() {
                for (var i = 0; i < id_list.length; i++) {
                    comm.setIni("dcresistancetester", id_list[i], $("#" + id_list[i]).val());
                }
                // }, 10000);
            }
        }
    }

//比较器模式切换时恢复相应参数
    {
        var  mode_copy = $("#select_ComparatorMode_input").val();    //比较器模式
        $("#select_ComparatorMode_input").change(function () {
            var mode = $("#select_ComparatorMode_input").val();    //比较器模式
            for(var i=1; i<7; i++){
                var enable = $("#select_Level" + i + "_input").val();
                var max  = $("#Input_ResistanceMax" + i + "_input").val();       //电阻值上限
                var min  = $("#Input_ResistanceMin" + i + "_input").val();       //电阻值下限
                var nomi = $("#Input_NominalResistance" + i + "_input").val();  //电阻标称值

                comm.setIni("dcresistancetester", mode_copy + "_select_Level" + i, enable);
                comm.setIni("dcresistancetester", mode_copy + "_Input_ResistanceMax" + i, max);
                comm.setIni("dcresistancetester", mode_copy + "_Input_ResistanceMin" + i, min);
                comm.setIni("dcresistancetester", mode_copy + "_Input_NominalResistance" + i, nomi);

                $("#select_Level" + i + "_input").val(comm.getIni("dcresistancetester", mode + "_select_Level" + i,  "OFF"));
                $("#Input_ResistanceMax" + i + "_input").val(comm.getIni("dcresistancetester", mode + "_Input_ResistanceMax" + i,  "0.00001"));
                $("#Input_ResistanceMin" + i + "_input").val(comm.getIni("dcresistancetester", mode + "_Input_ResistanceMin" + i,  "0.00001"));
                $("#Input_NominalResistance" + i + "_input").val(comm.getIni("dcresistancetester", mode + "_Input_NominalResistance" + i,  "0.00001"));
            }
            mode_copy = mode;
        });
    }


//如下为图表显示
    {
        var my_chart = new eEhart("#Chart_Resistance", "", [{name: '阻值'}], 1);
        var cpk_chart = new eEhart("#Chart_Statistics", "", [{name: 'cpk'}], 1);

        my_chart.option_data[0].yAxis = {
            type: 'value',
            // name: '蒸发量',
            // min: 0,
            // max: 250,
            position: 'left',
            axisLine: {onZero: false},
            axisLabel: {
                formatter: '{value} Ω'
            }
        };

        //背景色
        var a = Colorpicker.create({
            el: "Button_BackgroundColor",
            color: $('#Input_BackgroundColor_input').val(),
            change: function (elem, hex) {
                $('#Input_BackgroundColor_input').val(hex);
                $('#box_BackgroundColor_div').css("backgroundColor", hex);
                $("#Chart_Resistance").css("background-color", hex);
                // $("#Input_BackgroundColor_input").trigger("change");//触发事件
            }
        });
        $("#Chart_Resistance").css("background-color", $('#Input_BackgroundColor_input').val());

        //曲线颜色
        var b = Colorpicker.create({
            el: "Button_CurveColor",
            color: $("#Input_CurveColor_input").val(),
            change: function (elem, hex) {
                $('#Input_CurveColor_input').val(hex);
                $('#box_CurveColor_div').css("backgroundColor", hex);
                my_chart.option_data[0].series[0].color = $("#Input_CurveColor_input").val();
                my_chart.echart.setOption(my_chart.option_data);
                // $("#Input_CurveColor_input").trigger("change");//触发事件
            }
        });
        my_chart.option_data[0].series[0].color = $("#Input_CurveColor_input").val();

        //标签背景色
        var c = Colorpicker.create({
            el: "Button_LabelColor",
            color: $("#Input_LabelColor_input").val(),
            change: function (elem, hex) {
                $('#Input_LabelColor_input').val(hex);
                $('#box_LabelColor_div').css("backgroundColor", hex);
                my_chart.option_data[0].tooltip.backgroundColor = $("#Input_LabelColor_input").val();
                // $("#Input_LabelColor_input").trigger("change");//触发事件
            }
        });
        my_chart.option_data[0].tooltip.backgroundColor = $("#Input_LabelColor_input").val();

        set_smooth();
        function set_smooth() {
            var select = $("#select_CurveStyle_input").val();
            if(select == "Straightline"){
                my_chart.option_data[0].series[0].smooth = false;
                my_chart.echart.setOption(my_chart.option_data[0]);
            }
            else if(select == "Differential"){
                my_chart.option_data[0].series[0].smooth = true;
                my_chart.echart.setOption(my_chart.option_data[0]);
            }
        }
        $("#select_CurveStyle_input").change(function () {
            set_smooth();
        });

        //水平滚动条联动
        function dataZoom(params){
            if(params.dataZoomId.indexOf("1") < 0){
                var end = 100,start = 0;
                if(params.start == undefined){
                    end = params.batch[0].end;
                    start = params.batch[0].start;
                }else{
                    end = params.end;
                    start = params.start;
                }
                my_chart.option_data[0].dataZoom[0].end = end;
                my_chart.option_data[0].dataZoom[0].start = start;
                my_chart.echart.setOption(my_chart.option_data[0]);
            }
        }
        my_chart.echart.on('dataZoom', function (params) {
            dataZoom(params);
        });
    }


    {
        function set_cpk() {
            $("#text_STDEV_text").text(StDev(my_chart.option_data[0].series[0].data)); //基于样本估算标准偏差
            $("#text_SAMPLE_text").text(my_chart.option_data[0].series[0].data.length);//样品总数
            $("#text_NOMINAL_text").text(get_NOMINAL(my_chart.option_data[0].series[0].data));//中心值
            $("#text_USL_text").text(get_USL());//上限值
            $("#text_LSL_text").text(get_LSL());//下限值
            $("#text_AVERAGE_text").text(get_NOMINAL(my_chart.option_data[0].series[0].data));//平均值
            $("#text_MAX_text").text(Math.max.apply(null, my_chart.option_data[0].series[0].data));//最大值
            $("#text_MIN_text").text(Math.min.apply(null, my_chart.option_data[0].series[0].data));//最小值
            $("#text_CP_text").text(get_CP());//Cp 离散趋势精确度
            $("#text_CPK_text").text(get_CPK());//CPK
        }


        //加
        function add(a, b) {
            var c, d, e;
            try {
                c = a.toString().split(".")[1].length;
            } catch (f) {
                c = 0;
            }
            try {
                d = b.toString().split(".")[1].length;
            } catch (f) {
                d = 0;
            }
            return e = Math.pow(10, Math.max(c, d)), (mul(a, e) + mul(b, e)) / e;
        }
        //减
        function sub(a, b) {
            var c, d, e;
            try {
                c = a.toString().split(".")[1].length;
            } catch (f) {
                c = 0;
            }
            try {
                d = b.toString().split(".")[1].length;
            } catch (f) {
                d = 0;
            }
            return e = Math.pow(10, Math.max(c, d)), (mul(a, e) - mul(b, e)) / e;
        }
        //乘
        function mul(a, b) {
            var c = 0,
                d = a.toString(),
                e = b.toString();
            try {
                c += d.split(".")[1].length;
            } catch (f) {}
            try {
                c += e.split(".")[1].length;
            } catch (f) {}
            return Number(d.replace(".", "")) * Number(e.replace(".", "")) / Math.pow(10, c);
        }
        //除
        function div(a, b) {
            var c, d, e = 0,
                f = 0;
            try {
                e = a.toString().split(".")[1].length;
            } catch (g) {}
            try {
                f = b.toString().split(".")[1].length;
            } catch (g) {}
            return c = Number(a.toString().replace(".", "")), d = Number(b.toString().replace(".", "")), mul(c / d, Math.pow(10, f - e));
        }

        cpk_chart.option_data[0].dataZoom[0].start = 0;
        cpk_chart.option_data[0].dataZoom[0].show = false;
        cpk_chart.option_data[0].dataZoom[1].show = false;
        cpk_chart.option_data[0].xAxis.data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
        cpk_chart.option_data[0].series[0].data = cpk_chart.option_data[0].xAxis.data;
        cpk_chart.echart.setOption(cpk_chart.option_data[0]);
        function set_cpk_chart() {
            var max = $("#text_USL_text").text(); //max = 6.1303000000001;
            var min = $("#text_LSL_text").text(); //min = 1.333335;
            // var max = $("#text_MAX_text").text(); //max = 6.1303000000001;
            // var min = $("#text_MIN_text").text(); //min = 1.333335;
            var ofset = sub(max, min);
            var jiege = div(ofset, (cpk_chart.option_data[0].xAxis.data.length - 1));

            var temp = [];
            for(var i=0; i<cpk_chart.option_data[0].xAxis.data.length; i++){
                temp.push(add(mul(jiege, i),min).toFixed(4));
                cpk_chart.option_data[0].series[0].data[i] = 0;
            }
            cpk_chart.option_data[0].xAxis.data = temp;
            for(var a=0; a<my_chart.option_data[0].series[0].data.length; a++){
                var dat = toUnit(my_chart.option_data[0].series[0].data[a]);
                for(var b=0; b<cpk_chart.option_data[0].xAxis.data.length; b++){
                    if(dat <= cpk_chart.option_data[0].xAxis.data[b]){
                        cpk_chart.option_data[0].series[0].data[b]++;
                        b = cpk_chart.option_data[0].xAxis.data.length;
                    }
                }
            }
            cpk_chart.echart.setOption(cpk_chart.option_data[0]);
        }
    }

    {
        //打印
        $("#Button_Printf").click(function () {
            var img = my_chart.echart.getDataURL();
            printTable('<image id="img_chart" style="width: 100%; height: 100%" class="img" src="' + img + '"/>');
        });

        //预览
        $("#Button_Preview").click(function () {
            var img = my_chart.echart.getDataURL();
            var printWindow = window.open();
            printWindow.document.write('<image id="img_chart" style="width: 100%; height: 100%" class="img" src="' + img + '"/>');
        });

        function printTable(str) {
            var orderhtml = "";
            if (document.getElementById(str)) { orderhtml = document.getElementById(str).outerHTML; }
            else orderhtml = str;
            /* 创建iframe */
            var headobj = document.getElementsByTagName("head").item(0); //提取head
            printFrame = document.getElementById("lldxi_printRegionFrame_2012_0112");
            if (printFrame) { document.body.removeChild(printFrame); }
            printFrame = document.createElement("iframe");
            printFrame.setAttribute("src", "about:blank");
            printFrame.setAttribute("id", "lldxi_printRegionFrame_2012_0112");
            printFrame.setAttribute("marginheight", "0");
            printFrame.setAttribute("marginwidth", "0");
            printFrame.style.display = "none";
            document.body.appendChild(printFrame);
            if (window.ActiveXObject)//ie
            {
                var htmlobj = printFrame.contentWindow.document.createElement("html"); var bodyobj = printFrame.contentWindow.document.createElement("body");
                bodyobj.innerHTML = orderhtml; htmlobj.appendChild(headobj.cloneNode(true)); htmlobj.appendChild(bodyobj);
                printFrame.contentWindow.document.appendChild(htmlobj); printFrame.contentWindow.document.execCommand("Print", true);
            }
            else {
                var htmlstr = "<html>" + headobj.outerHTML + "<body>" + orderhtml + "<script type=\"text/javascript\">window.print();<\/script><\/body>" + "<\/html>";
                printFrame.contentWindow.document.write(htmlstr);
            }
        }
    }

    comm.setTime([["t1", 100], ["t2", 0], ["t3", 10000]]);
    // comm.setTimes(JSON.stringify({Time0_Name:"t1",Time0_Delay:"100",Time1_Name:"t2",Time1_Delay:"0",Time2_Name:"t3",Time2_Delay:"10000",}));





});






