【问题标题】:vue-chartjs unable to render line chartvue-chartjs 无法渲染折线图
【发布时间】:2021-05-07 06:30:55
【问题描述】:

MyChart.vue

<template>
  <v-container>
    <v-card elevation="2">
      <v-card-title>AQI Comparison</v-card-title>
      <line-chart :chart-data="datacollection"></line-chart>
    </v-card>
  </v-container>
</template>

<script>
import LineChart from "./LineChart.js";

export default {
  name: "AQIChartComponent",
  components: {
    LineChart
  },
  data() {
    return {
      datacollection: {
        labels: [
          "week 1",
          "week 2",
          "week 3",
          "week 4",
          "week 5",
          "week 6",
          "week 7",
          "week 8",
          "week 9",
          "week 10",
        ],
        datasets: [
          {
            data: [86, 114, 106, 106, 107, 111, 133, 221, 783, 2478],
            label: "Africa",
            borderColor: "#3e95cd",
            fill: false,
          },
          {
            data: [282, 350, 411, 502, 635, 809, 947, 1402, 3700, 5267],
            label: "Asia",
            borderColor: "#8e5ea2",
            fill: false,
          },
          {
            data: [168, 170, 178, 190, 203, 276, 408, 547, 675, 734],
            label: "Europe",
            borderColor: "#3cba9f",
            fill: false,
          },
          {
            data: [40, 20, 10, 16, 24, 38, 74, 167, 508, 784],
            label: "Latin America",
            borderColor: "#e8c3b9",
            fill: false,
          },
          {
            data: [6, 3, 2, 2, 7, 26, 82, 172, 312, 433],
            label: "North America",
            borderColor: "#c45850",
            fill: false,
          },
        ],
      }
    };
  }
};
</script>

LineChart.js

import { Line, mixins } from 'vue-chartjs'
const { reactiveProp } = mixins

export default {
  extends: Line,
  mixins: [reactiveProp],
  props: ['options'],
  mounted () {
    // this.chartData is created in the mixin.
    // If you want to pass options please create a local options object
    this.renderChart(this.chartData, this.options)
  }
}

我收到以下错误:

[Vue warn]: Error in mounted hook: "TypeError: chart_js__WEBPACK_IMPORTED_MODULE_0__.default is not a constructor"

found in

---> <LineChart>
       <VCard>
         <AQIChartComponent> at src/components/AQIChartComponent.vue
           <AQIComponent> at src/components/AQIComponent.vue
             <VMain>
               <VApp>
                 <App> at src/App.vue
                   <Root>

TypeError: chart_js__WEBPACK_IMPORTED_MODULE_0__.default is not a constructor

我什至将属性从 :chart-data 更改为 :chartData 但同样的错误。 我正在使用他们documentation 中的示例代码,我在这里做错了什么?

【问题讨论】:

    标签: javascript vue.js vue-chartjs


    【解决方案1】:

    从报错来看,我认为是vue-chartjs和chart.js不兼容的问题

    vue-chartjs 自 2020 年 12 月以来未更新。

    您可以尝试将 chart.js 降级到 2.9.4 吗?我认为它会解决构造函数的问题。

    "chart.js": "2.9.4",
    "vue-chartjs": "3.5.1",
    

    我在codeandbox FYR 中使用您提供的代码创建了一个小示例。

    https://codesandbox.io/s/distracted-galileo-r3lec?file=/package.json:378-401


    附言

    如果您想要更新图表库,可以查看顶点图表 https://github.com/apexcharts/vue-apexcharts

    【讨论】:

    • 这解决了大约一个小时的碰壁 - vue-chartjs 文档应该真正指出这一点。谢谢!
    【解决方案2】:

    首先,将您的chart.js 版本更新为2。您可以通过执行以下命令来做到这一点。

    npm install chart.js@2
    

    将图表选项与数据一起发送。像这样。

    <template>
      <v-container>
        <v-card elevation="2">
          <v-card-title>AQI Comparison</v-card-title>
          <line-chart
            :chart-data="datacollection"
            :options="chartOptions"
          ></line-chart>
        </v-card>
      </v-container>
    </template>
    

    并在您的脚本中将图表选项数据与datacollection 变量一起添加。就这样。

    <script>
    import LineChart from "./LineChart.js";
    
    export default {
      name: "AQIChartComponent",
      components: {
        LineChart,
      },
      data() {
        return {
          datacollection: {
            labels: [
              "week 1",
              "week 2",
              "week 3",
              "week 4",
              "week 5",
              "week 6",
              "week 7",
              "week 8",
              "week 9",
              "week 10",
            ],
            datasets: [
              {
                data: [86, 114, 106, 106, 107, 111, 133, 221, 783, 2478],
                label: "Africa",
                borderColor: "#3e95cd",
                fill: false,
              },
              {
                data: [282, 350, 411, 502, 635, 809, 947, 1402, 3700, 5267],
                label: "Asia",
                borderColor: "#8e5ea2",
                fill: false,
              },
              {
                data: [168, 170, 178, 190, 203, 276, 408, 547, 675, 734],
                label: "Europe",
                borderColor: "#3cba9f",
                fill: false,
              },
              {
                data: [40, 20, 10, 16, 24, 38, 74, 167, 508, 784],
                label: "Latin America",
                borderColor: "#e8c3b9",
                fill: false,
              },
              {
                data: [6, 3, 2, 2, 7, 26, 82, 172, 312, 433],
                label: "North America",
                borderColor: "#c45850",
                fill: false,
              },
            ],
          },
          chartOptions: {
            responsive: true,
            legend: {
              display: false,
            },
            tooltips: {
              titleFontSize: 20,
              bodyFontSize: 25,
            },
            scales: {
              xAxes: [],
              yAxes: [
                {
                  ticks: {
                    beginAtZero: false,
                  },
                },
              ],
            },
          },
        };
      },
    };
    </script>
    

    【讨论】:

    • 没用。我遇到了同样的错误。
    • 你的 package.json 文件中chart.js 的版本是什么?它必须是chart.js@2。可以通过npm install vue-chartjs chart.js@2安装。
    • chart.js 是 3.2.1 而 vue-chartjs 是 3.5.1
    • 所以将chart.js 版本更改为2。这就是问题所在。并重新安装您的依赖项。
    • 谢谢,降级到 v2 以便 chart.js 工作。
    猜你喜欢
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    • 2015-08-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2019-01-03
    相关资源
    最近更新 更多