使用vue3實(shí)現(xiàn)數(shù)據(jù)漏斗圖
用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)文章
vue-element-admin如何轉(zhuǎn)換成中文
這篇文章主要介紹了vue-element-admin如何轉(zhuǎn)換成中文問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03vue-baidu-map 進(jìn)入頁(yè)面自動(dòng)定位的解決方案(推薦)
這篇文章主要介紹了vue-baidu-map 進(jìn)入頁(yè)面自動(dòng)定位的解決方案,需要的朋友可以參考下2018-04-04vue3實(shí)現(xiàn)旋轉(zhuǎn)圖片驗(yàn)證
這篇文章主要為大家詳細(xì)介紹了vue3實(shí)現(xiàn)旋轉(zhuǎn)圖片驗(yàn)證,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04vue如何使用mapbox對(duì)當(dāng)前行政區(qū)劃進(jìn)行反選遮罩
這篇文章主要介紹了vue如何使用mapbox對(duì)當(dāng)前行政區(qū)劃進(jìn)行反選遮罩問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10vue實(shí)現(xiàn)桌面向網(wǎng)頁(yè)拖動(dòng)文件的示例代碼(可顯示圖片/音頻/視頻)
這篇文章主要介紹了vue實(shí)現(xiàn)桌面向網(wǎng)頁(yè)拖動(dòng)文件的示例代碼(可顯示圖片/音頻/視頻),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03基于el-table實(shí)現(xiàn)行內(nèi)增刪改功能
這篇文章主要介紹了基于el-table實(shí)現(xiàn)行內(nèi)增刪改功能,用過通過操作按鈕點(diǎn)擊刪除或者編輯功能即可實(shí)現(xiàn)相應(yīng)的效果,下面小編給大家分享實(shí)例代碼感興趣的朋友跟隨小編一起看看吧2024-04-04vue 實(shí)現(xiàn)網(wǎng)頁(yè)截圖功能詳解
這篇文章主要介紹了通過vue實(shí)現(xiàn)網(wǎng)頁(yè)截圖的功能,有興趣的童鞋可以了解一下2021-11-11VUE中對(duì)object.object和object[object]的使用解讀
這篇文章主要介紹了VUE中對(duì)object.object和object[object]的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06vue項(xiàng)目在線上服務(wù)器訪問失敗原因分析
這篇文章主要介紹了vue項(xiàng)目在線上服務(wù)器訪問失敗原因分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-08-08