Example how to use Chart.js

Hello, here is an example code on how to use Chart.js, you need to copy the code itself and change container id to “chart” Step-by-step guide | Chart.js

(async function() {
  const data = [
    { year: 2010, count: 10 },
    { year: 2011, count: 20 },
    { year: 2012, count: 15 },
    { year: 2013, count: 25 },
    { year: 2014, count: 22 },
    { year: 2015, count: 30 },
    { year: 2016, count: 28 },
  ];

  new Chart(
    document.getElementById('chart'),
    {
      type: 'bar',
      data: {
        labels: data.map(row => row.year),
        datasets: [
          {
            label: 'Acquisitions by year',
            data: data.map(row => row.count)
          }
        ]
      }
    }
  );
})();

Sorry, not clear for me.
Also the URL to chartjs.org doesn’t work (I get an error 404 in the browser).

What I tried to to:
In a Page I made a new “/Embed”, choosing “Chart”.
In the now appearing field “Enter embed code for Chart” I pasted the full code from inside your codebox.
(Not just the visible part from the code box, but the full code’s lenght.)

But this doesn’t work.

What do you mean with “you need to change container id”?

Edit: Now it works. The chart appears after clicking into the empty area for it.
Seems the new Embeds stuck sometimes.

Edit 2: The URL also works after removing the colon from it’s right end.

just copy a code I provided, it should work:

Chart is closed by default because it literally renders JS code, so it’s not safe to run it as is.

First I want to thank you for the progress of the application.

Now, unfortunately even this part of including statistical charts with Chart.js has not been completely satisfactory.

Although I included the example given in this post, the example is not satisfactory.

It is misconfigured and does not present the data as it should.

And if I want it as a pie chart it comes out like this

image

Give me example code pls in a bug report

How can I read the numerical value of a relation in several objects with a date as the title for the data series?

The Object type is “Diary”.
The objects have the name {year}-{month}-{day} (e.g. 2024-01-13).
The relation (# number) to the visualisation in chart.js is called „powerconsumption".

Can I filter the readout so that I only read out the objects with “2023-*”?

There is no such functionality yet, I can’t promise it will be considered for development.

Many thanks for the feedback.

So chart.js does not work for now in Anytype?

I tried to use the doughnut chart example from chartjs.org.
https://www.chartjs.org/docs/latest/charts/doughnut.html#doughnut

As soon as I paste the code in the field (where it says “Enter embed code for Chart”), I get nothing displayed, just an empty space!

I do not know how to translate their code examples into JS code yet, but here is a link to show the actual js code that generates charts: Step-by-step guide | Chart.js

Same for me. I tried many times with graph from the graph.js site and with some created by me. But when I paste only a white block appear. The code posted here is working but the other from official graph.js docs or the one I made are not working

A bit off-topic, but I’m not really keen on javascripting in my documents. Mermaid provides simple charts integration (please update lib version :pray:):

Examples:
image

Note: Mermaid embeds are broken on the 0.39 but it’ll be fixed in the next release

Same for me.
The code @Razor has posted in the startpost works.
But the same code, copied from graph.js website, doesn’t work.

I don’t get it why.
Also pasting with Ctrl + Shift + v makes no difference. The code from the official website doesn’t work, although identical with Razor’s example.

I tried to copy the code with Firefox as well as with Brave. Nothing works.
It also doesn’t matter how I copy the code (using the copy button or marking the code in the box with the mouse and pressing Ctrl + c).

Edit:
I also tried to copy and paste both codes separate in Notepad and saving it as utf-8.
Then closing Notepad, starting it again, loading the codes, copy them in the clipboard, pasting them.
Same result: the code Razor has posted works, the other one doesn’t.
??? :thinking: ???

Post an example please

Updated the library

Here’s a zipped txt file, saved with Notepad as uft-8.
It contains both codes:

  1. Yours from your startpost
  2. The one from chartjs.org

Beide Codes utf-8.zip (485 Bytes)

Both codes look identical from the first to the last character.
But only your code works, not the one from chartjs.org

this is why it does not work:

document.getElementById('acquisitions')

Change the id ‘acquisitions’ to ‘chart’ and it will work, container for charts is the same for all charts while examples on the website come with different ids.

Try to copy the code from the textfile.
But each time you try create a new chart embed.

Sometimes, if you update the content in an already existing embed, it interferes somehow.
For that reason better create new embeds for each try.

Edit: Overlapping posts - I got it!
There is in deed a difference <:-(