Closed humbertodosreis closed 5 years ago
have you try ?
//AMD
window.ApexCharts = require('apexcharts');
console.log(window.ApexCharts); // should return modules
// or
// Imports
import ApexCharts from 'apexcharts';
window.ApexCharts = ApexCharts; // return apex chart
based on the apexcharts.js
in order to import it you need to define the variables for it first
and you need to expose it via window.ApexCharts
in order to use it outside webpack bundle
Thankss
will need to update the documentation regarding webpack bundle
the current require('apexcharts')
is worked if not using webpack (scope issue)
Thank you @KevinKP17 for pointing out the documentation mistake. It seems that unlike Chartkick, ApexCharts doesn't expose itself to window
by default. To be honest, I personally still use the good old sprockets for my app, therefore not very familiar with webpacker. I'm planning to migrate soon.
@KevinKP17, looking forward to your PR.
Edit: I've tested it on new rails 6 app, and @KevinKP17 solution works for me.
Thank you @KevinKP17 , I've tested and your solution worked. I believe that needs update the documentation.
Fixed by #47.
I'm getting this same error, with latest versions (apexcharts@3.8.5 and ng-apexcharts@1.0.5) as well as the suggested (apexcharts@3.6.8 and ng-apexcharts@1.0.2) using Angularjs1.7
Hi! I don't know how to use this gem with Angular. It's better to just use the ng-apexcharts.
Hi everybody !
I've tried @KevinKP17 , solution but it doesn't work for me. I'm trying to make 2 js files from a template working :
Let me explain : I have html file from a template that looks like this :
<html>
<head>
[...]
<!-- App css -->
<link href="assets/css/icons.min.css" rel="stylesheet" type="text/css" />
<link href="assets/css/app.min.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="col-xl-6">
<div class="card">
<div class="card-body" style="position: relative;">
<h4 class="header-title">Basic Bar Chart</h4>
<div id="basic-bar" class="apex-charts" style="min-height: 394px;">
<div id="apexchartsmm90i38y" class="apexcharts-canvas apexchartsmm90i38y" style="width: 698px; height: 380px;">
[...]
</div>
<!-- App js -->
<script src="assets/js/app.min.js"></script>
<!-- third party:js -->
<script src="assets/js/vendor/apexcharts.min.js"></script>
<!-- third party end -->
<!-- demo:js -->
<script src="assets/js/pages/demo.apex-bar.js"></script>
</body>
</html>
There is no need of app.min.js to make a charts work. When I open this html files in chrome, with the 2 css files and 2 js files only, it works perfect.
So,
in my rails6 app, I put the 2 css files in css folder and those 2 js files in packs folder and import apexcharts.min.js and demo.apex-bar.js in that order. I put alert("blabla") in those 2 files to make sure webpack is going in. And I always get the Apexcharts is not definded ! I tried with import "apexcharts" to import it from node module instead of apexcharts.min.js but when I go in demo.apex-bar.js, I still go the error. I also tried to put module.exports in apexcharts.min and import from in demo.apex-bar but it doesn't work.
It's been 4 days, 10 hours a day, I'm working on that and trying to understand how webpack compiles files but I can't figure it out.
If anybody know how to intergrate js files from template in webpack just let me know please !
Thanks everybody.
flip
Umm.. what does your problem have to do with this ruby gem? It seems like you don't use any ruby in the markup and use apexcharts.js directly. Maybe you want to ask around about your problem here.
Sorry, I thought you guys where talking about integrating apexcharts js file in webpack. I opened this page directly from google and didn't it was about the gem. Sorry about that.
I'm having the same problem Uncaught ReferenceError: ApexCharts is not defined
, but in my case using importmap
app/javascript/application.js
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
import "@tabler/core"
import "@hotwired/turbo-rails"
import "controllers"
import Cleave from "cleave.js"
import jQuery from "jquery"
import 'jquery.signaturepad'
import ApexCharts from 'apexcharts'
window.ApexCharts = ApexCharts
importmap.rb
...
pin "apexcharts", to: "https://ga.jspm.io/npm:apexcharts@3.37.3/dist/apexcharts.common.js", preload: true
Echoing @renancarvalhoo is there any documentation around how to set this up with importmaps?
@styd do know how to set this up for importmaps?
@fzf This is how I make it work with importmap:
wget -O vendor/javascript/apexcharts.esm.js https://ga.jspm.io/npm:apexcharts@3.53.0/dist/apexcharts.esm.js
config/importmap.rb
to local esm file (if you do No. 1):
pin "apexcharts", to: "apexcharts.esm.js"
or, to CDN URL
pin "apexcharts", to: "https://ga.jspm.io/npm:apexcharts@3.53.0/dist/apexcharts.esm.js"
app/javascript/application.js
import ApexCharts from "apexcharts"
window.ApexCharts = ApexCharts
module: true
For example:
<% series = [{
name: "Desktops",
data: [10, 41, 35, 51, 49, 62, 69, 91, 148]
}]
options = {
chart: {
height: 350,
zoom: {
enabled: false
}
},
dataLabels: {
enabled: false
},
stroke: {
curve: 'straight'
},
title: {
text: 'Product Trends by Month',
align: 'left'
},
grid: {
row: {
colors: ['#f3f3f3', 'transparent'],
opacity: 0.5
},
},
xaxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep'],
},
module: true
} %>
<%= line_chart(series, options) %>
Thank you @styd that worked, I can work on a pull request to update the readme.
@fzf Sure.
I've trying use ApexCharts in my RoR 6 app, but occurred the error below:
My Gemfile:
My
app/javascript/packs/application.js
My app is built in: Ruby 2.6.3 Rails 6 Webpacker