【问题标题】:Apexcharts bar chart not appearing in Vue.js projectApexcharts 条形图未出现在 Vue.js 项目中
【发布时间】:2019-01-09 10:45:03
【问题描述】:

我正在尝试使用 Apexcharts 为我的 vue.js 网站创建一个图表,但是我的图表根本没有出现。

-Apexcharts 文档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>

我尝试通过将“var option = ...”转换为“option:...”来使用 vue.js 全局变量,但它只给了我一个错误。

我很确定它应该以“#chart”id 显示在 div 中

非常感谢任何帮助和建议,谢谢。

【问题讨论】:

    标签: javascript vue.js charts apexcharts


    【解决方案1】:

    编辑:这个答案是在未发布 vue-apexcharts 包装器时编写的。如果您正在寻找 Vue 与 ApexCharts 集成的更好示例,请查看https://github.com/apexcharts/vue-apexcharts


    ApexCharts 文档目前没有关于如何将其与 Vue/React 一起使用的示例,因此我将尝试提供一个简单的演示来在 Vue.js 中使用 ApexCharts。

    这是 HTML 部分

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

    这里是 JS 部分

    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 只需要对 DOM 元素的引用,无论是在 Vue/React 中都可以在屏幕上呈现图表。在上面的代码中,我通过 this.$refs 引用了 DOM 元素。

    这是我上面给出的示例的codepen链接

    【讨论】:

    • 我用你的代码替换了我的代码来修复它,它给了我一些错误,我添加了'import Vue from 'vue/dist/vue.js''导入并且它修复了大部分但是现在它在下面的评论中发布的 chrome 控制台中给了我这个错误
    • Uncaught (in promise) TypeError: Cannot read property 'firstChild' of undefined
    • 工作。谢谢!
    • 你的回答拯救了我的一天!
    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-11
    相关资源
    最近更新 更多