phibr0 / obsidian-charts

Charts - Obsidian Plugin | Create editable, interactive and animated Charts in Obsidian via Chart.js
https://charts.phib.ro/
GNU Affero General Public License v3.0
592 stars 27 forks source link

using dataview data from other files #34

Closed notflip closed 2 years ago

notflip commented 2 years ago

I'm trying to include dataview data from a certain folder, which has a few entries in the frontmatter, for example

---
date: 25-11-2021
weight: 68.4
---

I'm trying to achieve this using

const obsData = dv.pages('"Journal/Daily"')

const labels = [
  'January',
  'February',
  'March',
  'April',
  'May',
  'June',
];

const data = {
  labels: labels,
  datasets: [{
    label: 'My First dataset',
    backgroundColor: 'rgb(255, 99, 132)',
    borderColor: 'rgb(255, 99, 132)',
    data: [obsData.weight],
  }]
};

const chartData = {
    type: 'line',
    data
}

window.renderChart(chartData, this.container);

But the returned result is only a 0, am I missing something? Thank you!

stfrigerio commented 2 years ago

Hello!

I'm trying to do a very similar thing, and I've had some successess with the following code:

dv.header (2, 'Habits Counter');
const pages = dv.pages('"A 🔧/RealTime/2022/1° Trimestre/02 Febbraio/W05"')
.sort(p => p.file.link, 'asc')

const labels = pages.map(b=>b["day"]);

const data = pages.map(b=>b["ciga"]);

const chartData = {
    type: 'line',
    data: {
        labels: labels.values,
        datasets: [{
            label: 'Sigarette',
            data: data.values
            backgroundColor: [
                'rgba(255, 99, 132, 0.2)'
            ],
            borderColor: [
                'rgba(255, 99, 132, 1)'
            ],
            borderWidth: 1,
            tension: 0.2
        }]
    },
    options: {
     animations: {
        easing: 'linear',
    },
        scales: {
            y: {
                beginAtZero: true
            }
        }
    }
}

window.renderChart(chartData, this.container);

I have no knowledge of Javascript, so I'm not able to explain what I did. In fact I'm stuck in trying to add another dataset, if you find a way let me know cuz it's driving me crazy. Best of luck!

phibr0 commented 2 years ago

I would suggest to ask in the #dataview Channel of Obsidian's Community Discord.