I'm using jquery flot charts to represent my data. Here's the example JSFiddle I made that shows how the JSONS's required for the chart should look.
The data source is from a MySql stored procedure that has the below output example:
I need to represent in the chart, the count
values stacked for different innumber
's on y-axis, the name
values on x-axis, and in another chart, the values for outnumber
. (in stacked bars).
-The data series should match, so the specific labels should appear against customers.
Here's the PHP I have so far:
$query = $this->db->query("call GetAllCustomersV2($id, $year, $month, $day)");
$customers = $query->result_array();
foreach ($customers as $customer) {
if($customer['innumber'] != null){
$chartInbound['name'] = $customer['name'];
$chartInbound['label'] = $customer['innumber'];
$chartInbound['count'] = $customer['count'];
$chartInbound['customerid'] = $customer['id'];
array_push($out['chartInbound'], $chartInbound);
}
if($customer['outnumber'] != null){
$chartOutbound['name'] = $customer['name'];
$chartOutbound['label'] = $customer['outnumber'];
$chartOutbound['count'] = $customer['count'];
$chartOutbound['customerid'] = $customer['id'];
array_push($out['chartOutbound'], $chartOutbound);
}
}
The output of print_r($out['chartInbound']);
is:
Array
(
[0] => Array
(
[name] => 1st Online Solutions
[label] => 01-02
[count] => 577
[customerid] => 129
)
[1] => Array
(
[name] => Bookngo
[label] => 01-02
[count] => 2
[customerid] => 95
)
[2] => Array
(
[name] => Boutixury
[label] => 07
[count] => 1
[customerid] => 14
)
[3] => Array
(
[name] => Cruise Village
[label] => 01-02
[count] => 16
[customerid] => 25
)
[4] => Array
(
[name] => Cruise Village
[label] => 00
[count] => 1
[customerid] => 25
)
[5] => Array
(
[customer] => Cruise Village
[label] => 07
[countInbound] => 16
[minsInbound] => 125
[customerid] => 25
)
...................
)
The output of print_r(json_encode($out['chartInbound']));
is:
[
{
"name": "1st Online Soultions"
"label": "01-02",
"count": "577",
"customerid": "129"
},
{
"name": "Bookngo"
"label": "01-020",
"count": "2",
"customerid": "129"
},
{
"name": "Boutixury"
"label": "07",
"count": "1",
"customerid": "14"
},
{
"name": "Cruise Village"
"label": "07",
"count": "16",
"customerid": "25"
},
.................
]
But this is not very helpful.
Q: How can I create the dynamic JSON's shown in the above jsfiddle, from the query data ?
It looks like your data points in
chartTicks[i]
need to match the order of the ticks inchartData[i].data
. One way to ensure such a match is to sort the data by name in sql and to stack your results by customer first and label second in php.Going through your data with a loop and building up the
newData
andnewTicks
arrays for flot to use:I also had to change your tooltip generation since your code only worked when all dataseries where complete and sorted. It is also simpler now.
complete fiddle
Just an idea, I suppose you're using a group by in your stored procedure. If you could modify it and add a WITH ROLLUP the database would have the count calculated for you... See https://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.html or search SO for suggestion
You can do it on the client side (though ideally it should be done server-side), by using something like:
It accounts for different customers (different customerids) with the same name (though Flot will not really deal well with that), and customers with missing data for some labels. Shouldn't be too hard to shift this logic into PHP, and do it server-side.
EDIT: Okay, I hadn't noticed it acts weirdly when there are labelID "gaps". Here's the revised code:
Label IDs are given in the order they appear in the table that comes from the server. (Though it still distinguishes between two customers with the same name but different customerIDs)
This is a concise way to transform your current JSON data structure into the desired output:
Fiddle: http://jsfiddle.net/rdkgbteq/1/
Here is the same thing in PHP if you are wanting to transform the data on the server:
Let me know if you want me to expand upon what's going on here.
You are going to have to transform the structures yourself. You can either do this server side or client side. In either case run through the results and build the structure you want. Be careful of trying to encode php associative arrays in json and beware of the behaviour of NUMERIC_CHECK.