What I’m trying to do
Hello,
- I “simply” want to display a line graph of all tasks from a certain tag that I completed within the last 7 days.
- I want to display multiple lines in one graph.
Things I have tried
The query for my data would be this:
```dataview
TASK
WHERE completed
AND contains(tags, "#Post")
AND contains(path, "ProjectOne")
Ive found a different format that supposedly returns the same data:
const tasks = dv.pages().where(p => p.completed && p.tags.includes("#Post") && p.path.includes("ProjectOne"));
Now Im not sure wether the last code snippet is already a working way of querying the data. It is not clear to me how I can easily check the values stored in “tasks”. But onward:
For a chart / line graph I need two dimensions. So for the y I would need to have these values:
0 = no completed tasks of a date
1 = one completed tasks with the same date
2= two completed tasks with the same date
etc
Dataview offers the option “Automatic Task completion tracking” which automatically adds the metadata completion:: to completed tasks. So this data should be usable. I just dont know how exactly.
As far as I understand the example below, a chart simply adds the numeric values and the corresponding labels without any connection of the two. This means fo the y dimension, the list needs to include the 0 values for the days where no tasks where completed, otherwise it would not sync up with the dates on the x dimension. This information can not be generated from the tasks query alone.
So we need to create a list of the dates of the last 7 days. These dates would be the “Labels” for the x dimension. For the y values, we need to query the tasks as above, and then compare the dates of the entries of both lists and form a third one which consists of the corresponding values for each of the 7 days from the first list.
Column 1 | Column 2 | Column 3 |
---|---|---|
List of Days | List of Tasks | List of values for the y dimension |
2024-03-08-Friday | 0 | |
2024-03-09-Saturday | Post1 completed::2024-03-09-Saturday, Post2 completed::2024-03-09-Saturday | 2 |
2024-03-10-Sunday | Post3 completed::2024-03-10-Sunday | 1 |
2024-03-11-Monday | 0 | |
2024-03-12-Tuesday | 0 | |
2024-03-13-Wednesday | Post4 completed::2024-03-13-Wednesday, Post5 completed::2024-03-13-Wednesday | 2 |
2024-03-14-Thursday | 0 |
At least thats what I figured. I could get this to work in python but I have no idea how to do that in javascript, let alone dataview. Chat GPT seems to have its troubles with dataview syntax, too.
I use a great snippet for my daily notes that tracks certain metadata that I fill out. This script works perfectly fine (I just need add a step to translate the time format I enter in the note (e.g. “0930”) to a format with 0-100 decimal steps (instead of 0-60) that the graph expects). Since I create a daily note every day, it doesnt have a method to include “no-entry” days. But it shows how the line chart is created:
```dataviewjs
const journalPath = '"DailyNotes"';
const chartColor = '#3e4f79';
const chartColor2 = '#A40E4C';
const dailyNotesPlugin = app.internalPlugins.plugins['daily-notes'];
const dateFormat = dailyNotesPlugin.instance.options.format;
var labels = [];
var alarm = [];
var gotup = [];
var sortByName = (a, b) => moment(a, dateFormat).format('YYYYMMDD') - moment(b, dateFormat).format('YYYYMMDD');
var latest_journals = dv.pages(journalPath)
.sort(j => j.file.name, 'asc', sortByName);
for (var i=0; i < latest_journals.length; i++) {
var j = latest_journals[i];
if (j['Alarm'] !== undefined) {
alarm.push(j['Alarm']);
labels.push(j.file.name);
}
}
for (var i=0; i < latest_journals.length; i++) {
var j = latest_journals[i];
if (j['Got-up-at'] !== undefined) {
gotup.push(j['Got-up-at']);
}
}
const limitedLabels = labels.slice(0, 14);
const limitedAlarm = alarm.slice(0, 14);
const limitedGotup = gotup.slice(0, 14);
const lineChart = {
type: 'line',
data: {
labels: limitedLabels,
datasets: [{
label: 'Alarm',
data: limitedAlarm,
backgroundColor: [chartColor],
borderColor: [chartColor],
borderWidth: 1,
fill: true,
tension: 0.3,
transparency: 0.15,
},{
label: 'Got Up At',
data: limitedGotup,
backgroundColor: [chartColor2],
borderColor: [chartColor2],
borderWidth: 1,
fill: true,
tension: 0.3,
transparency: 0.15,
}]
}
}
window.renderChart(lineChart, this.container);
I hope that this script is a good starting point. It returns this graph, which helps me track my mornings of the previous 14 days (for the new line graph, I would set the fill to 0 and tension to 1).
I think charts are a great way to visualize progress or activity across the vault and I hope this is not more complicated than I think. Thank you for your time.