亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何計算占總數的相對百分比?

如何計算占總數的相對百分比?

縹緲止盈 2024-01-18 10:14:04
我正在嘗試創建一個字云,其字體大小基于總數的百分比。每個單詞都有一個“計數”屬性。<h2>Cryptos</h2><div class="topics">{#each cryptos as topic}    <a    href="/cryptos/{topic._id}"    style="font-size: {(topic.count / topics[0].count) * 100 + 100}%;">${topic._id}    ({topic.count})</a>{/each}</div>這不太正常工作,我不知道為什么。font-size: {(topic.count / topics[0].count) * 100 + 100}%;主題按照降序排列.count我基本上希望字體大小在低端為 100%,在高端為 200%。
查看完整描述

1 回答

?
達令說

TA貢獻1821條經驗 獲得超6個贊

通過將字體大小范圍與字數范圍進行比較,可以改變字體大小。


想象一下您有一個單詞列表及其計數:


const words = [

? { text: "apples", count: 10 },

? { text: "pears", count: 30 },

? // ...

]

的范圍font-size可以計算:


const minFontSize = 10

const maxFontSize = 30


// delta between sizes

const fontRange = maxFontSize - minFontSize

然后計算最小字數、最大字數和字數范圍。響應式語句對此很有效:


// list of counts

$: counts = words.map(record => record.count)


// minimum word count

$: min = Math.min(...counts)


// maximum word count

$: max = Math.max(...counts)


// delta between smallest and largest word count

$: countRange = max - min

現在我們可以計算字體大?。?/p>


minFontSize + (deltaWordCount * sizeRatio)

或者更具體地說:


{#each words as word}

? <span style="font-size: {minFontSize + ((word.count - min) * (fontRange / countRange))}px">

? ? {word.text}

? </span>

{/each}


查看完整回答
反對 回復 2024-01-18
  • 1 回答
  • 0 關注
  • 153 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號