PHP查询数据库渲染前端echars

想优化一下查询数据库的代码 应该有更简便的代码

 $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;
        }
return view("index/safty/planes/".$type."_".$module."_".$action."_".$plane, compact('left','right'))->with('data', [])->with('instruments_name', 'xxxxx')->with('X_data', json_encode($X_data, true))->with('Y_data', json_encode($Y_data, true))->with('A_data', json_encode($A_data, true))->with('B_data', json_encode($B_data, true))->with('E_data', json_encode($E_data, true))->with('F_data', json_encode($F_data, true))->with('G_data', json_encode($G_data, true))->with('H_data', json_encode($H_data, true))->with('I_data', json_encode($I_data, true))->with('J_data', json_encode($J_data, true))->with('K_data', json_encode($K_data, true))->with('L_data', json_encode($L_data, true))

输出到echars

 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);

        })();  

基于Monster 组和GPT的调写:

将重复的查询逻辑提取出来,并使用一个循环来处理不同的数据集。以下是优化后的代码示例:

$dataSets = [
    ["X_data" => ["TK5-01","TK5-02","TK5-03","TK5-04","TK5-05"], "Y_data" => []],
    ["X_data" => ["TK5-06","TK5-07","TK5-08","TK5-09","TK5-10"], "Y_data" => []],
    ["X_data" => ["TK5-11","TK5-12","TK5-13","TK5-14"], "Y_data" => []],
    ["X_data" => ["TK5-15","TK5-16"], "Y_data" => []],
    ["X_data" => ["TK5-17","TK5-18"], "Y_data" => []],
    ["X_data" => ["TK5-19"], "Y_data" => []]
];

foreach ($dataSets as $key => $dataSet) {
    $X_data = $dataSet["X_data"];
    $Y_data = &$dataSet["Y_data"];

    foreach ($X_data as $value) {
        $result = DB::table('instruments_tuokong')->select('variation')->orderBy('create_time', 'desc')->where('code', $value)->first();
        $Y_data[] = $result->variation;
    }
}

// 输出到echars
$viewData = [
    'data' => [],
    'instruments_name' => 'xxxxx',
];

foreach ($dataSets as $key => $dataSet) {
    $viewData[$dataSet["X_data"][0]."_data"] = json_encode($dataSet["X_data"], true);
    $viewData[$dataSet["X_data"][0]."_data"] = json_encode($dataSet["Y_data"], true);
}

return view("index/safty/planes/".$type."_".$module."_".$action."_".$plane, compact('left','right'))->with($viewData);