最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue封裝的標(biāo)準(zhǔn)漏斗圖教程

 更新時(shí)間:2025年02月12日 15:37:17   作者:努力奮斗小白  
文章展示了基于Vue封裝的標(biāo)準(zhǔn)漏斗圖的代碼,包括權(quán)重算法、滾輪監(jiān)聽和點(diǎn)擊示例功能,旨在提供實(shí)用的圖表展示技巧

Vue封裝的標(biāo)準(zhǔn)漏斗圖

代碼展示

基于Vue封裝的標(biāo)準(zhǔn)漏斗圖,增加權(quán)重算法,避免數(shù)值很小時(shí)圖表顯示過小,監(jiān)聽滾輪高度,防止彈窗飛走,點(diǎn)擊示例動(dòng)態(tài)顯示對(duì)應(yīng)的梯形圖表塊。

/**
 * 權(quán)重
 */
export function qz(arr) {
  // 計(jì)算數(shù)列的總和
  const total = arr.reduce((sum, val) => sum + val, 0)
  // 如果總和為零,則無法平衡,直接返回原數(shù)組
  if (total === 0) {
    return arr
  }
  function nonlinearScale(val) {
    return Math.sqrt(val) // 對(duì)數(shù)值進(jìn)行平方根變換
  }
  // 平方根為了讓數(shù)小點(diǎn),一般降級(jí)都采用平方根處理
  const scaledArr = arr.map(val => nonlinearScale(val))
  // 計(jì)算縮放后數(shù)列的總和
  const scaledTotal = scaledArr.reduce((sum, val) => sum + val, 0)
  // 將縮放后的數(shù)列歸一到100%,超過100就越界了
  let balancedArr = scaledArr.map(val => (val / scaledTotal) * 100)
  // 由于浮點(diǎn)數(shù)的精度問題,總和可能不是精確的100%,問題不大
  const error = 100 - balancedArr.reduce((sum, val) => sum + val, 0)
  const nonZeroCount = balancedArr.filter(val => val > 0).length
  if (nonZeroCount > 0) {
    const adjustment = error / nonZeroCount
    balancedArr = balancedArr.map(val => val + (val > 0 ? adjustment : 0))
  }
  // 四舍五入到小數(shù)點(diǎn)后兩
  balancedArr = balancedArr.map(val => Math.round(val * 100) / 100)
  return balancedArr
}
  <LdChart :chart-data="charDataArray" :flag-type="flagType" />

