var evtindex = '';
var ptpass = '';
var $wave = $("#wave").eq(0);
var $rms = $("#rms").eq(0);

var instantFAn = {};
var instantSAn = {};
var color;

$(document).ready(function () {
    getThemeColor();
    var idea;
    var eventIndex = $("#eventIndex").val();
    eventIndex = filterXSS(eventIndex);
    evtindex = eventIndex;
    $.ajax({
        type: "POST",
        url: "/pqs9200/area/getWavedata",
        dataType: "json",
        data: {
            eventIndex: eventIndex
        },
        beforeSend: function () {
            idea = ityzl_SHOW_LOAD_LAYER();
        },
        success: function (data) {
            if (data.code === 500) {
                layer.msg("没有获取到该事件的波形文件", {
                    icon: 5,
                    time: 3000
                });
                initWaves(null);
                document.getElementById("analysis").disabled = true;
            } else {
                initWaves(data.body);
                ptpass = data.body.pt;
            }
            //***********关闭loading
            ityzl_CLOSE_LOAD_LAYER(idea);
        }
    });
});

//获取主题颜色
function getThemeColor() {
    $.ajax({
        type: "POST",
        url: "/pqs9200/theme/getThemeInfo",
        async: false,
        dataType: 'json',
        success: function (data) {
            color = data.body.value;
        }
    })
}


//波形总体数据构造函数
function waveData(instantF, instantS, RMSF, RMSS, RMSFMinDetail, RMSSMinDetail, shunshiF, shunshiS, RMSFWave, RMSSWave, title, unit) {
    /*瞬时一次值最大最小值*/
    this.instantF = instantF;
    /*瞬时二次值最大最小值*/
    this.instantS = instantS;
    /*RMS一次值最大最小值*/
    this.RMSF = RMSF;
    /*RMS二次值最大最小值*/
    this.RMSS = RMSS;
    /*RMS一次值最小值具体的X Y周的位置，已经具体的相别*/
    this.RMSFMinDetail = RMSFMinDetail;
    /*RMS二次值最小值具体的X Y周的位置，已经具体的相别*/
    this.RMSSMinDetail = RMSSMinDetail;
    /*瞬时值一次值3相详细数据*/
    this.shunshiF = shunshiF;
    /*瞬时值二次值3相详细数据*/
    this.shunshiS = shunshiS;
    /*RMS一次值3相详细数据*/
    this.RMSFWave = RMSFWave;
    /*RMS二次值3相详细数据*/
    this.RMSSWave = RMSSWave;
    /*三相名称*/
    this.title = title;
    /*电压还是电流*/
    this.unit = unit;
}

//是否为开口三角，如果是，仅显示两相的曲线
var isOpen = false;
//时间
var time, type, severity, iPhasic;
var eventValue, persistTime, lineName, subName;
var waveDatas = [];

//根据后台查询的数据，清洗数据
function initWaves(wp) {
    if (null != wp) {
        eventValue = wp.eventValue;
        persistTime = wp.persistTime;
        lineName = wp.lineName;
        isOpen = wp.openTri;
        subName = wp.subName;
        //首先根据标题的长度，计算出需要绘制几份图（常见的有1张或2张，后期如果出现双路电压电流，便是4张波形图），考虑到波形的相数，实际情况更复杂些
        //相的数量
        iPhasic = wp.iPhasic;
        if (iPhasic === 0) {
            iPhasic = 1;
        }
        var picCounts = (wp.tmpWaveTitle.length - 1) / iPhasic;
        waveDatas = [];
        //循环筛选需求的数据
        for (var i = 0; i < picCounts; i++) {
            waveDatas.push(fliteWaveData(wp, i))
        }
        time = wp.time;
        type = wp.waveType;
        severity = wp.yzd;
        //严重度为负，事件为暂升事件
        if (severity < 0) {
            severity = "/";
            type = "/";
        }
        initWave(waveDatas, time, type, severity, isOpen);
    } else {
        initWave(null, null, null, null, null);
    }
}

//当用户选择一次值、二次值的时候，变更图形
$("#interval").change(
    function () {
        initWave(waveDatas, time, type, severity, isOpen);
    });

