網(wǎng)站首頁 編程語言 正文
前言:
頁面結構分為兩個部分,柱狀圖 + 文字為一部分,標注為為一部分。
先來看柱狀圖 + 文字這一部分。
寬度定為?width: 55
,?height
?高度使用百分比進行動態(tài)變化。整個部分使用?flex
?布局。通過?justify-content: space-around;
?屬性,對里面的項目進行排列。項目 item 同樣使用 flex 布局,這個是對 柱狀圖 + 文字 進行整體的排列。
<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> 選項三 <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; } } } } }
下方的標注部分,使用絕對定位,width = 100%,height = 100%,實現(xiàn)標注和漸變柱形部分的重疊。
這部分將 li 標簽的 width = 100%,間隔通過 top 來動態(tài)實現(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; } } } }
關于數(shù)值的計算,這里筆者是找到這一組數(shù)據(jù)里面的最大值
let maxValue = 0; data.forEach((dataItem) => { if (dataItem.value > maxValue) maxValue = dataItem.value; });
獲取最大值最近的100整數(shù)
let maxScaleNum = Math.ceil(maxValue / 100) * 100
計算每一個數(shù)據(jù)的 value,占最大值最近的100整數(shù)的百分比。
percent: (dataItem.value / maxScaleNum) * 100
標注的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> 選項三 <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'));
運行結果:
原文鏈接:https://juejin.cn/post/7142305449935634463
相關推薦
- 2022-08-14 kvm虛擬機配置NAT端口轉發(fā)的實現(xiàn)方法_Kvm
- 2022-10-26 jQuery?基礎選擇器與屬性選擇器_jquery
- 2022-12-06 Pthread并發(fā)編程之線程基本元素和狀態(tài)的剖析_C 語言
- 2022-10-18 EasyUI使用DataGrid實現(xiàn)動態(tài)列數(shù)據(jù)綁定_jquery
- 2022-06-16 docker?maven?plugin快速部署微服務的詳細流程_docker
- 2022-04-30 python的正則表達式和re模塊詳解_python
- 2022-10-24 Golang?errgroup?設計及實現(xiàn)原理解析_Golang
- 2023-03-29 Android?Flutter中Offstage組件的使用教程詳解_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支