本文写的目的是备份一下当时写的逻辑。该逻辑业务大致是SPC中的八大判异标准。
八大判异标准详情请看:
https://wenku.baidu.com/view/9430b38ad05abe23482fb4daa58da0116d171f53.html[SPC中八大判异标准]
实现后样式大概为:
选中那种判异标准则在上面图表显示
该功能是在VUE中实现,图表为echar,逻辑写在js中
以下为逻辑代码(格式有点问题可以复制到编辑器中嘻嘻):
/**
*

  • @param {(measuringValue:y轴上的,productId:x轴上的)} SPCList

  • @param {平均值} avg

  • @param {一倍标准差} sd

  • @param {动态判异标准的值} abnormalSet

  • @param {是否选中前四个判异标准} checkboxs1

  • @param {是否选中后四个判异标准} checkboxs2
    */
    export function alarm(SPCList, avg, sd, abnormalSet, checkboxs1, checkboxs2) {
    const one = abnormalSet.one
    const two = abnormalSet.two
    const three = abnormalSet.three
    const four = abnormalSet.four
    const five1 = parseInt(abnormalSet.five.split(’/’)[0])
    const five2 = parseInt(abnormalSet.five.split(’/’)[1])
    const six1 = parseInt(abnormalSet.six.split(’/’)[0])
    const six2 = parseInt(abnormalSet.six.split(’/’)[1])
    const seven = abnormalSet.seven
    const eight = abnormalSet.eight
    const markPointList = []
    const a = []// 一个点远离中心线3个标准差
    let aa = 0
    let b = []// 连续7个点位于中心线一侧(box)
    let bb = 0// 连续7个点位于中心线一侧的趋势 >1:中心线上方。<1:中心线下方
    const bbb = []// 连续7个点位于中心线一侧
    let c = []// 连续6个点上升或下降(box)
    let cc = 0// 连续6个点上升或下降的趋势 >1:上升。<1:下降
    const ccc = []// 连续6个点上升或下降
    let d = []// 连续14个点交替上下变化(box)
    let dd = 0
    const ddd = []
    let e = []// 2/3的点距离中心线的距离超过2个标准差(同一侧):收集上侧的值
    const ee = []// 2/3的点距离中心线的距离超过2个标准差(同一侧):收集下侧的值
    let f = []// 4/5的点距离中心线的距离超过一个标准差(同一侧):收集上侧的值
    const ff = []// 4/5的点距离中心线的距离超过一个标准差(同一侧):收集下侧的值
    let g = []// 连续15个点排列在中心线1个标准差范围内(任一侧)(box)
    let gg = 0// 连续15个点排列在中心线1个标准差范围内(任一侧)
    const ggg = []// 连续15个点排列在中心线1个标准差范围内(任一侧)
    let h = []// 连续8个点距中心线的距离大于1个标准差(任一侧)
    let hh = 0// 连续8个点距中心线的距离大于1个标准差(任一侧)
    const hhh = []// 连续8个点距中心线的距离大于1个标准差(任一侧)
    for (let i = 0; i < SPCList.length; i++) {
    // ----------第六种---------
    // 4/5的点距离中心线的距离超过一个标准差(同一侧)
    if (checkboxs2.includes(‘6’)) {
    if (i >= six2 - 1) {
    let fff = 0
    let ffff = 0
    for (let j = 0; j < six2; j++) {
    f.push({ yAxis: SPCList[i - j].measuringValue, xAxis: SPCList[i - j].productId })
    if (SPCList[i - j].measuringValue > avg + sd) {
    fff = fff + 1
    }
    if (SPCList[i - j].measuringValue < avg - sd) {
    ffff = ffff + 1
    }
    }
    if (fff >= six1) {
    ff.push(f)
    }
    if (ffff >= six1) {
    ff.push(f)
    }
    f = []
    }
    }
    // -------------------第一种----------------------
    // 一个点远离中心线3个标准差
    if (checkboxs1.includes(‘1’) && sd !== undefined) {
    if (SPCList[i].measuringValue >= avg + 3 * sd || SPCList[i].measuringValue <= avg - 3 * sd) {
    a.push({ yAxis: SPCList[i].measuringValue, xAxis: SPCList[i].productId })
    aa = aa + 1
    }
    }
    // -------------------第二种----------------------
    // 连续7个点位于中心线一侧(等于平均线不算)
    if (checkboxs1.includes(‘2’)) {
    if (i === 0) {
    bb = SPCList[i].measuringValue > avg ? 1 : -1 // 1:中心线上方。-1:中心线下方
    } else {
    if (SPCList[i].measuringValue > avg && bb > 0) {
    if (b.length === 0 && i !== 0) { // 解决每个异常组第一个值为空
    b.push({ yAxis: SPCList[i - 1].measuringValue, xAxis: SPCList[i - 1].productId })
    }
    b.push({ yAxis: SPCList[i].measuringValue, xAxis: SPCList[i].productId })
    bb = bb + 1
    } else if (SPCList[i].measuringValue < avg && bb < 0) {
    if (b.length === 0 && i !== 0) { // 解决每个异常组第一个值为空
    b.push({ yAxis: SPCList[i - 1].measuringValue, xAxis: SPCList[i - 1].productId })
    }
    b.push({ yAxis: SPCList[i].measuringValue, xAxis: SPCList[i].productId })
    bb = bb - 1
    } else {
    if (bb >= two) { // 符合报警条件
    bbb.push(b)
    } else if (bb <= -two) {
    bbb.push(b)
    }
    if (SPCList[i].measuringValue > avg) {
    bb = 1
    } else if (SPCList[i].measuringValue < avg) {
    bb = -1
    } else if (SPCList[i].measuringValue === avg) {
    bb = 0
    }
    b = []
    }
    if (i === SPCList.length - 1) { // 最后一条
    if (bb >= two) { // 符合报警条件
    bbb.push(b)
    } else if (bb <= -two) {
    bbb.push(b)
    }
    b = []
    }
    }
    }
    // -------------------第三种----------------------
    // 连续6个点上升或下降
    if (checkboxs1.includes(‘3’)) {
    if (i === 0) {
    cc = 0 // >1:上升。<1:下降
    c.push({ yAxis: SPCList[i].measuringValue, xAxis: SPCList[i].productId })
    } else {
    if (SPCList[i].measuringValue - SPCList[i - 1].measuringValue < 0 && cc <= 0) { // 比上个数小且之前呈下降趋势
    cc = cc - 1
    c.push({ yAxis: SPCList[i].measuringValue, xAxis: SPCList[i].productId })
    } else if (SPCList[i].measuringValue - SPCList[i - 1].measuringValue < 0 && cc > 0) { // 比上个数小且之前呈上升趋势
    cc = -1
    c = []
    c.push({ yAxis: SPCList[i - 1].measuringValue, xAxis: SPCList[i - 1].productId })// 把这一组第一个数也加上
    c.push({ yAxis: SPCList[i].measuringValue, xAxis: SPCList[i].productId })
    } else if (SPCList[i].measuringValue - SPCList[i - 1].measuringValue > 0 && cc < 0) { // 比上个数大且之前呈下降趋势
    cc = 1
    c = []
    c.push({ yAxis: SPCList[i - 1].measuringValue, xAxis: SPCList[i - 1].productId })// 把这一组第一个数也加上
    c.push({ yAxis: SPCList[i].measuringValue, xAxis: SPCList[i].productId })
    } else if (SPCList[i].measuringValue - SPCList[i - 1].measuringValue > 0 && cc >= 0) { // 比上个数大且之前呈上升趋势
    cc = cc + 1
    c.push({ yAxis: SPCList[i].measuringValue, xAxis: SPCList[i].productId })
    } else {
    cc = 0
    c = []
    c.push({ yAxis: SPCList[i].measuringValue, xAxis: SPCList[i].productId })
    }
    if (c.length > three) {
    if (c.length > three + 1) { // 如果持续成上升或下降趋势,那么删除前一个元素防止重复
    ccc.pop()
    }
    ccc.push©
    }
    }
    }
    // ---------------第四种-----------
    // 连续14个点交替上下变化
    if (checkboxs1.includes(‘4’)) {
    if (i <= 1) {
    dd = 1
    d.push({ yAxis: SPCList[i].measuringValue, xAxis: SPCList[i].productId })
    } else {
    if (i === SPCList.length - 1) { // 最后一条
    if (dd >= four) { // 符合报警
    ddd.push(d)
    }
    } else {
    if ((SPCList[i].measuringValue - SPCList[i - 1].measuringValue) * (SPCList[i].measuringValue - SPCList[i + 1].measuringValue) > 0) { // 交替开始
    dd = dd + 1
    if (d.length === 0) {
    d.push({ yAxis: SPCList[i - 1].measuringValue, xAxis: SPCList[i - 1].productId })
    }
    d.push({ yAxis: SPCList[i].measuringValue, xAxis: SPCList[i].productId })
    } else {
    if (dd >= four) { // 符合报警
    ddd.push(d)
    }
    dd = 1
    d = []
    }
    }
    }
    }

    // ----------第五种---------
    // 2/3的点距离中心线的距离超过2个标准差(同一侧)
    if (checkboxs2.includes(‘5’)) {
    // (2/3)从第三组开始计算
    if (i >= five2 - 1) {
    let eee = 0
    let eeee = 0
    for (let j = 0; j < five2; j++) {
    e.push({ yAxis: SPCList[i - j].measuringValue, xAxis: SPCList[i - j].productId })
    if (SPCList[i - j].measuringValue > avg + 2 * sd) {
    eee = eee + 1
    }
    if (SPCList[i - j].measuringValue < avg - 2 * sd) {
    eeee = eeee + 1
    }
    }
    if (eee >= five1) {
    ee.push(e)
    }
    if (eeee >= five1) {
    ee.push(e)
    }
    e = []
    }
    }

    // ----------第七种---------
    // 连续15个点排列在中心线1个标准差范围内(任一侧)
    if (checkboxs2.includes(‘7’)) {
    if (SPCList[i].measuringValue > avg - sd && SPCList[i].measuringValue < avg + sd) {
    g.push({ yAxis: SPCList[i].measuringValue, xAxis: SPCList[i].productId })
    gg = gg + 1
    } else {
    if (gg >= seven) {
    ggg.push(g)
    }
    g = []
    gg = 0
    }
    if (i === SPCList.length - 1) { // 最后一条
    if (gg >= seven) {
    ggg.push(g)
    }
    }
    }

    // ----------第八种---------
    // 连续8个点距中心线的距离大于1个标准差(任一侧)
    if (checkboxs2.includes(‘8’)) {
    if (SPCList[i].measuringValue > avg + sd || SPCList[i].measuringValue < avg - sd) {
    h.push({ yAxis: SPCList[i].measuringValue, xAxis: SPCList[i].productId })
    hh = hh + 1
    } else {
    if (hh >= eight) {
    hhh.push(h)
    }
    h = []
    hh = 0
    }
    if (i === SPCList.length - 1) { // 最后一条
    if (hh >= eight) {
    hhh.push(h)
    }
    }
    }
    }
    // 把第一种报警的点表示出来
    if (checkboxs1.includes(‘1’)) {
    if (aa >= one) {
    for (let i = 0; i < a.length; i++) {
    markPointList.push({
    yAxis: a[i].yAxis,
    xAxis: a[i].xAxis,
    symbolSize: 12,
    symbol: ‘diamond’,
    itemStyle: {
    color: ‘red’
    }
    })
    }
    }
    }

// 把第二种报警的点表示出来
if (checkboxs1.includes(‘2’)) {
for (let i = 0; i < bbb.length; i++) {
for (let ii = 0; ii < bbb[i].length; ii++) {
markPointList.push({
yAxis: bbb[i][ii].yAxis,
xAxis: bbb[i][ii].xAxis,
symbolSize: 12,
symbol: ‘diamond’,
itemStyle: {
color: ‘red’
}
})
}
}
}

// 把第三种报警的点表示出来

if (checkboxs1.includes(‘3’)) {
for (let i = 0; i < ccc.length; i++) {
for (let ii = 0; ii < ccc[i].length; ii++) {
markPointList.push({
yAxis: ccc[i][ii].yAxis,
xAxis: ccc[i][ii].xAxis,
symbolSize: 12,
symbol: ‘diamond’,
itemStyle: {
color: ‘red’
}
})
}
}
}

// 把第四种报警的点表示出来
if (checkboxs1.includes(‘4’)) {
for (let i = 0; i < ddd.length; i++) {
for (let ii = 0; ii < ddd[i].length; ii++) {
markPointList.push({
yAxis: ddd[i][ii].yAxis,
xAxis: ddd[i][ii].xAxis,
symbolSize: 12,
symbol: ‘diamond’,
itemStyle: {
color: ‘red’
}
})
}
}
}

// 把第五种报警的点表示出来

if (checkboxs2.includes(‘5’)) {
for (let i = 0; i < ee.length; i++) {
for (let ii = 0; ii < ee[i].length; ii++) {
markPointList.push({
yAxis: ee[i][ii].yAxis,
xAxis: ee[i][ii].xAxis,
symbolSize: 12,
symbol: ‘diamond’,
itemStyle: {
color: ‘red’
}
})
}
}
}

// 把第六种报警的点表示出来
if (checkboxs2.includes(‘6’)) {
for (let i = 0; i < ff.length; i++) {
for (let ii = 0; ii < ff[i].length; ii++) {
markPointList.push({
yAxis: ff[i][ii].yAxis,
xAxis: ff[i][ii].xAxis,
symbolSize: 12,
symbol: ‘diamond’,
itemStyle: {
color: ‘red’
}
})
}
}
}

// 把第七种报警的点表示出来
if (checkboxs2.includes(‘7’)) {
for (let i = 0; i < ggg.length; i++) {
for (let ii = 0; ii < ggg[i].length; ii++) {
markPointList.push({
yAxis: ggg[i][ii].yAxis,
xAxis: ggg[i][ii].xAxis,
symbolSize: 12,
symbol: ‘diamond’,
itemStyle: {
color: ‘red’
}
})
}
}
}

// 把第八种报警的点表示出来
if (checkboxs2.includes(‘8’)) {
for (let i = 0; i < hhh.length; i++) {
for (let ii = 0; ii < hhh[i].length; ii++) {
markPointList.push({
yAxis: hhh[i][ii].yAxis,
xAxis: hhh[i][ii].xAxis,
symbolSize: 12,
symbol: ‘diamond’,
itemStyle: {
color: ‘red’
}
})
}
}
}
var map = {}
map.markPointList = markPointList
return map
}

该逻辑将有异常的点取出来然后在echart中渲染出来。

Logo

前往低代码交流专区

更多推荐