【发布时间】:2015-07-18 16:31:01
【问题描述】:
我使用jquery flot charts 来表示我的数据。这是我制作的示例 JSFiddle,它显示了图表所需的 JSONS 的外观。
数据源来自 MySql 存储过程,其输出示例如下:
我需要在图表中表示 count 值堆叠在 y 轴上的不同 innumber 上,name 值在 x 轴上,在另一个图表中,outnumber 的值. (在堆积条形图中)。
-数据系列应匹配,因此特定标签应针对客户显示。
这是我目前拥有的 PHP:
$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);
}
}
print_r($out['chartInbound']); 的输出为:
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
)
...................
)
print_r(json_encode($out['chartInbound'])); 的输出是:
[
{
"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"
},
.................
]
但这不是很有帮助。
问:如何根据查询数据创建上述 jsfiddle 中显示的动态 JSON?
【问题讨论】:
-
你的问题中的数据(来自MySql的数据)和你的jsfiddle中的数据应该匹配吗??
-
chartData和chartTicks的格式应该相同,并且它们的值应该动态填充 -
可以修改存储过程,还是固定?是否可以通过 SQL 语句直接攻击数据?如果是的话,能否提供数据结构,或者只是修改获取数据的方式,就好像获取JSON数据一样。
标签: javascript php mysql json flot