I want to create charts using fusion charts and use json as the data format
If my data before like this :
{
"items": [
{
"2013-03-28": 1771,
"2013-03-29": 1585,
"2013-03-30": 1582,
"2013-03-31": 1476
}
]
}
I get the above data using php in processing :
<?php
$param = $_GET['myparam'];
$Data = file_get_contents("http://mylink.com/proccess.php?output=json");
$Proses2 = json_decode($Data);
$array = array();
$array[] = (object)$Proses2;
if ($_GET['callback']) {
echo $_GET['callback'] . '('.json_encode($array).')';
}else{
echo '{"items":'. json_encode($array) .'}';
}
How do I change the data so that it becomes like the format to be used in a chart like this?
{
"chart": {
"caption" : "Weekly Sales Summary" ,
"xAxisName" : "Week",
"yAxisName" : "Sales",
"numberPrefix" : "$"
},
"data" :
[
{ "label" : "Day 1", "value" : "14400" },
{ "label" : "Day 2", "value" : "19600" },
{ "label" : "Day 3", "value" : "24000" },
{ "label" : "Day 4", "value" : "15700" }
]
}
Which later became :
{
"chart": {
"caption" : "Weekly Sales Summary" ,
"xAxisName" : "Week",
"yAxisName" : "Sales",
"numberPrefix" : "$"
},
"data" :
[
{ "label" : "2013-03-28", "value" : "1771" },
{ "label" : "2013-03-29", "value" : "1585" },
{ "label" : "2013-03-30", "value" : "1582" },
{ "label" : "2013-03-31", "value" : "1476" }
]
}
As $Proses2
is a basic object ( stdClass
) you could easily add new properties like chart
and data
, fill them with what you want (in this case, data from items
) and finally remove the items
property
Here is an example:
<?php
// The json
$json = '{"items":[{"2013-03-28":1771,"2013-03-29":1585,"2013-03-30":1582,"2013-03-31":1476}]}';
// Extract the json to a STD class object
$object = json_decode($json);
// print the actual object
print_r($object);
// modify object by adding new property
$object->chart = array(
"caption" =>"Weekly Sales Summary",
"xAxisName" => "Week",
"yAxisName" => "Sales",
"numberPrefix" => "$"
);
// Remove previous property
unset($object->items);
print_r($object);
var item = {
"items":{
"2013-03-28": "1771",
"2013-03-29": "1585",
"2013-03-30": "1582",
"2013-03-31": "1476"
}
};
var data = [];temp=0;
for(var key in item.items)
{
alert(key);alert(item.items[key]);
data.push({});
data[temp].label = key;
data[temp].value = item.items[key];
temp++;
}
alert(JSON.stringify(data));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.