3

im trying to create a chart for my vue.js site using Apexcharts however my chart isnt appearing at all.

-Apexcharts documentation Here

HTML

  <div class="section sec2"> 
    <div id="chart"></div>
    {{chart}} <--failed attempt
  </div>
...

JavaScript

<script>
import ApexCharts from 'apexcharts'
export default {
  // name: 'HelloWorld',
  props: {//////////this is how i use global variable with vue.js///////////////
    width:{           type: String, default: function(){return('width:')}},
  }
}
var options = {
  chart: {
    type: 'bar'
  },
  series: [{
    name: 'sales',
    data: [30,40,45,50,49,60,70,91,125]
  }],
  xaxis: {
    categories: [1991,1992,1993,1994,1995,1996,1997, 1998,1999]
  }
}

var chart = new ApexCharts(document.querySelector("#chart"), options);

chart.render();

</script>

CSS

<style>
  @import '../assets/style/sec.css';
#chart {
  max-width: 650px;
  margin: 35px auto;
}

</style>

i tried using the vue.js global variable by turning "var option = ..." into "option:...", but it only gave me an error.

im pretty sure its supossed to show in the div with the "#chart" id

any helpand or advice is greatly appreciated, thank you.

1 Answer 1

6

EDIT: This answer was written when vue-apexcharts wrapper was not released. If you're looking for a better example on Vue integration with ApexCharts, check out https://github.com/apexcharts/vue-apexcharts


ApexCharts documentation currently doesn't have an example on how to use it with Vue/React, therefore I will try to provide a simple demo to use ApexCharts in Vue.js.

Here is the HTML Part

<div id="app">
    <div id="chart" ref="barchart"></div>
</div>

and here is the JS part

new Vue({
  el: '#app',
  mounted: function() {
    const chart = new ApexCharts(this.$refs.barchart, {
      chart: {
        type: 'bar',
        height: 400
      },
      series: [{
        name: 'sales',
        data: [30,40,45,50,49,60,70,91,125]
      }],
      xaxis: {
        categories: [1991,1992,1993,1994,1995,1996,1997, 1998,1999]
      }
    })

    chart.render();

  }
});

ApexCharts just require a reference to the DOM element, whether it is in Vue/React to render the chart on the screen. In the above code, I have referenced the DOM element via this.$refs.

Here is the codepen link for the example I gave above

Sign up to request clarification or add additional context in comments.

3 Comments

i replaced my code with yours to fix it and it gave me a couple errors, i added the ' import Vue from 'vue/dist/vue.js' ' import and it fix most of them but now its giving me this error in the chrome console that is posted in the bellow coment
Uncaught (in promise) TypeError: Cannot read property 'firstChild' of undefined
Your answer saved my day!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.