//开始画图
function initWave(waveDatas, time, type, severity, isOpen) {
    //清除之前增加的div
    $("#wave ~ .bx").remove();
    $("#rms ~ .bx").remove();
    //设置暂降触发点的位置 一次值与二次值Y轴不同（不是计算出来的）
    var height = $(window).height() - 90;
    var picHeight;
    var show = true;
    if (isOpen) {
        show = false;
    }
    var v = $("#interval").val();
    var isvisible = false;
    var cu = [], rmscu = [], title, unit, max, min;
    var a = "", b = "", c = "";
    var adata = [], bdata = [], cdata = [];
    var radata = [], rbdata = [], rcdata = [];
    var rmsvX = 0, rmsvY = 0, zhou = 0;
    if (null == waveDatas || waveDatas.length === 1) {
        picHeight = height;
    } else if (waveDatas.length > 1) {
        picHeight = height * 0.75;
    }
    $wave.css("height", picHeight);
    $rms.css("height", picHeight);
    var colors = [];
    if (null == waveDatas) {
        cu = [];
        rmscu = [];
        title = '该事件暂无波形图';
    } else if (waveDatas.length > 0) {
        title = '变电站名称：' + subName + ' 监测点名称：' + lineName + ' 发生时刻：' + time + ' 暂降幅值：' + eventValue + '% 持续时间：' + persistTime + "s";
        if (Number(eventValue) <= 90) {
            isvisible = true;
        }
        switch (iPhasic) {
            case 1:
                a = waveDatas[0].title.aTitle;
                if (v === "one") {
                    cu = [['0', waveDatas[0].instantF.min]];
                    rmscu = [['0', waveDatas[0].RMSF.min]];
                    max = waveDatas[0].instantF.max;
                    min = waveDatas[0].instantF.min;
                    adata = waveDatas[0].shunshiF.shunshiFA;

                    radata = waveDatas[0].RMSFWave.rmsFA;

                    rmsvX = waveDatas[0].RMSFMinDetail.rmsvFirstX;
                    rmsvY = waveDatas[0].RMSFMinDetail.rmsvFirstY;
                    zhou = waveDatas[0].RMSFMinDetail.firstZhou;

                } else {
                    cu = [['0', waveDatas[0].instantS.min]];
                    rmscu = [['0', waveDatas[0].RMSS.min]];
                    max = waveDatas[0].instantS.max;
                    min = waveDatas[0].instantS.min;
                    adata = waveDatas[0].shunshiS.shunshiSA;

                    radata = waveDatas[0].RMSSWave.rmsSA;

                    rmsvX = waveDatas[0].RMSSMinDetail.rmsvSecondX;
                    rmsvY = waveDatas[0].RMSSMinDetail.rmsvSecondY;
                    zhou = waveDatas[0].RMSSMinDetail.secondeZhou;

                }

                colors.push(yellow);
                colors.push("#fff");
                colors.push("#fff");
                break;
            case 2:
                a = waveDatas[0].title.aTitle;
                b = waveDatas[0].title.bTitle;
                if (v === "one") {
                    cu = [['0', waveDatas[0].instantF.min]];
                    rmscu = [['0', waveDatas[0].RMSF.min]];
                    max = waveDatas[0].instantF.max;
                    min = waveDatas[0].instantF.min;
                    adata = waveDatas[0].shunshiF.shunshiFA;
                    bdata = waveDatas[0].shunshiF.shunshiFB;

                    radata = waveDatas[0].RMSFWave.rmsFA;
                    rbdata = waveDatas[0].RMSFWave.rmsFB;

                    rmsvX = waveDatas[0].RMSFMinDetail.rmsvFirstX;
                    rmsvY = waveDatas[0].RMSFMinDetail.rmsvFirstY;
                    zhou = waveDatas[0].RMSFMinDetail.firstZhou;

                } else {
                    cu = [['0', waveDatas[0].instantS.min]];
                    rmscu = [['0', waveDatas[0].RMSS.min]];
                    max = waveDatas[0].instantS.max;
                    min = waveDatas[0].instantS.min;
                    adata = waveDatas[0].shunshiS.shunshiSA;
                    bdata = waveDatas[0].shunshiS.shunshiSB;

                    radata = waveDatas[0].RMSSWave.rmsSA;
                    rbdata = waveDatas[0].RMSSWave.rmsSB;

                    rmsvX = waveDatas[0].RMSSMinDetail.rmsvSecondX;
                    rmsvY = waveDatas[0].RMSSMinDetail.rmsvSecondY;
                    zhou = waveDatas[0].RMSSMinDetail.secondeZhou;

                }
                colors.push(yellow);
                colors.push(green);
                colors.push("#fff");
                break;
            case 3:
                a = waveDatas[0].title.aTitle;
                b = waveDatas[0].title.bTitle;
                c = waveDatas[0].title.cTitle;
                if (v === "one") {
                    cu = [['0', waveDatas[0].instantF.min]];
                    rmscu = [['0', waveDatas[0].RMSF.min]];
                    max = waveDatas[0].instantF.max;
                    min = waveDatas[0].instantF.min;
                    adata = waveDatas[0].shunshiF.shunshiFA;
                    bdata = waveDatas[0].shunshiF.shunshiFB;
                    cdata = waveDatas[0].shunshiF.shunshiFC;

                    radata = waveDatas[0].RMSFWave.rmsFA;
                    rbdata = waveDatas[0].RMSFWave.rmsFB;
                    rcdata = waveDatas[0].RMSFWave.rmsFC;

                    rmsvX = waveDatas[0].RMSFMinDetail.rmsvFirstX;
                    rmsvY = waveDatas[0].RMSFMinDetail.rmsvFirstY;
                    zhou = waveDatas[0].RMSFMinDetail.firstZhou;

                } else {
                    cu = [['0', waveDatas[0].instantS.min]];
                    rmscu = [['0', waveDatas[0].RMSS.min]];
                    max = waveDatas[0].instantS.max;
                    min = waveDatas[0].instantS.min;
                    adata = waveDatas[0].shunshiS.shunshiSA;
                    bdata = waveDatas[0].shunshiS.shunshiSB;
                    cdata = waveDatas[0].shunshiS.shunshiSC;

                    radata = waveDatas[0].RMSSWave.rmsSA;
                    rbdata = waveDatas[0].RMSSWave.rmsSB;
                    rcdata = waveDatas[0].RMSSWave.rmsSC;

                    rmsvX = waveDatas[0].RMSSMinDetail.rmsvSecondX;
                    rmsvY = waveDatas[0].RMSSMinDetail.rmsvSecondY;
                    zhou = waveDatas[0].RMSSMinDetail.secondeZhou;

                }

                colors.push(yellow);
                colors.push(green);
                colors.push(red);
                break;
        }


        if (waveDatas[0].unit === "电压") {
            if (v === "one") {
                unit = 'kV';
            } else {
                unit = 'V';
            }
        } else {
            unit = 'A';
        }
        //把剩余的DIV先拼接好
        for (var step = 1; step < waveDatas.length; step++) {
            var waveId = 'wave' + step;
            var rmsId = 'rms' + step;
            var newDivShunshi = $("<div class='bx' id='" + waveId + "'></div>");
            var newDivRms = $("<div class='bx' id='" + rmsId + "'></div>");
            newDivShunshi.insertAfter($wave);
            newDivRms.insertAfter($rms);
            $("#" + waveId).css("height", picHeight);
            $("#" + rmsId).css("height", picHeight);
        }
    } else {
        cu = [];
        rmscu = [];
        title = '变电站名称：' + subName + ' 监测点名称：' + lineName + ' 发生时刻：' + time + ' 暂降幅值：' + eventValue + '% 持续时间：' + persistTime + "s";
    }
    //绘制横向第一个波形图
    //绘制瞬时波形图
    Highcharts.stockChart('wave', {
        title: {
            text: title,
            floating: true,
            style: {fontSize: "14px"},
            y: -12
        },
        chart: {
            type: 'spline',
            events: {
                load: function () {
                    if (show) {
                        this.series[1].show();
                    } else {
                        this.series[1].hide();
                    }
                }
            },
            spacingTop: 30
        },
        plotOptions: {
            series: {
                animation: false
            }
        },
        //导出选中背景色
        navigation: {
            menuItemHoverStyle: {
                background: color,
                color: "#ffffff"
            },
        },
        tooltip: {
            borderColor: grey,
            backgroundColor: 'rgba(105,105,105,0.7)',
            style: {
                color: 'white',
                fontSize: "15px",
                padding: 10
            },
            formatter: function () {
                var s = '<b>时刻：' + this.x + 'ms</b>';
                $.each(this.points, function () {
                    s += '<br/>' + this.series.name + ': ' +
                        this.y.toFixed(2) + unit;
                });
                return s;
            },
            enabled: true,
            shared: true
        },
        rangeSelector: {
            enabled: false
        },
        //导航器各项属性设置
        navigator: {
            margin: 1,
            height: 15,
            xAxis: {
                labels: {
                    formatter: function () {
                        return this.value;
                    }
                }
            },
            series: [{
                data: adata
            }, {
                data: bdata
            }, {
                data: cdata
            }]
        },
        scrollbar: {
            enabled: false
        },
        credits: false,
        colors: colors,
        xAxis: {
            title: {
                text: 'ms',
                enabled: true,
                align: 'high'
            },
            labels: {
                formatter: function () {
                    return this.value;
                }
            }
        },
        yAxis: {
            title: {
                align: 'high',
                offset: 0,
                text: unit,
                rotation: 0,
                y: -10
            },
            showLastLabel: true,
            max: max,
            min: min,
            opposite: false
        },
        legend: {
            align: 'left',
            verticalAlign: 'top',
            enabled: true,
            itemDistance: 5
        },
        series: [{
            marker: {
                symbol: 'circle'
            },
            name: a,
            data: adata
        }, {
            marker: {
                symbol: 'circle'
            },
            name: b,
            data: bdata,
            showInLegend: show
        }, {
            marker: {
                symbol: 'circle'
            },
            name: c,
            data: cdata
        }, {
            type: 'scatter',
            name: '暂降触发点',
            marker: {
                symbol: 'url(/pqs9200/images/point.png)',
                width: 16,
                height: 16
            },
            data: cu
        }
        ],
    });
    //绘制RMS波形图
    Highcharts.stockChart('rms', {
        plotOptions: {
            series: {
                animation: false
            }
        },
        title: {
            text: title,
            floating: true,
            style: {fontSize: "14px"},
            y: -12
        },
        // subtitle: {
        //     text: '电压',
        //     align: 'left'
        // },
        chart: {
            type: 'spline',
            events: {
                load: function () {
                    if (show) {
                        this.series[1].show();
                    } else {
                        this.series[1].hide();
                    }
                }
            },
            spacingTop: 30
        },
        //导出选中背景色
        navigation: {
            menuItemHoverStyle: {
                background: color,
                color: "#ffffff"
            },
        },
        tooltip: {
            borderColor: grey,
            backgroundColor: 'rgba(105,105,105,0.7)',
            style: {
                color: 'white',
                fontSize: "15px",
                padding: 10
            },
            formatter: function () {
                var s = '<b>时刻：' + this.x + 'ms</b>';
                $.each(this.points, function () {
                    s += '<br/>' + this.series.name + ': ' +
                        this.y.toFixed(2) + unit;
                });
                return s;
            },
            shared: true
        },
        rangeSelector: {
            enabled: false,
        },
        //导航器各项属性设置
        navigator: {
            margin: 1,
            height: 15,
            xAxis: {
                labels: {
                    formatter: function () {
                        return this.value;
                    }
                }
            },
            series: [{
                data: radata
            }, {
                data: rbdata
            }, {
                data: rcdata
            }]

        },
        scrollbar: {
            enabled: false
        },
        credits: false,
        colors: colors,
        xAxis: {
            title: {
                text: 'ms',
                enabled: true,
                align: 'high'
            },
            labels: {
                formatter: function () {
                    return this.value;
                }
            }
        },
        yAxis: {
            title: {
                align: 'high',
                offset: 0,
                text: unit,
                rotation: 0,
                y: -10
            },
            showLastLabel: true,
            opposite: false
        },
        legend: {
            align: 'left',
            verticalAlign: 'top',
            enabled: true,
            itemDistance: 5
        },
        series: [{
            marker: {
                symbol: 'circle'
            },
            name: a,
            data: radata,
            id: 'a'
        }, {
            marker: {
                symbol: 'circle'
            },
            name: b,
            data: rbdata,
            showInLegend: show,
            id: 'b'
        }, {
            marker: {
                symbol: 'circle'
            },
            name: c,
            data: rcdata,
            id: 'c'
        }, {
            type: 'scatter',
            name: '暂降触发点',
            marker: {
                symbol: 'url(/pqs9200/images/point.png)',
                width: 16,
                height: 16
            },
            data: rmscu
        }, {
            type: 'flags',
            color: '#34c0e2',
            name: '最小暂降幅值',
            cursor: 'pointer',
            visible: isvisible,
            showInLegend: isvisible,
            data: [{
                x: rmsvX,
                title: rmsvY
            }],
            onSeries: zhou,
            shape: 'flag'
        }
        ]
    });

    //第一个波形图数据绘制完毕后，绘制后续的波形图
    if (waveDatas !== null && waveDatas.length > 1) {
        var waveDatasTemp = waveDatas.slice(1);
        waveDatasTemp.reverse();
        for (var step = 0; step < waveDatasTemp.length; step++) {
            var waveDataTemp = waveDatasTemp[step];
            drawPics(waveDataTemp, picHeight, step, show);
        }
    }
}

