在开发者工具上调试不报错,真机调试就报错,弄了半天也整不明白,不太懂js,有大佬懂吗?
import * as echarts from '../../ec-canvas/echarts';
function line_set(chart, xdata, ydata) {
var option = {
xAxis: {
data: xdata,
},
series: [{
data: ydata,
}]
};
chart.setOption(option);
}
Page({
data: {
ec: {
lazyLoad: true
},
timer: ''
},
onLoad: function (options) {
var _this = this;
this.setData({
timer: setInterval(function () {
_this.getOption();
}, 2000)
});
},
onReady: function () {
this.oneComponent = this.selectComponent('#mychart-dom-line');
// 初始化图表
this.init_chart([], []);
this.onLoad();
},
onUnload: function () {
clearInterval(this.data.timer);
},
init_chart: function (xdata, ydata) {
this.oneComponent.init((canvas, width, height, dpr) => {
const chart = echarts.init(canvas, null, {
width: width,
height: height,
devicePixelRatio: dpr
});
// 初始的 option 对象
var option = {
title: {
text: '温度随时间变化图',
left: 'center'
},
color: ["#37A2DA", "#67E0E3", "#9FE6B8"],
grid: {
containLabel: true
},
tooltip: {
show: true,
trigger: 'axis'
},
xAxis: {
name: '日期',
axisLabel: {
interval: 0,
rotate: 40
},
type: 'category',
boundaryGap: false,
data: xdata
},
yAxis: {
name: '温度/℃',
x: 'center',
type: 'value',
splitNumber: 13,
min: 0,
max: 26,
splitLine: {
lineStyle: {
type: 'dashed'
}
},
show: true
},
series: [{
name: '温度',
type: 'line',
smooth: true,
data: ydata
}]
};
chart.setOption(option);
this.chart = chart;
return chart;
});
},
getOption: function () {
var _this = this;
wx.request({
url: 'https://apis.bemfa.com/va/getmsg',
method: 'GET',
data: {
uid: "1ec3897dd9d650c1fc3c961cddc62b68",
topic: "esp32",
type: 1,
num: 10
},
header: {
"Content-Type": "application/json"
},
success: function (res) {
console.log(res);
_this.setData({
data: res.data,
})
var temperature = [];
var date = [];
var j=0;
for (var i = res.data.data.length-1; i >= 0; i--) {
temperature[j] = res.data.data[i].msg.split("#")[7];
date[j] = res.data.data[i].time.slice(11, 16);
j++;
}
// 只更新折线图的数据,而不重新初始化整个图表
line_set(_this.chart, date, temperature);
},
})
},
})