0

我正在使用 phplot 在网页中绘制图形,并且我有以下代码,

   <?php
     //Include the code
    require_once 'C:/xampp/htdocs/phplot-6.1.0/phplot.php';

     //Define the object
     $plot = new PHPlot();

    //Define some data

     $example_data = array(
     array('a',3),
     array('b',5),
     array('c',7),
     array('d',8),
     array('e',4),
     array('f',6),
      array('g',7)
      );
     $plot->SetDataValues($example_data);

     //Turn off X axis ticks and labels because they get in the way:
     $plot->SetXTickLabelPos('none');
     $plot->SetXTickPos('none');

      //Draw it
      $plot->DrawGraph();
       ?>

我不想像 $example_data 那样定义数据,但我想从 txt 或 json 等外部文件读取或上传它,请建议如何实现这一点以及要上传的外部文件的类型是什么?

4

3 回答 3

4

是的你可以 :

$file = 'your.json';
$example_data = json_decode( @file_get_contents( $file ) );

your.json (例如):

[["a",3],["b",5],["c",7],["d",8],["e",4],["f",6],["g",7]]

更新 !

用于创建动态 json 文件:

$data = array();

$data[] = array( 'a' , 3 );
$data[] = array( 'b' , 1 );
$data[] = array( 'c' , 2 );
$data[] = array( 'd' , 4 );
$data[] = array( 'e' , 8 );
$data[] = array( 'f' , 6 );
$data[] = array( '6' , 7 );

echo json_encode( $data );

另一种方式 :

make_data.php

$data = array();

$data[] = array( 'a' , 3 );
$data[] = array( 'b' , 1 );
$data[] = array( 'c' , 2 );
$data[] = array( 'd' , 4 );
$data[] = array( 'e' , 8 );
$data[] = array( 'f' , 6 );
$data[] = array( '6' , 7 );

return $data;

并阅读:

$example_data = include( 'make_data.php' );
于 2013-09-19T20:09:05.023 回答
0

似乎相当常见的解决方案是简单地返回一个 php 数组。

包括.php:

<?php
return array(14, 34, 342, 4252);

索引.php:

<?php

$data=include('include.php');
于 2013-09-19T20:21:17.600 回答
0

像这样?

   <?php
     //Include the code
    require_once 'C:/xampp/htdocs/phplot-6.1.0/phplot.php';

     //Define the object
     $plot = new PHPlot();

    //Define some data

     $example_data = json_decode(file_get_contents("some_external_file.json"),true);
     $plot->SetDataValues($example_data);

     //Turn off X axis ticks and labels because they get in the way:
     $plot->SetXTickLabelPos('none');
     $plot->SetXTickPos('none');

      //Draw it
      $plot->DrawGraph();
       ?>


some_external_file.json

{"a":3,"b":5,"c":7,"d":8,"e":4,"f":6,"g":7};
于 2013-09-19T20:11:41.497 回答