//绘制剩余横向的波形图
function drawPics(waveDataTemp, picHeight, step, show) {
    step = step + 1;
    //新建瞬时DIV和RMS波形DIV
    var waveId = 'wave' + step;
    var rmsId = 'rms' + step;
    var v = $("#interval").val();
    var a = "", b = "", c = "";
    var max, min, unit;
    var adata = [], bdata = [], cdata = [];
    var radata = [], rbdata = [], rcdata = [];
    var colors = [];
    switch (iPhasic) {
        case 1:
            a = waveDataTemp.title.aTitle;
            colors.push(yellow);
            colors.push("#fff");
            colors.push("#fff");
            break;
        case 2:
            a = waveDataTemp.title.aTitle;
            b = waveDataTemp.title.bTitle;
            colors.push(yellow);
            colors.push(green);
            colors.push("#fff");
            break;
        case 3:
            a = waveDataTemp.title.aTitle;
            b = waveDataTemp.title.bTitle;
            c = waveDataTemp.title.cTitle;
            colors.push(yellow);
            colors.push(green);
            colors.push(red);
            break;
    }

    if (v === "one") {
        max = waveDataTemp.instantF.max;
        min = waveDataTemp.instantF.min;
        adata = waveDataTemp.shunshiF.shunshiFA;
        bdata = waveDataTemp.shunshiF.shunshiFB;
        cdata = waveDataTemp.shunshiF.shunshiFC;
        radata = waveDataTemp.RMSFWave.rmsFA;
        rbdata = waveDataTemp.RMSFWave.rmsFB;
        rcdata = waveDataTemp.RMSFWave.rmsFC;
    } else {
        max = waveDataTemp.instantS.max;
        min = waveDataTemp.instantS.min;
        adata = waveDataTemp.shunshiS.shunshiSA;
        bdata = waveDataTemp.shunshiS.shunshiSB;
        cdata = waveDataTemp.shunshiS.shunshiSC;
        radata = waveDataTemp.RMSSWave.rmsSA;
        rbdata = waveDataTemp.RMSSWave.rmsSB;
        rcdata = waveDataTemp.RMSSWave.rmsSC;
    }
    if (waveDataTemp.unit === "电压") {
        if (v === "one") {
            unit = 'kV';
        } else {
            unit = 'V';
        }
    } else {
        unit = 'A';
    }


    Highcharts.stockChart(waveId, {
            chart: {
                type: 'spline',
                events: {
                    load: function () {
                        if (show) {
                            this.series[1].show();
                        } else {
                            this.series[1].hide();
                        }
                    }
                }
            },
            //导出选中背景色
            navigation: {
                menuItemHoverStyle: {
                    background: color,
                    color: "#ffffff"
                },
            },
            tooltip: {
                borderColor: grey,
                backgroundColor: 'rgba(105,105,105,0.7)',
                style: {
                    color: 'white',
                    fontSize: "15px",
                    padding: 10,
                },
                formatter: function () {
                    var s = '<b>时刻：' + this.x + 'ms</b>';
                    $.each(this.points, function () {
                        s += '<br/>' + this.series.name + ': ' +
                            this.y.toFixed(2) + unit;
                    });
                    return s;
                },
                shared: true
            },
            rangeSelector: {
                enabled: false
            },
            //导航器各项属性设置
            navigator: {
                margin: 1,
                height: 15,
                xAxis: {
                    labels: {
                        formatter: function () {
                            return this.value;
                        }
                    }
                },
                series: [{
                    data: adata
                }, {
                    data: bdata
                }, {
                    data: cdata
                }]
            },
            scrollbar: {
                enabled: false
            },
            credits: false,
            colors: colors,
            xAxis: {
                title: {
                    text: 'ms',
                    enabled: true,
                    align: 'high'
                },
                labels: {
                    formatter: function () {
                        return this.value;
                    }
                }
            },
            yAxis: {
                title: {
                    align: 'high',
                    offset: 0,
                    text: unit,
                    rotation: 0,
                    y: -10
                },
                showLastLabel: true,
                opposite: false,
                max: max,
                min: min
            },
            legend: {
                align: 'left',
                verticalAlign: 'top',
                enabled: true,
                itemDistance: 5
            },
            series: [{
                marker: {
                    symbol: 'circle'
                },
                name: a,
                data: adata
            }, {
                marker: {
                    symbol: 'circle'
                },
                name: b,
                data: bdata,
                showInLegend: show
            }, {
                marker: {
                    symbol: 'circle'
                },
                name: c,
                data: cdata
            }
            ]
        }
    );


    Highcharts.stockChart(rmsId, {
        chart: {
            type: 'spline',
            events: {
                load: function () {
                    if (show) {
                        this.series[1].show();
                    } else {
                        this.series[1].hide();
                    }
                }
            }
        },
        //导出选中背景色
        navigation: {
            menuItemHoverStyle: {
                background: color,
                color: "#ffffff"
            },
        },
        tooltip: {
            borderColor: grey,
            backgroundColor: 'rgba(105,105,105,0.7)',
            style: {
                color: 'white',
                fontSize: "15px",
                padding: 10,
            },
            formatter: function () {
                var s = '<b>时刻：' + this.x + 'ms</b>';
                $.each(this.points, function () {
                    s += '<br/>' + this.series.name + ': ' +
                        this.y.toFixed(2) + unit;
                });
                return s;
            },
            shared: true
        },
        rangeSelector: {
            enabled: false,
        },
        //导航器各项属性设置
        navigator: {
            margin: 1,
            height: 15,
            xAxis: {
                labels: {
                    formatter: function () {
                        return this.value;
                    }
                }
            },
            series: [{
                data: radata
            }, {
                data: rbdata
            }, {
                data: rcdata
            }]

        },
        scrollbar: {
            enabled: false
        },
        credits: false,
        colors: colors,
        xAxis: {
            title: {
                text: 'ms',
                enabled: true,
                align: 'high'
            },
            labels: {
                formatter: function () {
                    return this.value;
                }
            }
        },
        yAxis: {
            title: {
                align: 'high',
                offset: 0,
                text: unit,
                rotation: 0,
                y: -10
            },
            showLastLabel: true,
            opposite: false
        },
        legend: {
            align: 'left',
            verticalAlign: 'top',
            enabled: true,
            itemDistance: 5
        },
        series: [{
            marker: {
                symbol: 'circle'
            },
            name: a,
            data: radata
        }, {
            marker: {
                symbol: 'circle'
            },
            name: b,
            data: rbdata,
            showInLegend: show
        }, {
            marker: {
                symbol: 'circle'
            },
            name: c,
            data: rcdata
        }
        ]
    });

}


