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
610 stars 28 forks source link

[Bug]: Graphs don't generate in custom dataviewjs views #126

Closed ThePolyBlank closed 2 months ago

ThePolyBlank commented 4 months ago

Describe the bug

When putting graph code in a js file and loading it with dv.view("scriptname"), the graph does not generate when it should.

Relevant errors (if available)

No response

Steps to reproduce

  1. Save a script with valid dataviewjs graph code
  2. load the script using dv.view(scriptname)

Expected Behavior

The graph shows up

Additional context

the same behavior accours when using eval()

Code

const chartData = {
    type: 'line',
    data: {
        labels: [1,2,3,4,5,6,7],
        datasets: [
            {
                label: 'Sample',
                data: [1,2,3,4,5,6,7],
                fill: true,
                backgroundColor: [
                    'rgba(255, 99, 132, 0.2)'
                ],
                borderColor: [
                    'rgba(255, 99, 132, 1)'
                ],
                borderWidth: 2,
            }
        ]
    }
}
window.renderChart(chartData, this.container)

Operating system

Windows

McArdellje commented 2 months ago

Your issue is with using this inside of a view, which behaves the same as a function, where the this object is not the same as the one given to the dataviewjs block, causing it to be unable to read this.container the fix is simply change your call to dv.view(scriptname, this) and change the code in the script file to use input.container. this will pass in the dataviewjs's this into the script as a variable called input so that is can get the container from it