I am new to this forum and to Open Flash Chart 2. I just installed a chart in one of my website, which works fine. I am getting the chart points dynamically through a URL using file_get_contents in PHP. Everything works fine, but some URLs doesn’t has points data so the chart is returning me the JSON error
Open Flash Chart
JSON Parse Error [Syntax Error]
Error at character 0, line 1:
0: <br />
I know why the error is punched, but I want to get rid of this, instead it should show “Not enough data to map the chart”.
Is there any solution for this?
Thank you in advance 😀
EDIT:
MY CODE:
<?php
include 'oc/php-ofc-library/open-flash-chart.php';
$url = 'http://*****.api3.nextbigsound.com/metrics/artist/'.$_REQUEST['artistId'].'.json';
$file = file_get_contents($url);
$data = json_decode($file);
foreach($data as $Idx => $key){
if($key->Service->name === 'MySpace'){
foreach($key->Metric as $Index => $Item){
$ctr = 1;
if($Index == "views"){
foreach($Item as $a => $b){
$record[] = $b;
}
}
}
}
}
$year = array();
$price = array();
$year[] = 'Sun, 12 Feb 2012'; $price[] = 36.7;
$year[] = 'Sun, 13 Feb 2012'; $price[] = 38.7;
$year[] = 'Sun, 14 Feb 2012'; $price[] = 42.8;
$year[] = 'Sun, 15 Feb 2012'; $price[] = 38.2;
$year[] = 'Sun, 16 Feb 2012'; $price[] = 37.8;
$year[] = 'Sun, 17 Feb 2012'; $price[] = 34.7;
$year[] = 'Sun, 18 Feb 2012'; $price[] = 38.4;
$chart = new open_flash_chart();
$chart->set_bg_colour( '#FFFFFF' );
$title = new title( 'MySpace Views' );
$title->set_style( "{font-size: 20px; background:#fff; color: #A2ACBA; text-align: center; width: 300px; border-radius:50px;}" );
$chart->set_title( $title );
$area = new area();
$area->set_colour( '#5B56B6' );
$area->set_values( $record );
$area->set_key( 'Views', 7 );
$chart->add_element( $area );
$x_labels = new x_axis_labels();
$x_labels->set_steps( 1 );
//$x_labels->set_vertical();
$x_labels->set_colour( '#A2ACBA' );
$x_labels->set_labels( $year );
$x = new x_axis();
$x->set_colour( '#A2ACBA' );
$x->set_grid_colour( '#D7E4A3' );
$x->set_offset( false );
$x->set_steps(1);
$x->set_range('0','6');
// Add the X Axis Labels to the X Axis
$x->set_labels( $x_labels );
$chart->set_x_axis( $x );
//
// LOOK:
//
$x_legend = new x_legend( '2012' );
$x_legend->set_style( '{font-size: 20px; color: #778877}' );
$chart->set_x_legend( $x_legend );
//
// remove this when the Y Axis is smarter
//
$y = new y_axis();
$min = min($record);
$max = max($record);
$len = strlen($max);
if($len == '9'){
$diff = '10000';
}elseif($len == '8'){
$diff = '1000';
}elseif($len == '7'){
$diff = '1000';
}elseif($len == '6'){
$diff = '1000';
}elseif($len == '5'){
$diff = '1000';
}
$y->set_range( $min, $max, $diff );
$chart->add_y_axis( $y );
echo $chart->toPrettyString();
Here is the print_r($data)
Array ( [0] => stdClass Object ( [Service] => stdClass Object ( [name] => MySpace [id] => 1 ) [Profile] => stdClass Object ( [url] => http://www.myspace.com/adamgreen1 [id] => 384 ) [Metric] => Array ( ) ) [1] => stdClass Object ( [Service] => stdClass Object ( [name] => Last.fm [id] => 2 ) [Profile] => stdClass Object ( [url] => http://www.last.fm/music/adam+green [id] => 174985 ) [Metric] => stdClass Object ( [plays] => stdClass Object ( [15383] => 10165386 [15384] => 10165386 [15385] => 10165375 [15386] => 10168408 [15387] => 10171611 [15388] => 10174725 [15389] => 10177797 ) [fans] => stdClass Object ( [15383] => 242392 [15384] => 242392 [15385] => 242535 [15386] => 242580 [15387] => 242641 [15388] => 242709 [15389] => 242775 ) [comments] => stdClass Object ( [15383] => 916 [15384] => 916 [15385] => 918 [15386] => 918 [15387] => 918 [15388] => 918 [15389] => 918 ) ) ) [2] => stdClass Object ( [Service] => stdClass Object ( [name] => Wikipedia [id] => 17 ) [Profile] => stdClass Object ( [url] => http://en.wikipedia.org/wiki/Adam_Green_(musician) [id] => 918802 ) [Metric] => Array ( ) ) )
Given that error, it would seem your php script is returning HTML in some cases, instead of json. That’s where you have to make the fix. If your Flash stuff is expecting ONLY json strings, then you cannot ever allow anything BUT json strings to be output from the script.