无法用vue-charts绘制散点图

Unable to draw a scatter chart with vue-charts

我最终得到的是空白页和错误

TypeError: Cannot read property 'getBasePixel' of undefined

页面上没有图表。我是 vue.js 的新手,所以可能完全使用错误,但我尝试主要使用 vue-chartjs 页面中的演示。和 chart.js 页。好像我在某个地方搞砸了,但看不到哪里。有人报告 chrome。如有任何帮助,我们将不胜感激。尝试引入一些数据流..2 准确地说....

CHARTS.JS

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

export default {
  extends: Scatter,
  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)
  }
}

Randomchart.vue*

<template>
  <div class="small">
    <scatter :chart-data="datacollection" :chart-options="options"></scatter>
    <button @click="fillData()">Randomize</button>
  </div>
</template>

<script>
  import Scatter from '@/components/Chart1.js'

  export default {
    components: {
      Scatter
    },
    data () {
      return {
        datacollection: null,
        options: null
      }
    },
    mounted () {
      this.fillData()
    },
    methods: {
      fillData () {
          console.log("firing phiil");
        this.datacollection = {
            datasets: [{
            label: 'My First dataset',
            xAxisID: 'x-axis-1',
            yAxisID: 'y-axis-1',
            borderColor: 'rgba(47, 152, 208, 0.2)',
            backgroundColor: [
                                'rgba(47, 152, 208, 0.2)',
                            ],
            data: [{
                x: randomScalingFactor(),
                y: randomScalingFactor(),
            }, {
                x: randomScalingFactor(),
                y: randomScalingFactor(),
            }, {
                x: randomScalingFactor(),
                y: randomScalingFactor(),
            }, {
                x: randomScalingFactor(),
                y: randomScalingFactor(),
            }, {
                x: randomScalingFactor(),
                y: randomScalingFactor(),
            }, {
                x: randomScalingFactor(),
                y: randomScalingFactor(),
            }, {
                x: randomScalingFactor(),
                y: randomScalingFactor(),
            }]
        }, {
            label: 'My Second dataset',
            xAxisID: 'x-axis-1',
            yAxisID: 'y-axis-2',
            borderColor: 'rgba(0, 0, 208, 0.2)',
            backgroundColor: [
                                'rgba(47, 152, 208, 0.2)',
                            ],
            data: [{
                x: randomScalingFactor(),
                y: randomScalingFactor(),
            }, {
                x: randomScalingFactor(),
                y: randomScalingFactor(),
            }, {
                x: randomScalingFactor(),
                y: randomScalingFactor(),
            }, {
                x: randomScalingFactor(),
                y: randomScalingFactor(),
            }, {
                x: randomScalingFactor(),
                y: randomScalingFactor(),
            }, {
                x: randomScalingFactor(),
                y: randomScalingFactor(),
            }, {
                x: randomScalingFactor(),
                y: randomScalingFactor(),
            }]
        }]
    }
        console.log(this.datacollection);
      }

}
  }

 function randomScalingFactor () {
        return Math.round(Math.random(-100, 100));
    }
</script>

<style>
  .small {
    max-width: 600px;
    margin:  150px auto;
  }
</style>

问题在于额外的数据集参数 xAxisIDyAxisID

似乎它们设置不正确,所以当 vue-charts 尝试获取对那些 DOM 元素的引用时,它得到 undefined,因此出现错误。

您应该删除这些参数,或者添加这些元素(通过选项或手动)

Here is a working version of the app

PS:另外,如果需要从区间[-100, 100]中获取随机数,应该使用Math.round(Math.random() * 200) - 100。 在您的版本中,它总是 returns 1 或 0(因为 Math.random 不带任何参数并且总是 returns 介于 1 和 0 之间的值)