網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
前言:
頁(yè)面結(jié)構(gòu)分為兩個(gè)部分,柱狀圖 + 文字為一部分,標(biāo)注為為一部分。
先來(lái)看柱狀圖 + 文字這一部分。
寬度定為?width: 55
,?height
?高度使用百分比進(jìn)行動(dòng)態(tài)變化。整個(gè)部分使用?flex
?布局。通過(guò)?justify-content: space-around;
?屬性,對(duì)里面的項(xiàng)目進(jìn)行排列。項(xiàng)目 item 同樣使用 flex 布局,這個(gè)是對(duì) 柱狀圖 + 文字 進(jìn)行整體的排列。
<div className="crosswise_diagram_top"> {listData.map((item) => { return ( <div className="crosswise_diagram_item" key={item.value}> <div className="crosswise_diagram_item_precent"> <div className="precent" style={{ width: 55, height: `${item.percent}%` }} > <div> 選項(xiàng)三 <span>254</span> </div> </div> </div> <div className="crosswise_diagram_item_name">{item.name}</div> </div> ) })} </div>
.crosswise_diagram_top { display: flex; justify-content: space-around; height: 100%; .crosswise_diagram_item { display: flex; flex-direction: column; width: 55px; position: relative; justify-content: end; height: 100%; .crosswise_diagram_item_name { margin-top: 11px; font-size: 16px; font-weight: 400; color: #07132b; } .crosswise_diagram_item_precent { height: 100%; display: contents; .precent { border-radius: 4px; position: relative; &::after { content: ''; height: 102%; border-right: 1px dashed #07132b; position: absolute; top: -2%; } > div { position: absolute; width: 66px; text-align: center; top: -2%; height: 21px; margin-top: -21px; } } } } }
下方的標(biāo)注部分,使用絕對(duì)定位,width = 100%,height = 100%,實(shí)現(xiàn)標(biāo)注和漸變柱形部分的重疊。
這部分將 li 標(biāo)簽的 width = 100%,間隔通過(guò) top 來(lái)動(dòng)態(tài)實(shí)現(xiàn)。
<div className="crosswise_diagram_bottom"> <ul className="crosswise_diagram_ul"> {scaleArray.map((item, itemIndex) => { return ( <li className="crosswise_diagram_li" style={{ top: `${(100 / 5) * itemIndex}%` }} key={item} > <span className="crosswise_diagram_name">{item}</span> <span className="crosswise_diagram_precent" /> </li> ) })} </ul> </div>
.crosswise_diagram_bottom { position: absolute; top: -36px; left: -55px; height: 100%; width: 100%; .crosswise_diagram_ul { width: 100%; .crosswise_diagram_li { width: 100%; position: absolute; display: flex; flex-direction: row; .crosswise_diagram_name { position: relative; top: -9px; width: 45px; font-size: 12px; font-weight: bold; color: #a6b1bf; } .crosswise_diagram_precent { width: 90%; height: 100%; border-top: 1px dashed #d7dbe0; color: #a6b1bf; } } } }
關(guān)于數(shù)值的計(jì)算,這里筆者是找到這一組數(shù)據(jù)里面的最大值
let maxValue = 0; data.forEach((dataItem) => { if (dataItem.value > maxValue) maxValue = dataItem.value; });
獲取最大值最近的100整數(shù)
let maxScaleNum = Math.ceil(maxValue / 100) * 100
計(jì)算每一個(gè)數(shù)據(jù)的 value,占最大值最近的100整數(shù)的百分比。
percent: (dataItem.value / maxScaleNum) * 100
標(biāo)注的top,使用 for 循環(huán)生成。
const multiple = Math.floor(maxScaleNum / scaleNum) const newArray = new Array() for (let i = 0; i <= maxScaleNum; i += multiple) { newArray.push(i) } setScaleArray(newArray.reverse())
?整體代碼:
import React, { useState, useEffect } from 'react'; import ReactDom from 'react-dom'; const Test = ({ data, scaleNum = 5 }) => { const [listData, setListData] = useState( [] ) const [scaleArray, setScaleArray] = useState([]) useEffect(() => { if (scaleNum <= 0) { return } let maxValue = 0 data.forEach((dataItem) => { if (dataItem.value > maxValue) maxValue = dataItem.value }) let maxScaleNum = Math.ceil(maxValue / 100) * 100 if (maxValue <= 0) { setScaleArray(new Array(scaleNum).fill(0)) setListData( data.map((dataItem) => { return { name: dataItem.name, percent: 0, value: 0 } }) ) return } setListData( data.map((dataItem) => { return { name: dataItem.name, percent: (dataItem.value / maxScaleNum) * 100, value: dataItem.value } }) ) const multiple = Math.floor(maxScaleNum / scaleNum) const newArray = new Array() for (let i = 0; i <= maxScaleNum; i += multiple) { newArray.push(i) } setScaleArray(newArray.reverse()) }, [data, scaleNum]) return ( <section className="crosswise_diagram"> <div className="crosswise_diagram_top"> {listData.map((item) => { return ( <div className="crosswise_diagram_item" key={item.value}> <div className="crosswise_diagram_item_precent"> <div className="precent" style={{ width: 55, height: `${item.percent}%` }} > <div> 選項(xiàng)三 <span>254</span> </div> </div> </div> <div className="crosswise_diagram_item_name">{item.name}</div> </div> ) })} </div> <div className="crosswise_diagram_bottom"> <ul className="crosswise_diagram_ul"> {scaleArray.map((item, itemIndex) => { return ( <li className="crosswise_diagram_li" style={{ top: `${(100 / 5) * itemIndex}%` }} key={item} > <span className="crosswise_diagram_name">{item}</span> <span className="crosswise_diagram_precent" /> </li> ) })} </ul> </div> </section> ) } ReactDom.render(<Test style={{ width: 440 }} scaleNum={6} data={[ { name: '西瓜', value: 40 }, { name: '菠蘿', value: 56 }, { name: '香蕉', value: 47 } ]} />, document.getElementById('app'));
運(yùn)行結(jié)果:
原文鏈接:https://juejin.cn/post/7142305449935634463
相關(guān)推薦
- 2023-06-03 React實(shí)現(xiàn)一個(gè)倒計(jì)時(shí)hook組件實(shí)戰(zhàn)示例_React
- 2022-03-26 Android?Studio中Logcat寫(xiě)入和查看日志_Android
- 2022-03-23 C++實(shí)現(xiàn)水仙花數(shù)判斷實(shí)例_C 語(yǔ)言
- 2022-08-23 React?Native中實(shí)現(xiàn)動(dòng)態(tài)導(dǎo)入的示例代碼_React
- 2023-05-24 Android打空包后提示沒(méi)有"android:exported"的屬性設(shè)置問(wèn)題解決_Android
- 2022-08-26 詳解WPF雙滑塊控件的使用和強(qiáng)制捕獲鼠標(biāo)事件焦點(diǎn)_C#教程
- 2022-06-25 在Nginx服務(wù)器上安裝SSL證書(shū)完成HTTPS請(qǐng)求的步驟詳解(springboot項(xiàng)目)_ngin
- 2022-01-13 使用element插件中Descriptions遇到的坑
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支