简体   繁体   中英

Process SQL to JSON through PHP for Bubble Chart

I'm working on a Bubble Chart using Highcharts. Here's a sample of my data:

  name  | price | quantity | count
--------+-------+----------+-------
 Female |     2 |        3 |     5
 Female |     3 |       12 |    10
 Female |     5 |        6 |    15
 Female |     1 |        7 |    25
 Male   |     3 |        5 |     7
 Male   |     2 |        9 |    11
 Male   |     5 |        7 |    23
 Male   |     4 |        4 |    14

I'm using PHP to query the data and encode to JSON:

$query = "SELECT name, price, quantity, count FROM sales WHERE id = $1";

$result = pg_prepare($db, "report", $query);
$result = pg_execute($db, "report", array($ID));

while ($row = pg_fetch_array($result, NULL, PGSQL_ASSOC))
{
        $response['xdata'][$row['name']]['x'][] = $row['price'];
        $response['xdata'][$row['name']]['y'][] = $row['quantity'];
        $response['xdata'][$row['name']]['radius'][] = $row['count'];
}

echo json_encode($response);

However, the desired JSON format is as follows in order to properly plot the graph :

series: [{
    name: 'Female',
    marker:{
        symbol:'circle',
        fillColor:'rgba(24,90,169,.5)',
        lineColor:'rgba(24,90,169,.75)',
        lineWidth:1,
        color:'rgba(24,90,169,1)',
        states:{
            hover:{
                enabled:false
            }
        }
    },
    data: [{x:2,y:3,marker:{radius:5}},
           {x:3,y:12,marker:{radius:10}},
           {x:5,y:6,marker:{radius:15}},
           {x:1,y:7,marker:{radius:25}}]
    },{
    name: 'Male',
    marker:{
        symbol:'circle',
        fillColor:'rgba(238,46,47,.5)',
        lineColor:'rgba(238,46,47,.75)',
        lineWidth:1,
        color:'rgba(238,46,47,1)',
        states:{
            hover:{
                enabled:false
            }
        }
    },
    data: [{x:3,y:5,marker:{radius:7}},
           {x:2,y:9,marker:{radius:11}},
           {x:5,y:7,marker:{radius:23}},
           {x:4,y:4,marker:{radius:14}}]
   }]

My question is, how can I correctly process $query in PHP to get the desired JSON format as above and pass it to series through something like optionsBubble.series = data.xdata ? Thanks a lot!

You'd first have to build the non-db-related parts into your PHP structure, eg

$data = array(
   0 => array(
        'name' => 'Female',
        'marker' => array (
             'symbol': 'circle'
             etc....),
        'data' => array() // database insertion occurs here
        ),
   1 => array(
        'name' => 'Male',
        etc...
        )
);

$locations = array('Female' => 0, 'Male' => 1, etc...) // reverse map your 'name' fields

while(...) {
     $data[$locations[$row['name']]][data]['x'][] = $row['price'];
     $data[$locations[$row['name']]][data]['y'][] = $row['quantity'];
           ^^^^^^^^^^^^^^^^^^^^^^^^--- reverse lookup to get right array index for 'name'
}

First I'm going to recommend you take a look at your SQL query, especially the part of WHERE id=$1 . If I'm not mistaken (and on this I'm fairly sure.) your query is going to return one (1) row not many like what you probably want. I would recommend removing the WHERE clause and see if that solves your problem.

If not drop me a line and I'll see what else I see and we can go from there.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM