我的line-chart
遇到问题。来自api /控制器的数据无法在我的图表上传输,但作为响应显示我的所有数据
如图所示,line-chart
是空的,但是在response
中有一个数据。
我说错了
TypeError::无法读取未定义的属性“ map” 在app.js:86744
我不知道如何解决它,请帮助我。谢谢
Report.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();
答案 0 :(得分:1)
您的问题是您的API调用是异步的。因此,有可能在数据到达之前将图表呈现出来。
您需要确保数据在那里。
最简单的方法是在图表组件上使用loaded
状态和v-if
。
您可以在文档中查看示例:https://vue-chartjs.org/guide/#chart-with-api-data
答案 1 :(得分:0)
根据返回的响应,处理数据时会出现一些错误。
.then(response => {
// let results = response.data.data <- does not exist
let results = response.data
// These keys do not exist on your response date_input, number_of_morality, chicken_age
// 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: 'Manually Name Labels', // this.number_of_morality, <- This does not exist on your response
datasets:[{
label: 'Mortality',
backgroundColor: '#f87979',
data: response.data // this.date_input <- This did not exist on your response
}]
}
})
这应该渲染一些东西。但是,要正确解决此问题,我需要查看Laravel中Mortality模型的数据库架构。
您可以尝试将控制器更新为:
公共功能index()
{
$mortality = Mortality::select('number_of_mortality','date_input','chicken_age')->where('cycle_id', 2)
->where(['user_id' => Auth::id()]);
return response()->json($mortality);
}
但是同样,在没有看到架构的情况下,我不确定是否可以解决问题。如果您进行了更改,请在chrome开发工具中发布更新后的响应结果
答案 2 :(得分:0)
您没有在reportMortalityLineChart.js中安装道具 示例:
props: {
// eslint-disable-next-line vue/require-default-prop
chartData: {
type: Array,
required: false
},
chartLabels: {
type: Array,
required: true
}
},
mounted () {
this.renderChart({
labels: this.chartLabels,
datasets: [
{
label: 'downloads',
borderColor: '#249EBF',
pointBackgroundColor: 'rgba(0,0,0,0)',
borderWidth: 1,
data: this.chartData
}
]
}, this.options)
}