// 接口返回
this.charDataArray = []
          this.charDataTwoArray = []
          this.num1 = 0
          this.num2 = 0
          if (res.data && res.data.length > 0) {
            this.loading = false
            // 數(shù)據(jù)都是固定的五個(gè)!!!1
            const obj1 = [
              { height: '20%', top: '0%', name: '名稱1', value: '', type: '10%', unit: '萬元', color: '#7aa5fa', checkItem: false, itemColor: '#7aa5fa', fontColor: '#303313' },
              { height: '20%', top: '20%', name: '名稱2', value: '', type: '30%', unit: '萬元', color: '#7ae0b8', checkItem: false, itemColor: '#7ae0b8', fontColor: '#303313' },
              { height: '20%', top: '40%', name: '名稱3', value: '', type: '50%', unit: '萬元', color: '#f6e0b4', checkItem: false, itemColor: '#f6e0b4', fontColor: '#303313' },
              { height: '20%', top: '60%', name: '名稱4', value: '', type: '80%', unit: '萬元', color: '#d9d9d9', checkItem: false, itemColor: '#d9d9d9', fontColor: '#303313' },
              { height: '20%', top: '80%', name: '名稱5', value: '', type: '100%', unit: '萬元', color: '#f5aa8a', checkItem: false, itemColor: '#f5aa8a', fontColor: '#303313' }
            ]
            const obj2 = [
              { height: '20%', top: '0%', name: '名稱1', value: '', type: '10%', unit: '個(gè)', color: '#7aa5fa', checkItem: false, itemColor: '#7aa5fa', fontColor: '#303313' },
              { height: '20%', top: '20%', name: '名稱2', value: '', type: '30%', unit: '個(gè)', color: '#7ae0b8', checkItem: false, itemColor: '#7ae0b8', fontColor: '#303313' },
              { height: '20%', top: '40%', name: '名稱3', value: '', type: '50%', unit: '個(gè)', color: '#f6e0b4', checkItem: false, itemColor: '#f6e0b4', fontColor: '#303313' },
              { height: '20%', top: '60%', name: '名稱4', value: '', type: '80%', unit: '個(gè)', color: '#d9d9d9', checkItem: false, itemColor: '#d9d9d9', fontColor: '#303313' },
              { height: '20%', top: '80%', name: '名稱5', value: '', type: '100%', unit: '個(gè)', color: '#f5aa8a', checkItem: false, itemColor: '#f5aa8a', fontColor: '#303313' }
            ]
            const sumOne = res.data[0].reduce((sum, current) => sum + Number(current.sales), 0)
            const sumTwo = res.data[1].reduce((sum, current) => sum + Number(current.sales), 0)
            this.num1 = sumOne.toFixed(2)
            this.num2 = sumTwo
            obj1.forEach(item => {
              res.data[0].forEach(it => {
                if (item.type === it.winOrderRate) {
                  item.value = it.sales
                  // 計(jì)算比例
                  let bl = 0
                  sumOne === 0 ? bl = 0 : bl = Math.floor(Number(it.sales) / sumOne * 100)
                  Number(it.sales) > 0 && bl === 0 ? bl = 1 : ''
                  // 設(shè)置合適的比例1
                  item.height = bl + '%'
                }
              })
            })
            obj2.forEach(item => {
              res.data[1].forEach(it => {
                if (item.type === it.winOrderRate) {
                  item.value = it.sales
                  // 計(jì)算比例
                  let bl = 0
                  sumTwo === 0 ? bl = 0 : bl = Math.floor(Number(it.sales) / sumTwo * 100)
                  Number(it.sales) > 0 && bl === 0 ? bl = 1 : ''
                  // 設(shè)置合適的比例
                  item.height = bl + '%'
                }
              })
            })
            const qzOne = qz(obj1.map(it => Number(it.height.split('%')[0])))
            const qzTwo = qz(obj2.map(it => Number(it.height.split('%')[0])))
            obj1.forEach((it, index) => {
              it.height = qzOne[index] + '%'
            })
            obj2.forEach((it, index) => {
              it.height = qzTwo[index] + '%'
            })
            // 排除0的,
            const objNew1 = obj1.filter(item => item.value !== 0 && item.value !== '0')
            const objNew2 = obj2.filter(item => item.value !== 0 && item.value !== '0')
            // 第一個(gè)是 0, 第二個(gè)是 第一個(gè)的高度, 第三個(gè)是 第一個(gè)+ 第二個(gè) 第四個(gè)是 1+2+3
            const result = []
            const result1 = []
            let sum = 0
            let sum1 = 0
            for (let i = 0; i < objNew1.length; i++) {
              sum += Number(objNew1[i].height.split('%')[0])
              result.push(sum)
            }
            for (let i = 0; i < objNew2.length; i++) {
              sum1 += Number(objNew2[i].height.split('%')[0])
              result1.push(sum1)
            }
            result.pop()
            result1.pop()
            objNew1.forEach((it, index) => {
              // 第一個(gè)是 0, 之后的每一個(gè)都是 前面的相加!!!1
              if (index === 0) {
                it.top = '0%'
              } else {
                it.top = result[index - 1] + '%'
              }
            })
            objNew2.forEach((it, index) => {
              // 第一個(gè)是 0, 之后的每一個(gè)都是 前面的相加
              if (index === 0) {
                it.top = '0%'
              } else {
                it.top = result1[index - 1] + '%'
              }
            })
            // 計(jì)算比例得
            const one = '名稱1'
            const two = '名稱2'
            const three = '名稱3'
            const four = '名稱4'
            const five = '名稱5'
            const all = [one, two, three, four, five]
            this.charDataArray.push(all)
            this.charDataArray.push(objNew1)
            this.charDataArray.push(objNew2)
            this.charDataArray.push('類型1')

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

松桃| 嘉义市| 资溪县| 奈曼旗| 静安区| 兴文县| 博白县| 永顺县| 临西县| 广平县| 镇原县| 宁晋县| 浪卡子县| 松阳县| 沙坪坝区| 石河子市| 安平县| 基隆市| 岳阳市| 会理县| 白沙| 绥江县| 盐池县| 碌曲县| 岳普湖县| 沙湾县| 衡阳县| 合江县| 柳河县| 石屏县| 海林市| 永福县| 铁力市| 河源市| 上饶市| 五家渠市| 弋阳县| 湄潭县| 剑阁县| 临沭县| 河北区|