Skip to content

Instantly share code, notes, and snippets.

@min50250
Last active September 18, 2023 01:26
Show Gist options
  • Save min50250/ec92b4016323f66de63c9672503cc979 to your computer and use it in GitHub Desktop.
Save min50250/ec92b4016323f66de63c9672503cc979 to your computer and use it in GitHub Desktop.
Obsidian Keyword Cloud, Step1 - dataviewjs
/*
```dataviewjs */
let html = ""
let table = [
['[[사과]]', 12],
['[[바나나]]', 50],
['[[체리]]', 60],
['[[딸기]]', 80],
['[[포도]]', 40],
['[[멜론]]', 25],
['[[키위]]', 65],
['[[복숭아]]', 55],
['[[자두]]', 5],
['[[레몬]]', 24],
];
table = valueToLevel(table);
html = ""
for(let j = 0 ; j < table.length ; j++){
html = html + "<span class=\"" + table[j][2] + " cloud\" >" + table[j][0] + "</span>"
}
dv.paragraph(html)
// 주어진 배열(arr)의 요소들을 레벨로 분류하는 함수
// 각 요소는 [이름, 값] 형태를 가짐
function valueToLevel(arr) {
arr = shuffleArray(arr)
const values = arr.map((item) => item[1]);
const minValue = Math.min(...values);
const maxValue = Math.max(...values);
const totalLevels = 7;
const levelSize = (maxValue - minValue) / totalLevels;
return arr.map((item) => {
const name = item[0];
const value = item[1];
const level = Math.ceil((value - minValue) / levelSize);
const adjustedLevel = Math.min(totalLevels, Math.max(1, level));
return [name, value, `cloud${adjustedLevel}`];
});
}
// 주어진 배열(array)을 랜덤하게 섞는 함수
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment