Advanced Query with Graph for Tag Count

Just wanted to share what finally worked for me if anyone else is struggling. Here is an advanced query that will display your journal page tags along with a count within a calendar year. I like to do a year end review and this saves me the step of generating the graph myself.

#+BEGIN_QUERY WORKS, Produces graph with data

{

:title “Journal Tags Count – 2024”

:query [:find ?tag (count ?b)

      :keys tag count

      :in $ ?year

      :where

        \[?p :block/journal? true\]

        \[?p :block/journal-day ?d\]

        \[(str ?d) ?date-str\]

        \[(subs ?date-str 0 4) ?page-year\]

        \[(= ?page-year ?year)\]

        \[?b :block/page ?p\]

        \[?b :block/refs ?r\]

        \[?r :block/name ?tag\]\]

:inputs [“2024”]

:view

(fn [results]

(let \[max-count (apply max (map :count results))\] ; biggest bar width

  \[:div

   {:style

    {:display "flex"

     :flex-direction "column"

     :gap "4px"

     :padding "4px 4px 4px 0"

     :background-color "#333"

     :color "#eee"

     :font-size "80%"}}

   (for \[{:keys \[tag count\]} results\]

     \[:div

      {:style

       {:width (str (\* (/ count max-count) 100) "%")

        :height "20px"

        :padding "2px"

        :white-space "nowrap"

        :border-radius "0 4px 4px 0"

        :box-shadow "1px 1px 2px #111"

        :background "lightblue"

        :background-clip "padding-box"}}

      (str count " " tag)\])\]))}

#+END_QUERY

#+BEGIN_QUERY WORKS, Produces graph with data

{

:title "Journal Tags Count – 2024"

:query [:find ?tag (count ?b)

:keys tag count

:in $ ?year

:where

[?p :block/journal? true]

[?p :block/journal-day ?d]

[(str ?d) ?date-str]

[(subs ?date-str 0 4) ?page-year]

[(= ?page-year ?year)]

[?b :block/page ?p]

[?b :block/refs ?r]

[?r :block/name ?tag]]

:inputs ["2024"]

:view

(fn [results]

(let [max-count (apply max (map :count results))] ; biggest bar width

[:div

{:style

{:display "flex"

:flex-direction "column"

:gap "4px"

:padding "4px 4px 4px 0"

:background-color "#333"

:color "#eee"

:font-size "80%"}}

(for [{:keys [tag count]} results]

[:div

{:style

{:width (str (* (/ count max-count) 100) "%")

:height "20px"

:padding "2px"

:white-space "nowrap"

:border-radius "0 4px 4px 0"

:box-shadow "1px 1px 2px #111"

:background "lightblue"

:background-clip "padding-box"}}

(str count " " tag)])]))}

#+END_QUERY