【发布时间】:2018-12-07 23:54:11
【问题描述】:
我的line-chart 有问题。来自 api/controller 的数据无法在我的图表上传输,但作为响应显示了我的所有数据
如图所示,line-chart 为空,但response 中有数据。
据说有错误
TypeError: 无法读取未定义的属性“地图” 在 app.js:86744
我不知道如何解决它,请帮助我。谢谢
代码
报告.Vue
<div class="container">
<h1>USAGE</h1>
<mortalityLineChart :chartdata="datacollection"> </mortalityLineChart>
</div>
</template>
<script>
export default {
data () {
return {
date_input: null,
number_of_morality: null,
chicken_age:null,
datacollection: null
}
},
created () {
this.fillData()
},
mounted () {
this.fillData()
},
methods: {
fillData () {
axios.get('/api/report')
.then(response =>{
let results = response.data.data
let dateresult = results.map(a => a.date_input)
let mortalityresult = results.map(a => a.number_of_morality)
let chickenageresult = results.map(a => a.chicken_age)
this.date_input = dateresult
this.number_of_morality= mortalityresult
this.chicken_age= chickenageresult
this.datacollection = {
labels: this.number_of_morality,
datasets:[
{
label:'Mortality',
backgroundColor: '#f87979',
data:this.date_input
}
]
}
})
.catch(error => {
console.log(error)
})
},
}
}
</script>
reportMortalityLineChart.js
import {Line} from 'vue-chartjs' // We specify what type of chart we want from vue-chartjs and the mixins module
export default({
extends: Line, //We are extending the base chart class as mentioned above
props: ["mychartData"],
watch: {
mychartData: {
handler: function (val) {
this._chart.update()
},
deep: true
}
},
data () {
return {
options: { //Chart.js options
scales: {
yAxes: [{
ticks: {
beginAtZero: true
},
gridLines: {
display: true
}
}],
xAxes: [ {
gridLines: {
display: false
}
}]
},
legend: {
display: true
},
responsive: true,
maintainAspectRatio: false
}
}
},
mounted () {
// this.chartData is created in the mixin
this.renderChart(this.chartData, this.options)
}
})
App.js
import reportMortalityLineChart from './charts/reportMortalityLineChart'
Vue.component('mortalityLineChart',reportMortalityLineChart);
预览
2017-12-10: 4
2017-12-11: 12
2017-12-12: 17
2017-12-13: 16
2017-12-14: 17
2017-12-15: 10
2017-12-16: 11
2017-12-17: 13
2017-12-18: 6
2017-12-19: 1
2017-12-20: 13
2017-12-21: 16
2017-12-22: 4
2017-12-23: 10
2017-12-24: 10
2017-12-25: 10
2017-12-26: 19
2017-12-27: 5
2017-12-28: 7
2017-12-29: 13
2017-12-30: 13
2017-12-31: 10
2018-01-01: 13
2018-01-02: 23
2018-01-03: 15
2018-01-04: 35
2018-01-05: 61
ReportController.php
public function index()
{
$mortality = Mortality::where('cycle_id', 2)
->where(['user_id' => Auth::id()])
->pluck('number_of_mortality','date_input','chicken_age');
return response()->json($mortality);
}
死亡率表
Schema::create('mortalities', function (Blueprint $table) {
$table->increments('id');
$table->date('date_input');
$table->integer('number_of_mortality');
$table->integer('chicken_age');
$table->mediumText('cause_of_death')->nullable();
$table->unsignedInteger('cycle_id');
$table->unsignedInteger('user_id');
$table->timestamps();
【问题讨论】:
-
response.data.data未定义。查看开发工具以查看实际响应数据 -
他们是空先生。我该如何解决?
-
您能发布服务器的完整响应吗?另一方面,除非您在响应中有数据的对象键,否则您需要将
let results = response.data.data更新为let results = response.data -
我在哪里可以找到完整的回复?在 api/控制器? @TimWickstrom.com
-
在您的 chrome devtools 图片中,“报告”突出显示,并且您可以预览响应。只需从 chrome 的“响应”面板中选择所有内容,然后将其粘贴到您的问题中。
标签: vue.js chart.js axios vue-chartjs