想问一下 怎么把这些写成很简便的代码,并且有效在echars输出相对应得X轴,Y轴。主要想优化一下查询数据库代码 但有点困难
$X_data = ["TK5-01","TK5-02","TK5-03","TK5-04","TK5-05"];
foreach ($X_data as $key => $value) {
$result = DB::table('instruments_tuokong')->select('variation')->orderBy('create_time', 'desc')->where('code', $value)->first();
$Y_data[] = $result->variation;
//var_dump($Y_data);die;
$A_data = ["TK5-06","TK5-07","TK5-08","TK5-09","TK5-10"];
foreach ($A_data as $key => $value) {
$result = DB::table('instruments_tuokong')->select('variation')->orderBy('create_time', 'desc')->where('code', $value)->first();
$B_data[] = $result->variation;
}
$E_data = ["TK5-11","TK5-12","TK5-13","TK5-14"];
foreach ($E_data as $key => $value) {
$result = DB::table('instruments_tuokong')->select('variation')->orderBy('create_time', 'desc')->where('code', $value)->first();
$F_data[] = $result->variation;
}
$G_data = ["TK5-15","TK5-16"];
foreach ($G_data as $key => $value) {
$result = DB::table('instruments_tuokong')->select('variation')->orderBy('create_time', 'desc')->where('code', $value)->first();
$H_data[] = $result->variation;
}
$I_data = ["TK5-17","TK5-18"];
foreach ($I_data as $key => $value) {
$result = DB::table('instruments_tuokong')->select('variation')->orderBy('create_time', 'desc')->where('code', $value)->first();
$J_data[] = $result->variation;
}
$K_data = ["TK5-19"];
foreach ($K_data as $key => $value) {
$result = DB::table('instruments_tuokong')->select('variation')->orderBy('create_time', 'desc')->where('code', $value)->first();
$L_data[] = $result->variation;
}
这是我的echars表
(function() {
var X_data = '{{$X_data}}';
var Y_data = '{{$Y_data}}';
var X = X_data.replaceAll('"', '"');
var Y = Y_data.replaceAll('"', '"');
var C = JSON.parse(X);
var D = JSON.parse(Y);
console.log(C);
console.log(D);
//绕坝渗压变化率
var chartDom2 = document.getElementById('col01');
var myChart2 = echarts.init(chartDom2);
var option2;
option2 = {
title: {
text: '',
textStyle: {
color: '#597de3',
fontSize: 15
},
padding: [20, 50, 0, 20]
},
tooltip: {
trigger: 'axis'
},
legend: {
// data: ['库水位高程:3470m'],
right: '30',
// top:'-30%',
icon: [],
padding: [0, 0],
textStyle: {
color: '#597de3'
},
},
// color:['#db9715','#15c594'],
grid: {
left: '9%',
right: '6%',
// top:'3%',
margin:'50%',
containLabel: true
},
xAxis: {
type: 'category',
name: '',
boundaryGap: true,
axisLabel:{
rotate:40,
},
axisLine: {
show: true, // Y轴线
lineStyle: {
type: 'solid',
color: '#fff'
}
},
data: C
},
yAxis: {
type: 'value',
name: '(mm)',
nameTextStyle: {
padding: [0, 10, -5, 10] // 上右下左与原位置距离
},
axisLabel: {
formatter: '{value}',
rotate:40,
},
axisLine: {
show: true, // Y轴线
lineStyle: {
type: 'solid',
color: '#fff'
}
},
splitLine: {
lineStyle: {
type: 'dashed',
color: '#fff',
// color: ['#aaa','#fff'],
width: 0.6,
}
},
},
series: [{
// name: '库水位高程:3470m',
type: 'line',
tooltip: {
valueFormatter: function(value) {
return value + ' ';
}
},
smooth: true,
symbol: 'circle',
symbolSize: 10,
lineStyle: {
normal: {
width: 2,
color:"#00ff00", //设置实线的颜色
},
},
data: D
},
],
textStyle: {
// color: '#597DE3'
color:"#fff",
}
};
myChart2.resize({
height: 410,
// top: -23,
width:529
});
option2 && myChart2.setOption(option2,true);
})();