Skip to content

Instantly share code, notes, and snippets.

@WebInspectInc
Last active April 26, 2024 03:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WebInspectInc/bf3f26e95899eb91782068d974ffa23e to your computer and use it in GitHub Desktop.
Save WebInspectInc/bf3f26e95899eb91782068d974ffa23e to your computer and use it in GitHub Desktop.
A small script for creating task completion graphs in Obsidian. See how to use this here: https://obsidian.rocks/plotting-task-completions-with-dataviewjs-and-obsidian-charts/
const dateformat = "YYYY-MM-DD";
const monthformat = "YYYY-MM";
const yearformat = "YYYY";
const days = parseInt(dv.current().days);
const months = parseInt(dv.current().months);
const years = parseInt(dv.current().years);
const projectFolder = dv.current().projectfolder || '';
const graphColor = dv.current().lineColor || '#de454e';
const charts = [];
if (!(days || months || years)) {
dv.el('strong', 'Task Graphs: to generate graphs, please add days, months, or years (or all three!) in your file properties.');
}
function getDailyTasks(numDays) {
let dates = createArrayOfDates('days', dateformat, numDays);
return countTasks(dates);
}
function getMonthlyTasks(numMonths) {
let dates = createArrayOfDates('months', monthformat, numMonths);
return countTasks(dates);
}
function getYearlyTasks(numYears) {
let dates = createArrayOfDates('years', yearformat, numYears);
return countTasks(dates);
}
function createArrayOfDates(range, format, num) {
const start = moment().add(1, range).startOf('day');
let dates = [...Array(num)];
console.log(dates);
return dates.map(() => start.subtract(1, range).format(format).toString());
}
function countTasks(dates) {
var taskAmounts = [];
var taskLabels = []
for (let i = dates.length - 1;i>=0;i--) {
let doneSymb = '✅';
let dString = `${doneSymb} ${dates[i]}`;
let tasks = dv.pages(projectFolder).file.tasks.where(t => t.text.includes(dString));
taskLabels.push(dates[i]);
taskAmounts.push(tasks.length);
}
return { amounts: taskAmounts, labels: taskLabels };
}
if (days) {
const dailyTasks = getDailyTasks(days);
charts.dayChart = {
type: 'line',
data: {
labels: dailyTasks.labels,
datasets: [{
label: 'Daily completed tasks',
data: dailyTasks.amounts,
backgroundColor: [
graphColor
],
borderColor: [
graphColor
],
borderWidth: 1
}]
}
}
}
if (months) {
const monthlyTasks = getMonthlyTasks(months);
charts.monthChart = {
type: 'line',
options: {
scales: {
y: {
beginAtZero: true
}
}
},
data: {
labels: monthlyTasks.labels,
datasets: [{
tension: 0.1,
label: 'Monthly completed tasks',
data: monthlyTasks.amounts,
backgroundColor: [
graphColor
],
borderColor: [
graphColor
],
borderWidth: 1
}]
}
}
}
if (years) {
const yearlyTasks = getYearlyTasks(years);
charts.yearChart = {
type: 'line',
options: {
scales: {
y: {
beginAtZero: true
}
}
},
data: {
labels: yearlyTasks.labels,
datasets: [{
tension: 0.1,
label: 'Yearly completed tasks',
data: yearlyTasks.amounts,
backgroundColor: [
graphColor
],
borderColor: [
graphColor
],
borderWidth: 1
}]
}
}
}
if (!window.renderChart) {
dv.el('strong', 'Task Graphs: Please install and/or enable the Obsidian Charts plugin');
} else if (!input.container) {
dv.el('strong', 'Task Graphs: Your include has a syntax error. Please see documentation and supply the correct parameters.')
} else {
if (days) window.renderChart(charts.dayChart, input.container);
if (months) window.renderChart(charts.monthChart, input.container);
if (years) window.renderChart(charts.yearChart, input.container);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment