Skip to content

Instantly share code, notes, and snippets.

@marcelometal
Created February 24, 2025 17:17
Show Gist options
  • Save marcelometal/fb58b0331842e3fb6e0d84479ce91e53 to your computer and use it in GitHub Desktop.
Save marcelometal/fb58b0331842e3fb6e0d84479ce91e53 to your computer and use it in GitHub Desktop.
Obisidian + DataView: Analytics Year

πŸš€ Itens trabalhados

let pages = [...dv.pages().where(p => p.file.path.includes('/'))];
pages.sort((a, b) => b.file.name.localeCompare(a.file.name));

let grouped = {};

function addToHierarchy(pathArray, obj) {
  let part = pathArray[0];
  if (!obj[part]) {
    obj[part] = { count: 0, children: {} };
  }
  obj[part].count++;
  if (pathArray.length > 1) {
    addToHierarchy(pathArray.slice(1), obj[part].children);
  }
}

for (let page of pages) {
  let date = page.file.name.split('-');
  
  if (date.length < 2) continue;
  
  let year = date[0];
  let folderPath = page.file.folder.split('/');

  if (!grouped[year]) {
    grouped[year] = {};
  }

  addToHierarchy(folderPath, grouped[year]);
}

let sortedYears = Object.keys(grouped).sort().reverse();

for (let year of sortedYears) {
  dv.header(2, `🎯 ${year}`);

  function displayHierarchy(obj) {
    let html = '<ul>';
    let keys = Object.keys(obj).sort();

    for (let key of keys) {
      html += `<li>πŸ“ ${key} (${obj[key].count})`;
      html += displayHierarchy(obj[key].children);
      html += `</li>`;
    }
    html += '</ul>';
    return html;
  }

  dv.el('div', displayHierarchy(grouped[year]), {cls: 'dataview-table'});
}

Top 5 itens mais trabalhados

const pages = Array.from(dv.pages());

const folderCounts = {};

for (const p of pages) {
    const folder = p.file.folder;
    if (folder) {
        folderCounts[folder] = (folderCounts[folder] || 0) + 1;
    }
}

const sortedFolders = Object.entries(folderCounts)
    .sort((a, b) => b[1] - a[1])
    .slice(0, 5);

const topFolders = sortedFolders.map(entry => entry[0]);
const topCounts = sortedFolders.map(entry => entry[1]);

const chartData = {
    type: 'pie',
    data: {
        labels: topFolders,
        datasets: [{
            label: 'Number of Files',
            data: topCounts,
            backgroundColor: [
                '#003f5c',
                '#58508d',
                '#bc5090',
                '#ff6361',
                '#ffa600',
            ],
            borderColor: [
                '#003f5c',
                '#58508d',
                '#bc5090',
                '#ff6361',
                '#ffa600',
            ],
            borderWidth: 1,
        }]
    },
    options: {
        responsive: true,
        maintainAspectRatio: false,
        plugins: {
            legend: {
                position: 'right',
                labels: {
                    boxWidth: 10,
                    fontSize: 12,
                }
            }
        },
        scales: {
            x: {
                display: false,
            },
            y: {
                display: false,
            }
        }
    }
};

const chartContainer = this.container;
chartContainer.style.height = "400px";
chartContainer.style.width = "100%";

window.renderChart(chartData, chartContainer);

Quantidade de temas ao longo dos anos

const pages = Array.from(dv.pages());

const fileCounts = {};

for (const p of pages) {
    const fileName = p.file.name;
    fileCounts[fileName] = (fileCounts[fileName] || 0) + 1;
}

const sortedFileNames = Object.keys(fileCounts).sort();
const sortedCounts = sortedFileNames.map(name => fileCounts[name]);

const chartData = {
    type: 'bar',
    data: {
        labels: sortedFileNames,
        datasets: [{
            label: 'Occurrences',
            data: sortedCounts,
            backgroundColor: '#003f5c',
            borderColor: '#003f5c',
            borderWidth: 1,
        }]
    },
    options: {
        responsive: true,
        maintainAspectRatio: false,
        scales: {
            x: {
                ticks: {
                    maxRotation: 90,
                    minRotation: 45,
                },
                grid: {
                    display: false,
                }
            },
            y: {
                beginAtZero: true,
            }
        }
    }
};

const chartContainer = this.container;
chartContainer.style.height = "400px";
chartContainer.style.width = "100%";

window.renderChart(chartData, chartContainer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment