json 从php服务器获取用于convase js的自定义输出

iswrvxsc  于 2023-07-01  发布在  PHP
关注(0)|答案(1)|浏览(115)

我有一个表在worodpress网站和输出从这个表是这样的:

{
  { [line1]=> array(3) {{'x'=>'5' , 'y'=>'8},{'x'=>'5' , 'y'=>'8},{'x'=>'5' , 'y'=>'8}},
  { [line2]=> array(3) {{'x'=>'5' , 'y'=>'8},{'x'=>'5' , 'y'=>'8},{'x'=>'5' , 'y'=>'8}}
}

我想通过此数据显示图表,所需的输出是这样的:

<script type="text/javascript">
  window.onload = function () {
    var chart = new CanvasJS.Chart("chartContainer",
      {
        axisX: {
          title: "Thick Axis line",
          lineThickness: 6

        },
        data: [{
          type: "line",
          title: 'line1',
          dataPoints: [{ x: 5, y:8 }, { x: 5, y: 8 }]
        }, {
          type: "column",
          title: 'line2',
          dataPoints: [{ x: 5, y: 8 }, { x: 5, y: 8}]
        }
        ]
      });

    chart.render();
  }
</script>

如何将php输出转换为客户需要数据?
当然,行数不同,可以从1行到20行

xienkqul

xienkqul1#

希望这段代码能帮助你:

<?php 
    //your output
    var_dump($php_output) ;
?>
<script type="text/javascript">
    var point = <?php echo json_encode($php_output, JSON_NUMERIC_CHECK); ?>;

    function convase_needed_output() {
       var output= [];

       for (let i in point) {
        var custom_output = '';
        custom_output = {type: "spline", dataPoints: point[i]};

        output.push(custom_output);
       }

      return output;
    }

//convase js exmaple
window.onload = function() {
  var chart = new CanvasJS.Chart("chartContainer", {

    title: {
      text: "php to convase needed output"
    },
    data: convase_needed_output()
  });

  chart.render();
}
</script>

相关问题