Issue #2122Opened July 8, 2019by zyxnowell1 reactions

[Question] Adding script to content with chart js

Question

Hello I am trying to add a Graph JS block with this code

editor.BlockManager.add('test-chart-block', {
        label: 'Test Chart block',
        attributes: {class: 'fa fa-text'},
        content: {
          content: `<canvas id="bar-chart" width="800" height="450"></canvas>`,
          script:`new Chart(document.getElementById("bar-chart"), {
            type: 'bar',
            data: {
              labels: ["Africa", "Asia", "Europe", "Latin America", "North America"],
              datasets: [
                {
                  label: "Population (millions)",
                  backgroundColor: ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9","#c45850"],
                  data: [2478,5267,734,784,433]
                }
              ]
            },
            options: {
              legend: { display: false },
              title: {
                display: true,
                text: 'Predicted world population (millions) in 2050'
              }
            }
        });`
        }
      });

but whenever I would drag the new block I would get an error: image

How do I add a script to a content to generate the chart? Also are third party scripts allowed in GrapesJS or are there properties that needs do be defined in initializing the editor?

Thank you!

Answers (1)

zyxnowellJuly 8, 20191 reactions

I just figured it out, you just need to declare your custom JS in the init method and set allowSripts to true

grapesjs.init({
        . . . .
        allowScripts: 1,
        canvas: {
          scripts: [
            "https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.js"
          ],
          styles: [
            "https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.css"
          ]
        }
       . . . .
      });

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.