Development,  Tech Tips

Using Dataview with Charts in Obsidian

Obsidian is my third most used application after Keyboard Maestro and Alfred. I’ve been using the dataview plugin since I got started with Obsidian. It’s an incredible plugin that gives you the ability to treat your notes like database records. In this example I’ll show how I use dataview to make my projects queryable, and then how I use Obsidian-Charts to make some bar charts of this data.

Using Dataview Variables

Here’s an example of a Project file in my Obsidian vault:

Example project file in Obsidian

 

Here’s the template file I use for my projects:

In my project pages I’m making use of Obsidian variables that are defined inline. You can also define them in the YAML frontmatter of any file.

Querying with Dataview

Obsidian dataview lets me query the files in my vault using those variables. Here’s a straightforward example making use of the variables shown above:

The result of this is a table, shown here with a bit of CSS applied.

Table of query resultsPretty cool, right? This table is updated in near real-time when any of the included data in my vault changes.

Using Dataview Results with Obsidian-Charts

In order to use dataview results as the data for Obsidian Charts I used the dataviewjs aspect of dataview.

After a lot of trial and error I finally understood how to query for the raw data from dataview and interpret/modify/use that data.

Here’s the final result:

The result looks like this:

Obsidian charts example

As I said, it took some trial and error to get here. I knew I had a working query (from the section above) so it was simply a matter of getting it into the chart. I read through the documentation for dataviewjs and determined I needed to use dv.query() . I used console.log(rawData)  to explore the results (after opening the developer tools in Obsidian via View > Toggle Developer Tools). After determining where the data was in the results I was able to work up some javascript to build out the data arrays that I thought Obsidian-Charts was expecting. I was awfully confused by the labels and data that Obsidian-Charts uses. The whole chart has labels and each dataset has a label. Eventually I figured it out — the top-level labels are the “columns” on the X axis; the other labels are repeated for each value in their associated data array; think of if this way… each dataset (e.g., Backend) is an array containing the value (e.g., 40) for each master label (e.g., Agileadam Foo). So in this example there are 3 elements in each of the four datasets — one for each of the Agileadam * projects.

The code above is the final refactoring of the following, which may be easier for you to digest if you’re not used to array maps:

Also, it may be helpful for you to see was “rows” looks like. Here’s an example:

 

Leave a Reply

Your email address will not be published.