pacuna / frappe_charts

Frappé Charts Ruby gem for Rails.
MIT License
45 stars 3 forks source link

rails5 error: "No parent element to render on was provided" #1

Closed 7stud closed 6 years ago

7stud commented 6 years ago

It's not clear to me how to setup frappe_charts in rails5. I followed the instructions, but I don't see a chart. I put the following div in one of my views:

<div>Should see a chart:</div>
<div id="chart"></div>

Then I used the following chart code taken from https://frappe.github.io/charts/

  // Javascript
  let data = {
    labels: ["12am-3am", "3am-6am", "6am-9am", "9am-12pm",
      "12pm-3pm", "3pm-6pm", "6pm-9pm", "9pm-12am"],

    datasets: [
      {
        title: "Some Data",
        values: [25, 40, 30, 35, 8, 52, 17, -4]
      },
      {
        title: "Another Set",
        values: [25, 50, -10, 15, 18, 32, 27, 14]
      },
      {
        title: "Yet Another",
        values: [15, 20, -3, -15, 58, 12, -17, 37]
      }
    ]
  };

  let chart = new Chart({
    parent: "#chart", // or a DOM element
    title: "My Awesome Chart",
    data: data,
    type: 'bar', // or 'line', 'scatter', 'pie', 'percentage'
    height: 250,

    colors: ['#7cd6fd', 'violet', 'blue'],
    // hex-codes or these preset colors;
    // defaults (in order):
    // ['light-blue', 'blue', 'violet', 'red',
    // 'orange', 'yellow', 'green', 'light-green',
    // 'purple', 'magenta', 'grey', 'dark-grey']

    format_tooltip_x: d => (d + '').toUpperCase(),
    format_tooltip_y: d => d + ' pts'
  });

I put that js in a file called myjs.js, and I put myjs.js inside app/assets/javascripts.

The Web Inspector for Safari 11.0.3 shows the following error:

[Error] No parent element to render on was provided.
    value (frappe-charts.min.iife.self-b8ef73bbef29618b2d28f4d7e48ce94a22c177817581f96876040e05b37b8cab.js:1:20163)
    e (frappe-charts.min.iife.self-b8ef73bbef29618b2d28f4d7e48ce94a22c177817581f96876040e05b37b8cab.js:1:38230)
    F (frappe-charts.min.iife.self-b8ef73bbef29618b2d28f4d7e48ce94a22c177817581f96876040e05b37b8cab.js:1:6672)
    t (frappe-charts.min.iife.self-b8ef73bbef29618b2d28f4d7e48ce94a22c177817581f96876040e05b37b8cab.js:1:54102)
    Global Code (myjs.self-7669f6d58e40406caac05410603527cf209d9b31a7f72ac6e0438ad1b74b22ba.js:23)

Line 23 in myjs.js is:

let chart = new Chart({

app/assets/config/manifest.js:

//= link_tree ../images
//= link_directory ../javascripts .js
//= link_directory ../stylesheets .css

apps/assets/javascripts/application.js:

//= require rails-ujs
//= require turbolinks
//= require frappe-charts.min.iife
//= require_tree .

app/assets/stylesheets/application.js:

/*
...
...
 *= require_tree .
 *= require_self
 *= require frappe-charts.min
*/ 

Gemfile:

source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end

#Automatic gemset switching for rvm:
ruby '2.4.0'
#ruby-gemset=demo1_gemset

#####
gem 'frappe_charts'
gem 'jquery-rails'
####
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.4'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use Puma as the app server
gem 'puma', '~> 3.7'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '~> 2.13'
  gem 'selenium-webdriver'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

Is the error saying that the DOM hasn't finished loading by the time the js executes so frappe_charts can't find the div? Okay, to test that I tried adding a script tag to my app/views/layouts/application.html.erb:

  <body>
    <%= yield %>
    <script src="https://unpkg.com/frappe-charts@0.0.8/dist/frappe-charts.min.iife.js"></script>
  </body>

and I removed //= require frappe-charts.min.iife in app/assets/javascripts/application.js. Same error.

For a final attempt, I copied the js at the cdn and pasted it in a file and put the file in app/assets/javascripts; and then I coped the css file at the cdn and pasted it in a file and put the file in app/assets/stylesheets; then I deleted the frappe requires from the application.jss and application.css files. Same error.

Not the final attempt. I read a tutorial on yarn and rails5, so I tried installing frappe-charts with yarn. Same error.

Souravirus commented 6 years ago

I am also having a problem running the same javascript and html code but my error is Failed to create chart: can't acquire context from the given item. I have also replaced the code<div id="chart"></div> with <canvas id="chart"></canvas> but still the error persists.

pacuna commented 6 years ago

@7stud @Souravirus did you try executing your code after the DOM is loaded?

document.addEventListener('DOMContentLoaded', function(){ 
    // your code goes here
}, false);

If you're using turbolinks it may be better to use

document.addEventListener('turbolinks:load', function(){ 
    // your code goes here
}, false);

All this assuming you don't have jquery installed. If you do just use $(document).ready(function(){ /* ... */ });

Souravirus commented 6 years ago

It actually worked by adding the cdn of the frappe charts @pacuna