Plug: Attribute chart

I usually track my day in a daily note using custom attributes. I needed a way to visualize the correlation between these attributes, so I created this simple plug.

For example, I use these custom attributes in the front matter of my daily notes:

attribute:
  mood: 7
  hoursOfExercise: 1

To create a chart I add this widget:

```attributeChart
  query:
    page where name =~ /^Journal\/Day\//
  attributes:
    - name: hoursOfExercise
      type: line
    - name: mood
      type: line 
```

Result:

ppArLzGsOj

You can give it a try by adding this plug

github:vuau/silverbullet-plug-attribute-chart/attributeChart.plug.js
8 Likes

This is pretty cool! I edited your post a bit to make it more obvious that this is a fenced code block feature.

Also found the github repo for those who want to take a peek: GitHub - vuau/silverbullet-plug-attribute-chart

Happy to add this to Plugs would you be ok with that?

Thanks for editing my post to make it clearer Zef. And please feel free to put it in Plugs if you think it might be useful for other people.

I love this!

Added to the website Plugs/Attribute Chart

3 Likes

This plug is great.

How can I improve the query building the chart by filtering only dates where, for example attribute mood is non empty?

I’d like to only show dates with data points added.

I may want to create different charts with different attributes so I’d love to know how such filtering could be added.

Thanks

I would filter the pages like this


page where name =~ /^Journal\/Day\// and attribute.mood != undefined

You can create as many charts as you want on a page using fenced code blocks. Currently, the plug just extracts attribute values into an array and uses Chart.js to draw the charts. It only works with the types line and bar. I haven’t tested it with other chart types, but to draw other types, we may need to organize the data differently.

Thank you very much :slightly_smiling_face:

I am 100% sure I tried that query, using attribute.mood but I am not sure why it didn’t work then.
Maybe i needed a full Reload or something to get it working.

Now it does :smiley:

1 Like

One more small request :slight_smile:

How would you filter the chart to show only the last x Days (30 days, 90 days, etc)?

I can manage doing that by order by name desc, for example, but that would reverse the chart too, which is not what I want.

I thought of maybe parsing the name back to a date and then doing it but wondering if there is a more efficient way of achieving it

How about this

page where
  name =~ /^Journal\/Day\// and
  attribute.mood != undefined and
  niceDate(created) >= "2024-09-1" and
  niceDate(created) <= "2024-09-30"

Updated: I missed that you mentioned “The last x days”. That requires some calculations with the current date. Something similar to the “DaysTillDate” template function that people shared. However, I don’t think we can call a custom function in a query.

thanks for the recommendation.

Your query didn’t work for me but I found a way it throws results:

  query:
    page where name =~ /^Journal\/Day\/2024/
    and attribute.mood != null
    and "{{niceDate(created)}}" >= "2024-09-01"
    and "{{niceDate(created)}}" <= "2024-09-30"

However the filtering doesn’t seem to work. No matter which dates I add, I always get all of them.

I confirmed that

```template
{{niceDate(created)}}
```

throws a date in each of the Journal pages.

Now, even if this works, I see a small caveat. I not always create the Journal date on the exact date, soemetimes i forget, or I am away from computer, and i create the journal date entry a couple of days later.
using niceDate(created) would use the creation date and not the Journal date per se.

I think your issue with the Journal date is not created date is already solved by my mistake :sweat_smile:. When creating the plug I used the page name for the x axis instead of the created date.

I’m thinking of supporting a date range option such as “fromDate - toDate" or “last 30 days” … (well when I have time to work on it)

1 Like

@paletochen I tried creating a “DaysTillNow” function and it seems worked for your use case to filter pages within the last 30 days

First, register a function using space-script

```space-script
silverbullet.registerFunction("daysTillNow", (date) => {
  const parsedDate = Temporal.PlainDate.from(date);
  const now = Temporal.Now.plainDateISO();
  return parsedDate.until(now, { largestUnit: 'days' }).days;
});
```

Then run System: Reload command to activate the function

Finally, use it in your query

```attributeChart
  query:
    page where name =~ /^Journal\/Day\// and
    daysTillNow(created) <= 30
```
1 Like

Thanks!
This works with created.
I guess I’ll have to be mindful of always creating the journal dates in the right moment, but this should do a much better job than having nothing as even if i missed the creation for a few days, it allows me to plot the chart with way lower points, aiming at something like ~30 days or so (at some point it doesn’t matter if i render 30, 28 or 35 :slight_smile: )

Thanks again!