$("#instance,#RMSD").on("mousemove", ".bx", function (e) {
    var sourceChartContainer = $(this);
    // 获取所有highcharts对象
    var sourceChart = sourceChartContainer.highcharts();
    // x坐标轴
    var sourceXAxis = sourceChart.xAxis[0];
    // 获取极值
    var extremes = sourceXAxis.getExtremes();
    // 遍历同节点元素 即highcharts元素块
    var targetChartContainerList = sourceChartContainer
        .siblings('.bx');

    // 遍历进行处理
    targetChartContainerList.each(function (num,
                                            targetChartContainerElement) {
        var targetChartContainer = $(targetChartContainerElement);
        // 获取highcharts对象
        var targetChart = targetChartContainer.highcharts();
        // 偏移值
        var sourceOffset = sourceChartContainer.offset();
        var targetOffset = targetChartContainer.offset();
        var targetE = {};

        targetE.pageX = e.pageX + targetOffset.left
            - sourceOffset.left;
        targetE.pageY = e.pageY + targetOffset.top
            - sourceOffset.top;
        var targetE = targetChartContainer
                .find('rect.highcharts-background')[0]
            || targetChartContainer
                .find('path.highcharts-tracker')[0];

        if (targetChart && targetChart.pointer) {
            var el = targetChart.pointer.normalize(e); // Find
            if (targetChart.series.length === 0) {
                if (targetChart.series[num] !== undefined) {
                    var point = targetChart.series[1].searchPoint(
                        el, true); // Get
                    if (point) {
                        point.onMouseOver(); // Show the hover
                        // marker
                    }
                }
            } else {
                if (targetChart.series[num] !== undefined) {
                    var point = targetChart.series[1].searchPoint(
                        el, true); // Get
                    if (point) {
                        point.onMouseOver(); // Show the hover
                        // marker
                    }
                }
            }
        }
    });
});


