桑心996 2023-05-15 08:53 采纳率: 79.3%
浏览 40
已结题

PHP数据库提取数组到前端

想问一下 怎么把这些写成很简便的代码,并且有效在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);

        })();  

  • 写回答

2条回答 默认 最新

  • 瞬间的未来式 2023-05-15 09:06
    关注

    该回答引用chatgpt:感觉应该可以,尝试一下,有问题可以@我

    
    $chartData = [
        ["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 ($chartData as $data) {
        foreach ($data['X_data'] as $value) {
            $result = DB::table('instruments_tuokong')->select('variation')->orderBy('create_time', 'desc')->where('code', $value)->first();
            $data['Y_data'][] = $result->variation;
        }
    }
    
    // 输出数据到Echars
    foreach ($chartData as $data) {
        echo "X轴数据: " . implode(", ", $data['X_data']) . "<br>";
        echo "Y轴数据: " . implode(", ", $data['Y_data']) . "<br><br>";
    }
    
    

    这段代码创建了一个名为$chartData的数组,其中包含不同的X轴数据和相应的Y轴数据。通过循环遍历$chartData数组,并使用嵌套的循环来获取每个X轴数据对应的Y轴数据。最后,使用循环输出数据到Echars,X轴数据和Y轴数据分别用逗号分隔。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月17日
  • 修改了问题 5月15日
  • 创建了问题 5月15日

悬赏问题

  • ¥15 Windows Script Host 无法找到脚本文件"C:\ProgramData\Player800\Cotrl.vbs”
  • ¥15 matlab自定义损失函数
  • ¥15 35114 SVAC视频验签的问题
  • ¥15 impedancepy
  • ¥15 求往届大挑得奖作品(ppt…)
  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图