-1

In these days i'm trying to draw a graph from a file using PhpMyGraph5.0, on the autor's site (http://phpmygraph.abisvmm.nl/) there is this example file:

<?php    
//Set content-type header
header("Content-type: image/png");

//Include phpMyGraph5.0.php
include_once('phpMyGraph5.0.php');

//Set config directives
$cfg['title'] = 'Example graph';
$cfg['width'] = 500;
$cfg['height'] = 250;

//Set data
$data = array(
    'Jan' => 12,
    'Feb' => 25,
    'Mar' => 0,
    'Apr' => 7,
    'May' => 80,
    'Jun' => 67,
    'Jul' => 45,
    'Aug' => 66,
    'Sep' => 23,
    'Oct' => 23,
    'Nov' => 78,
    'Dec' => 23
);

//Create phpMyGraph instance
$graph = new phpMyGraph();

//Parse
$graph->parseHorizontalLineGraph($data, $cfg);
?> 

Because i need to get the input from a file i've modded the example file changing the $data assignment with:

$data = file("$PATH/$MYFILE");

I've formatted the text inside MYFILE and these are some lines of the file:

'00:00' => 19,
'00:05' => 19,
'00:10' => 21,
...
'17:10' => 21,
'17:15' => 21,
'17:20' => 21,

But when i try to draw the graph i obtain this message instead of the graph:

"exception `Exception` with message `The value of the key %s` is not numeric.`"  

I've searched in PhpMyGraph5.0.php and i've found the test that throws the exception:

//Loop
foreach($data as $key => $value) {
    //Test
    if(!is_numeric($value)) {
        throw new Exception('The value of the key "%s" is not numeric.');
    }
...

I've tried to substitute the "throw Exception" with this cast:

$value=(int)$value;

but i obtain only an empty graph.

If i manually paste the content of MYFILE inside $data = array(PASTE_HERE); it works, but i can't do it manually.

I think that the problem is about the data type of the value, but i've no ideas on how to solve this problem.

Thanks to everyone and sorry for my bad english.

4

1 回答 1

1

That exception seems to be badly coded, try changing it to this and it should give you the value of the key where it finds the value is not numeric, that may help identify where the error is :-

throw new Exception(sprintf('The value of the key "%s" is not numeric.',$key));

EDIT

Ok I see the problems, you are not getting what you think you are getting from the $data = file("$PATH/$MYFILE");

if you test using this

$data = file("$PATH/$MYFILE");
print_r($data);

You get the output :

Array
(
    [0] => '00:00' => 19,
    [1] => '00:05' => 19,
    [2] => '00:10' => 21,
    [3] => '17:10' => 21,
    [4] => '17:15' => 21,
    [5] => '17:20' => 21
)

So index [0] is actually an array and not a number, hence the error.

You are going to have to rethink the way you input your data.

Try this for size:

Change you data file to look like this

'00:00',19
'00:05',19
'00:10',21
'17:10',21
'17:15',21
'17:20',21

And your code to do this

$data = array();

$handle = fopen('tst.txt', 'r');
while (!feof($handle)) {
    $line = fgets($handle, 8192);
    list($time,$count) = explode(',',$line);
    $data[$time] = $count;
}
fclose($handle);
print_r($data);

This will generate the following array

Array
(
    ['00:00'] => 19
    ['00:05'] => 19
    ['00:10'] => 21
    ['17:10'] => 21
    ['17:15'] => 21
    ['17:20'] => 21
)

Which I assume is what you wanted in the first place.

EDIT 2

Dont change the package, change what you send it

Replace this line

$data[$time] = $count;

With

$data[$time] = (int)$count;

That should do it.

于 2014-07-17T08:24:04.783 回答