dongyangben6144 2018-04-26 14:42
浏览 75

如何在PHP.JS中使用PHP制作JSON

everyone. What I want is Bubble Chart(Chart.js) with data from PHP. PHP executes python file. *Actually, executing complicated algorithm, however here simplify with test.py.

index.php

<?php

$pyfile = "python ./test.py";
exec($pyfile, $output, $rtn);
$outputJson = json_encode($output);
?>

test.py

import sys
if __name__=='__main__':

 print [{'data': [{'x':10 ,'y':10, 'r':30}],'backgroundColor':[ 'rgb(141,63,223)'],'label': ['test1']},
       {'data': [{'x':20 ,'y':20, 'r':50}],'backgroundColor':[ 'rgb(141,29,73)'],'label': ['test2']},
       {'data': [{'x':30 ,'y':30, 'r':70}],'backgroundColor':['rgb(16,230,73)'],'label': ['test3']}]

index.php

<body>
<canvas id="my_chart">
Canvas not supported...
</canvas>

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.min.js"> 
</script>

I have to bring the result from php to javascript like this.

<script>

var type = 'bubble';

  var data = {
    datasets: //I want to insert result from php here
    ]};

  var options = {

      title: {
        display: true,
        text: 'bubble-sample'
      },

      scales: {

          xAxes: [{
              ticks: {max: 50, min: 0,stepSize: 10}
          }],

          yAxes: [{
              ticks: {max: 50,min: 0,stepSize: 10}
          }]
      },

};
var ctx = document.getElementById('my_chart').getContext('2d');
var myChart = new Chart(ctx, {
    type: type,
    data: data,
    options: options
  });

In the end, with Chart.js...

var type = 'bubble';

  var data = {
    datasets: [
        {
           "data": [{
               "x":40,
               "y":30,
               "r":30
           },],

           "backgroundColor":[ "rgb(141,63,223)" ],

           "label": ["test1n"]
        },
        {

            data: [{"x":20 ,"y":20, "r":50} ,],

            backgroundColor:[ "rgb(141,29,73)"],

            label: ["test2"]
        },
        {

            data: [{"x":30 ,"y":30, "r":70} ,],

            backgroundColor:["rgb(16,230,73)"],

            label: ["test3"]
        }
    ]};

  var options = {

      title: {
        display: true,
        text: 'bubble-sample'
      },

      scales: {

          xAxes: [{
              ticks: {max: 50, min: 0,stepSize: 10}
          }],

          yAxes: [{
              ticks: {max: 50,min: 0,stepSize: 10}
          }]
      },

};
var ctx = document.getElementById('my_chart').getContext('2d');
var myChart = new Chart(ctx, {
    type: type,
    data: data,
    options: options
  });

Here, I found how to use php variables in javascript.

var outputjs = '<?php echo $outputJson; ?>'

However, it is just a string, because this is coming from "print" in python file.

How should I do to make this result of python script working in Chart.js as a datasets array??

  • 写回答

1条回答 默认 最新

  • dsfdf854456 2018-04-26 14:47
    关注

    Try

     var outputjs = <?php echo $outputJson; ?>
    

    (Surrounding it with ' is making it a string)

    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法