//获取横向一个波形的全部数据，包括瞬时波形的一次、二次值，RMS波形的一次、二次值，如果是电压的话，还需要计算出对应的最大最小值
function fliteWaveData(wp, step) {
    var shunData = wp.sunData;
    var rmsData = wp.rmsData;
    var pt = Number(wp.pt) / 1000;
    var ct = Number(wp.ct);

    var titleList = wp.tmpWaveTitle;
    //首先判断当前绘制的是电压还是电流
    var xishu = pt;//默认电压，二次值转一次值乘以pt
    //三相名称
    var aTitle = "", bTitle = "", cTitle = "", unit = "电压";
    //RMS最小值横、纵坐标、相别名称
    var rmsvFirstX = 0, rmsvFirstY = 0, rmsvSecondX = 0, rmsvSecondY = 0, firstZhou = 'a', secondeZhou = 'a';
    //每个波形对应的最大最小值
    var ifmax, ifmin, ismax, ismin, rfmax, rfmin, rsmax, rsmin;
    var shunshiFA = [], shunshiFB = [], shunshiFC = [], shunshiSA = [], shunshiSB = [], shunshiSC = [], rmsFA = [],
        rsmFB = [], rmsFC = [], rmsSA = [], rsmSB = [], rmsSC = [];
    //给瞬时赋个初始值
    if (shunData.length > 0) {
        if (titleList[iPhasic * step + 1].substring(0, 1) !== "U") {
            xishu = ct;
            unit = "电流";
        }
        for (var i = 1; i <= iPhasic; i++) {
            switch (i) {
                case 1:
                    aTitle = titleList[iPhasic * step + i].substring(1);
                    break;
                case 2:
                    bTitle = titleList[iPhasic * step + i].substring(1);
                    break;
                case 3:
                    cTitle = titleList[iPhasic * step + i].substring(1);
                    break;
            }
        }
        if (shunData[0][iPhasic * step + 1] !== undefined) {
            ifmax = shunData[0][iPhasic * step + 1] * xishu;
            ifmin = shunData[0][iPhasic * step + 1] * xishu;
            ismax = shunData[0][iPhasic * step + 1];
            ismin = shunData[0][iPhasic * step + 1];
        }

        if (rmsData[0][iPhasic * step + 1] !== undefined) {
            rfmax = rmsData[0][iPhasic * step + 1] * xishu;
            rfmin = rmsData[0][iPhasic * step + 1] * xishu;
            rmsvFirstY = rmsData[0][iPhasic * step + 1] * xishu;
            rmsvFirstX = rmsData[0][0];
            rsmax = rmsData[0][iPhasic * step + 1];
            rsmin = rmsData[0][iPhasic * step + 1];
            rmsvSecondY = rmsData[0][iPhasic * step + 1];
            rmsvSecondX = rmsData[0][0];
        }
        //瞬时值--同时还需要获取出瞬时值的一次、二次的最大最小值
        for (var shun = 0; shun < shunData.length; shun++) {
            if (shunData[shun][iPhasic * step + 1] === undefined) {
                break;
            }
            //根据波形数据相的数量来筛选数据
            switch (iPhasic) {
                case 1:
                    var shunFirstA = shunData[shun][iPhasic * step + 1] * xishu;
                    shunshiFA.push([shunData[shun][0], shunFirstA]);
                    ifmax = ifmax > shunFirstA ? ifmax : shunFirstA;
                    ifmin = ifmin < shunFirstA ? ifmin : shunFirstA;

                    var shunSecondA = shunData[shun][iPhasic * step + 1];
                    shunshiSA.push([shunData[shun][0], shunSecondA]);
                    ismax = ismax > shunSecondA ? ismax : shunSecondA;
                    ismin = ismin < shunSecondA ? ismin : shunSecondA;
                    break;
                case 2:
                    var shunFirstA = shunData[shun][iPhasic * step + 1] * xishu;
                    var shunFirstB = shunData[shun][iPhasic * step + 2] * xishu;
                    shunshiFA.push([shunData[shun][0], shunFirstA]);
                    shunshiFB.push([shunData[shun][0], shunFirstB]);
                    ifmax = getMaxTwo(ifmax, shunFirstA, shunFirstB);
                    ifmin = getMinOpen(ifmin, shunFirstA, shunFirstB);

                    var shunSecondA = shunData[shun][iPhasic * step + 1];
                    var shunSecondB = shunData[shun][iPhasic * step + 2];
                    shunshiSA.push([shunData[shun][0], shunSecondA]);
                    shunshiSB.push([shunData[shun][0], shunSecondB]);
                    ismax = getMaxTwo(ismax, shunSecondA, shunSecondB);
                    ismin = getMinOpen(ismin, shunSecondA, shunSecondB);
                    break;
                case 3:
                    var shunFirstA = shunData[shun][iPhasic * step + 1] * xishu;
                    var shunFirstB = shunData[shun][iPhasic * step + 2] * xishu;
                    var shunFirstC = shunData[shun][iPhasic * step + 3] * xishu;
                    shunshiFA.push([shunData[shun][0], shunFirstA]);
                    shunshiFB.push([shunData[shun][0], shunFirstB]);
                    shunshiFC.push([shunData[shun][0], shunFirstC]);
                    ifmax = getMax(ifmax, shunFirstA, shunFirstB, shunFirstC);
                    //如果接线方式是V
                    if (isOpen) {
                        ifmin = getMinOpen(ifmin, shunFirstA, shunFirstC);
                    } else {
                        ifmin = getMin(ifmin, shunFirstA, shunFirstB, shunFirstC);
                    }

                    var shunSecondA = shunData[shun][iPhasic * step + 1];
                    var shunSecondB = shunData[shun][iPhasic * step + 2];
                    var shunSecondC = shunData[shun][iPhasic * step + 3];
                    shunshiSA.push([shunData[shun][0], shunSecondA]);
                    shunshiSB.push([shunData[shun][0], shunSecondB]);
                    shunshiSC.push([shunData[shun][0], shunSecondC]);
                    ismax = getMax(ismax, shunSecondA, shunSecondB, shunSecondC);
                    //如果接线方式是V
                    if (isOpen) {
                        ismin = getMinOpen(ismin, shunSecondA, shunSecondC);
                    } else {
                        ismin = getMin(ismin, shunSecondA, shunSecondB, shunSecondC);
                    }
                    break;
            }
        }

        //RMS值--同时还需要获取出RMS值的一次、二次的最大最小值
        for (var rms = 0; rms < rmsData.length; rms++) {
            if (rmsData[rms][iPhasic * step + 1] === undefined) {
                break;
            }
            //根据波形数据相的数量来筛选数据
            switch (iPhasic) {
                case 1:
                    var rmsFirstA = rmsData[rms][iPhasic * step + 1] * xishu;
                    rmsFA.push([rmsData[rms][0], rmsFirstA]);
                    rfmax = rfmax > rmsFirstA ? rfmax : rmsFirstA;
                    rfmin = rfmin < rmsFirstA ? rfmin : rmsFirstA;
                    if (rfmin < rmsvFirstY) {
                        rmsvFirstY = rfmin;
                        firstZhou = "a";
                        rmsvFirstX = rmsData[rms][0];
                    }

                    var rmsSecondA = rmsData[rms][iPhasic * step + 1];
                    rmsSA.push([rmsData[rms][0], rmsSecondA]);
                    rsmax = rsmax > rmsSecondA ? rsmax : rmsSecondA;
                    rsmin = rsmin < rmsSecondA ? rsmin : rmsSecondA;
                    if (rsmin < rmsvSecondY) {
                        rmsvSecondY = rsmin;
                        secondeZhou = "a";
                        rmsvSecondX = rmsData[rms][0];
                    }
                    break;
                case 2:
                    var rmsFirstA = rmsData[rms][iPhasic * step + 1] * xishu;
                    var rmsFirstB = rmsData[rms][iPhasic * step + 2] * xishu;
                    rmsFA.push([rmsData[rms][0], rmsFirstA]);
                    rsmFB.push([rmsData[rms][0], rmsFirstB]);
                    rfmax = getMaxTwo(rfmax, rmsFirstA, rmsFirstB);
                    rfmin = getMinOpen(rfmin, rmsFirstA, rmsFirstB);
                    if (rfmin < rmsvFirstY) {
                        rmsvFirstY = rfmin;
                        if (rfmin === rmsFirstA) {
                            firstZhou = "a";
                        } else if (rfmin === rmsFirstB) {
                            firstZhou = "b";
                        }
                        rmsvFirstX = rmsData[rms][0];
                    }


                    var rmsSecondA = rmsData[rms][iPhasic * step + 1];
                    var rmsSecondB = rmsData[rms][iPhasic * step + 2];
                    rmsSA.push([rmsData[rms][0], rmsSecondA]);
                    rsmSB.push([rmsData[rms][0], rmsSecondB]);
                    rsmax = getMaxTwo(rsmax, rmsSecondA, rmsSecondB);
                    rsmin = getMinOpen(rsmin, rmsSecondA, rmsSecondB);
                    if (rsmin < rmsvSecondY) {
                        rmsvSecondY = rsmin;
                        if (rsmin === rmsSecondA) {
                            secondeZhou = "a";
                        } else if (rsmin === rmsSecondB) {
                            secondeZhou = "b";
                        }
                        rmsvSecondX = rmsData[rms][0];
                    }
                    break;
                case 3:
                    var rmsFirstA = rmsData[rms][iPhasic * step + 1] * xishu;
                    var rmsFirstB = rmsData[rms][iPhasic * step + 2] * xishu;
                    var rmsFirstC = rmsData[rms][iPhasic * step + 3] * xishu;
                    rmsFA.push([rmsData[rms][0], rmsFirstA]);
                    rsmFB.push([rmsData[rms][0], rmsFirstB]);
                    rmsFC.push([rmsData[rms][0], rmsFirstC]);
                    rfmax = getMax(rfmax, rmsFirstA, rmsFirstB, rmsFirstC);
                    if (isOpen) {
                        rfmin = getMinOpen(rfmin, rmsFirstA, rmsFirstC);
                    } else {
                        rfmin = getMin(rfmin, rmsFirstA, rmsFirstB, rmsFirstC);
                    }
                    if (rfmin < rmsvFirstY) {
                        rmsvFirstY = rfmin;
                        if (rfmin === rmsFirstA) {
                            firstZhou = "a";
                        } else if (rfmin === rmsFirstB) {
                            firstZhou = "b";
                        } else {
                            firstZhou = "c";
                        }
                        rmsvFirstX = rmsData[rms][0];
                    }

                    var rmsSecondA = rmsData[rms][iPhasic * step + 1];
                    var rmsSecondB = rmsData[rms][iPhasic * step + 2];
                    var rmsSecondC = rmsData[rms][iPhasic * step + 3];
                    rmsSA.push([rmsData[rms][0], rmsSecondA]);
                    rsmSB.push([rmsData[rms][0], rmsSecondB]);
                    rmsSC.push([rmsData[rms][0], rmsSecondC]);
                    rsmax = getMax(rsmax, rmsSecondA, rmsSecondB, rmsSecondC);
                    if (isOpen) {
                        rsmin = getMinOpen(rsmin, rmsSecondA, rmsSecondC);
                    } else {
                        rsmin = getMin(rsmin, rmsSecondA, rmsSecondB, rmsSecondC);
                    }
                    if (rsmin < rmsvSecondY) {
                        rmsvSecondY = rsmin;
                        if (rsmin === rmsSecondA) {
                            secondeZhou = "a";
                        } else if (rsmin === rmsSecondB) {
                            secondeZhou = "b";
                        } else {
                            secondeZhou = "c";
                        }
                        rmsvSecondX = rmsData[rms][0];
                    }
                    break;
            }
        }
    }
    //瞬时一次值最大最小值
    var instantF = {};
    var instantS = {};
    var RMSF = {};
    var RMSS = {};
    var RMSFMinDetail = {};
    var RMSSMinDetail = {};
    var shunshiF = {};
    var shunshiS = {};
    var RMSFWave = {};
    var RMSSWave = {};
    var title = {};

    //三相名称
    title.aTitle = aTitle;
    title.bTitle = bTitle;
    title.cTitle = cTitle;

    //电流还是电压
    title.unit = unit;

    //每个波形对应的三相数据
    shunshiFA = shunshiFA.slice(0);
    shunshiFB = shunshiFB.slice(0);
    shunshiFC = shunshiFC.slice(0);
    rmsFA = rmsFA.slice(0);
    rsmFB = rsmFB.slice(0);
    rmsFC = rmsFC.slice(0);

    shunshiSA = shunshiSA.slice(0);
    shunshiSB = shunshiSB.slice(0);
    shunshiSC = shunshiSC.slice(0);
    rmsSA = rmsSA.slice(0);
    rsmSB = rsmSB.slice(0);
    rmsSC = rmsSC.slice(0);

    shunshiF.shunshiFA = shunshiFA;
    shunshiF.shunshiFB = shunshiFB;
    shunshiF.shunshiFC = shunshiFC;

    shunshiS.shunshiSA = shunshiSA;
    shunshiS.shunshiSB = shunshiSB;
    shunshiS.shunshiSC = shunshiSC;

    RMSFWave.rmsFA = rmsFA;
    RMSFWave.rmsFB = rsmFB;
    RMSFWave.rmsFC = rmsFC;

    RMSSWave.rmsSA = rmsSA;
    RMSSWave.rmsSB = rsmSB;
    RMSSWave.rmsSC = rmsSC;

    //赋值对应的最大最小值
    instantF.max = ifmax;
    instantF.min = ifmin;
    instantS.max = ismax;
    instantS.min = ismin;
    if (step === 0) {
        instantFAn.max = ifmax;
        instantFAn.min = ifmin;
        instantSAn.max = ismax;
        instantSAn.min = ismin;
    }
    RMSF.max = rfmax;
    RMSF.min = rfmin;
    RMSS.max = rsmax;
    RMSS.min = rsmin;
    //精确到小数位2位
    rmsvFirstY = rmsvFirstY.toFixed(5);
    rmsvSecondY = rmsvSecondY.toFixed(5);
    RMSFMinDetail.rmsvFirstX = rmsvFirstX;
    RMSFMinDetail.rmsvFirstY = rmsvFirstY;
    RMSFMinDetail.firstZhou = firstZhou;

    RMSSMinDetail.rmsvSecondX = rmsvSecondX;
    RMSSMinDetail.rmsvSecondY = rmsvSecondY;
    RMSSMinDetail.secondeZhou = secondeZhou;
    return new waveData(instantF, instantS, RMSF, RMSS, RMSFMinDetail, RMSSMinDetail, shunshiF, shunshiS, RMSFWave, RMSSWave, title, unit);
}


