【问题标题】:Can't render chart, data from api无法渲染图表,来自 api 的数据
【发布时间】:2020-08-03 07:36:36
【问题描述】:

我从 api 获取数据。我可以显示图表数据,但不能呈现图表。 我的代码:

<script>
import { Pie, mixins } from 'vue-chartjs'
import axios from 'axios'
export default {
mixins: [mixins.reactiveData],
extends: Pie,
data() {
return {
  chartdata: ''
 }
},
mounted () {
this.renderChart(this.chartdata, this.options)
},

created() {
axios.get("http://localhost:3000/chart1")
.then(response => {
  const responseData = response.data
  
  this.chartdata = {
    labels: responseData.map(item => item.lb),
    datasets: 
      [
          {
            label: 'hrlo',
            backgroundColor: ['purple','#b2b2b2'],
            data: responseData.map(item => item.dt)
          }
      ]
  } 
 })
  .catch( e => {
    this.errors.push(e)
  })

 },  
}
</script>

API:[ { "lb": "安卓", “dt”:80 }, { “磅”:“IOS”, “dt”:20 } ]

对不起,因为我的英语不好。

【问题讨论】:

    标签: api vue-chartjs


    【解决方案1】:

    您还可以包含任何错误消息吗?我怀疑“chartdata”的错误数据结构甚至在检索要显示的数据之前导致错误,即“chartdata”需要是

    chartdata: {
        labels: [],
        datasets: []
    }
    

    【讨论】:

    • 感谢您的回答。但我不这么认为。因为我可以显示图表数据。我的代码:
      {{ item }}
      和结果: [ "Android", "IOS" ] [ { "label" : "hrlo", "backgroundColor": [ "purple", "#b2b2b2" ], "data": [ 80, 20 ] } ] 但我无法渲染此图表数据
    • @HiếuLêVăn 感谢您的澄清。模块是否返回任何错误消息?
    • 没有hav错误。 t可以解决。获取数据后我需要重新渲染@@
    猜你喜欢
    • 2021-03-11
    • 2016-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-01
    • 2020-06-28
    • 2018-09-22
    相关资源
    最近更新 更多