欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用vue3實(shí)現(xiàn)數(shù)據(jù)漏斗圖

 更新時(shí)間:2024年11月15日 10:17:58   作者:一個(gè)老碼農(nóng)  
漏斗圖一般會(huì)借助一些第三方的庫(kù)來(lái)實(shí)現(xiàn),這些庫(kù)使用起來(lái)雖然簡(jiǎn)單順手,但是如果要定制樣式就會(huì)不太方便,本文我們就來(lái)用vue3手?jǐn)]一個(gè)漏斗圖吧

用vue3實(shí)現(xiàn)數(shù)據(jù)漏斗圖

在前端開發(fā)中,漏斗圖我們一般會(huì)借助一些第三方的庫(kù)來(lái)實(shí)現(xiàn),如: echarts。這些庫(kù)使用起來(lái)雖然簡(jiǎn)單順手,但是如果要定制樣式就會(huì)不太方便。今天我就來(lái)用vue3手?jǐn)]一個(gè)漏斗圖。

代碼實(shí)現(xiàn)

比如有以下一組數(shù)據(jù),我希望至上而下從大到小排序,并生成一個(gè)漏斗圖:

[
  { label: 'A', value: 100 },
  { label: 'B', value: 200 },
  { label: 'C', value: 50 },
  { label: 'D', value: 800 },
  { label: 'E', value: 500 }
]

廢話不多說(shuō),直接上代碼:

<template>
  <div class="funnel-container">
    <div class="top-title">這是一個(gè)漏斗</div>
    <div v-for="(option, index) in sortOptions()" :key="index" class="funnel-item">
      <!-- 白色遮罩,底部為弧形,遮在下一個(gè)option色塊上,以實(shí)現(xiàn)色塊的頂部凹陷效果 -->
      <div
        class="white-rad"
        :style="{
          width: `${index === 0 ? topWidth : topWidth * Math.pow(0.76, index) + 5}px`,
          zIndex: topZIndex - (index + 1) * 2 + 1
        }"
      />
      <!-- 每一個(gè)option的色塊 -->
      <div
        class="funnel-step"
        :style="{
          width: `${topWidth * Math.pow(0.76, index)}px`,
          zIndex: topZIndex - (index + 1) * 2,
          backgroundColor: colors[index]
        }"
      >
        <!-- 色塊中顯示的文字 -->
        <p>{{ option.label }} {{ option.value }}</p>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
// 最頂部title的zindex
const topZIndex = ref(100)
// 最頂部title和第一個(gè)option的寬度
const topWidth = ref(320)
// 顏色值
const colors = ref(['#7c0a3a', '#d42d2a', '#ff8833', '#2790c3', '#005587'])
// 數(shù)據(jù)
const optionsData = ref([
  { label: 'A', value: 100 },
  { label: 'B', value: 200 },
  { label: 'C', value: 50 },
  { label: 'D', value: 800 },
  { label: 'E', value: 500 }
])

// 從大到小排序
const sortOptions = () => {
  optionsData.value.sort((a, b) => b.value - a.value)
  return optionsData.value
}
</script>

<style>
.funnel-container {
  width: 320px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin: 50px;
}

/* 頂部橢圓形title */
.funnel-container .top-title {
  margin: 0;
  padding: 10px;
  background-color: #935d71;
  color: #fff;
  border-width: 3px 6px 10px 6px;
  border-style: solid;
  border-color: #520a2a;
  border-radius: 50%;
  z-index: 100;
  position: relative;
  height: 55px;
  width: 320px;
}

.funnel-container .funnel-item {
  width: 100%;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* option的白色遮罩 */
.funnel-container .funnel-item .white-rad {
  background-color: white;
  height: 55px;
  margin-transform: translateY( -50px;
  border-radius: 0 0 50% 50%;
  position: relative;
}

/* 每一個(gè)option色塊,利用clip-path屬性裁剪為底部弧形效果 */
.funnel-container .funnel-item) .funnel-step {
  margin-transform: translateY( -25px;
  border-radius: 0 0 50% 50%;
  position: relative;
  clip-path: polygon(0% 0%, 100% 0%, 85% 100%, 15% 100%);
  height: 100px;
}

/* 色塊上的文字 */
.funnel-step p {
  color: white;
  text-align: center;
  margin-top: 40px;
  font-weight: 500;
}
</style>

效果圖如下:

實(shí)現(xiàn)思路

1. 最頂部的橢圓使用css的屬性border-radius: 50%;實(shí)現(xiàn),橢圓的上下左右border寬度有出入,以實(shí)現(xiàn)視差效果: border-width: 3px 6px 10px 6px;

2. 漏斗每一個(gè)option使用css的屬性clip-path: polygon(0% 0%, 100% 0%, 85% 100%, 15% 100%);實(shí)現(xiàn)底部弧度。

3. 漏斗每一個(gè)option頂部用一個(gè)底部被切成孤度的白色遮罩遮蓋,以實(shí)現(xiàn)頂部的凹陷效果。

4. 把數(shù)據(jù)optionsData以對(duì)象的value屬性從大到小進(jìn)行排序,并將label屬性值和value屬性值顯示在色塊中

注意事項(xiàng)

1. 每個(gè)色塊的底部寬度為頂部寬度的70%,而下一個(gè)色塊頂部需要比上一個(gè)色塊底部寬一些才能實(shí)現(xiàn)較好的視覺效果,本demo中采用的是76%。即:下一個(gè)色塊的頂部寬度為上一個(gè)色塊頂部寬度的76%。

2. 因?yàn)槭褂玫恼谡謱?shí)現(xiàn)的頂部凹陷效果,所以元素的z-index屬性,自上而下依次遞減。

3. 白色遮罩需要比下面色塊稍寬,以實(shí)現(xiàn)色塊兩邊尖尖的效果,可根據(jù)實(shí)際情況調(diào)整

到此這篇關(guān)于使用vue3實(shí)現(xiàn)數(shù)據(jù)漏斗圖的文章就介紹到這了,更多相關(guān)vue3數(shù)據(jù)漏斗圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論