//根据多个值的比较返回其中最大值
function getMax(temp, tempA, tempB, tempC) {
    temp = temp > tempA ? temp : tempA;
    temp = temp > tempB ? temp : tempB;
    temp = temp > tempC ? temp : tempC;
    return temp;
}

//根据多个值的比较返回其中最大值
function getMaxTwo(temp, tempA, tempB) {
    temp = temp > tempA ? temp : tempA;
    temp = temp > tempB ? temp : tempB;
    return temp;
}

//根据多个值的比较返回其中最小值
function getMin(temp, tempA, tempB, tempC) {
    temp = temp < tempA ? temp : tempA;
    temp = temp < tempB ? temp : tempB;
    temp = temp < tempC ? temp : tempC;
    return temp;
}

function getMinOpen(temp, tempA, tempB) {
    temp = temp < tempA ? temp : tempA;
    temp = temp < tempB ? temp : tempB;
    return temp;
}


/**
 * 点击高级分析
 * */
$('#analysis').click(function () {
    /**
     * 点击后弹出等待处理提示
     */
    var $alertMsg = layer.msg('高级分析<Br\>&nbsp&nbsp&nbsp正在处理....', {
        icon: 16,
        shade: 0.01,
        time: 0
    });
    parent.layer.open({
        type: 2,
        title: '高级分析',
        maxmin: false,
        shadeClose: true, // 点击遮罩关闭层
        area: ['100%', '100%'],
        content: 'analysis?maxy=' + instantSAn.max + '&miny=' + instantSAn.min + '&time=' + time + '&pt=' + ptpass,
        scrollbar: false,
        success: function (layero, index) {
            $(layero).find("iframe")[0].contentWindow.analysis(evtindex);
            if ($alertMsg !== -1) {
                layer.close($alertMsg);
            }
        }
    });
});
