dongyuzhu2244 2018-08-11 12:13
浏览 50

删除数组中的前缀y以使用它作为图形的ax轴

I took a row from a database with php and mysql and put it in a the array $dataPoints6. Then i transfered it with json-encode in an js array. In Generel I want to use this array for data part of a line graph. The problem (checked with console.log) is, that there is always an y in every array part. See here ( :{y: "39"})

How can I delete this y? Without this y I can use an array for the x or y axis in a line graph. Here is my Code. Thx for your help.

 <?php
   
 $dataPoints6 = array();
 try{
     // Creating a new connection.
    // Replace your-hostname, your-db, your-username, your-password according to your database
    $link = new \PDO(   'mysql:host=localhost;dbname=alurhbuj3;charset=utf8mb4', //'mysql:host=localhost;dbname=canvasjs_db;charset=utf8mb4',
                        'cu7iymbi1b', //'root',
                        'Thomas100388', //'',
                        array(
                            \PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                            \PDO::ATTR_PERSISTENT => false
                        )
                    );
       
    $handle6 = $link->prepare("select y from Lt_data where nr=1"); 
    $handle6->execute(); 
    $result6 = $handle6->fetchAll(\PDO::FETCH_OBJ);
        
    foreach($result6 as $row6){
        array_push($dataPoints6, array($row6));
    }
   
    
    $link = null;
    
}
catch(\PDOException $ex){
  
}


?>

 <script>
 var js_array = <?php echo json_encode($dataPoints6); ?>;
   
   var a =[2,4,6,8,10];
    console.log(js_array);
var trace1 = {
  x: a, 
  y: [1,2,3,4,5], 
  type: 'scatter'
};
var trace2 = {
  x: [1, 2, 3, 4], 
  y: [16, 5, 11, 9], 
  type: 'scatter'
};
var data = [trace1, trace2];

var layout1 = {
  yaxis: {rangemode: 'tozero',
          showline: true,
          zeroline: true}
};

var layout2 = {
  yaxis: {rangemode: 'tozero',
           
          zeroline: true}
};

Plotly.newPlot('div1', data, layout1);

Plotly.newPlot('div2', data, layout2);
</script>
<body>
  
  <div id="div1"></div>
  <div id="div2"></div>

</body>

</div>
  • 写回答

1条回答 默认 最新

  • dongyupen6269 2018-08-11 12:57
    关注

    Because you only need the array of values without key. as you have created your dummy arrays in javascript. I would suggest you should add the value from the database directly into the $dataPoints6 array as follows.

    foreach($result6 as $row6){
        $dataPoints6[] = $row6->y;
    }
    

    This way you will get your array same as your javascript array

    a =[2,4,6,8,10];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题