Lucas Jota
Lucas Jota

Reputation: 1873

Create PieChart with google chart api and php

I'm kinda new to google chart api, and still wiping the dust out my php knowledge, so maybe someone can help with this (IMO) basic question...

So I have this php class wich queries data from server and should post it back to my page. There is a working Line Chart in it, as you can see at the snippet below:

$rowdata = array();
            $i=0;
            $_POST['from'] = str_replace("/","-",$_POST['from']);
            $_POST['to'] = str_replace("/","-",$_POST['to']);

            $cur_date = $_POST['from'];

            $sql =  "SELECT DATE(entered),
                           COUNT(*) AS totalaccounts,
                           SUM(CASE WHEN facebook_username != '' THEN 1 ELSE 0 END) AS facebook,
                           SUM(CASE WHEN facebook_username = '' THEN 1 ELSE 0 END) AS standard
                    FROM Account
                    WHERE entered BETWEEN '". $_POST['from'] ."' AND '". $_POST['to'] ."'
                    GROUP BY DATE(entered)";

            $result = $dbMain->query($sql);
            $rows = tratarDadosParaGrafico($result);

            $return = json_encode(array('cols' => $cols, 'rows' => $rows));

            $data = array(
                'cols' => array(
                    array('id' => '', 'label' => 'Date', 'type' => 'string'),
                    array('id' => '', 'label' => 'Total Accounts', 'type' => 'number'),
                    array('id' => '', 'label' => 'Total Accounts (Facebook)', 'type' => 'number'),
                    array('id' => '', 'label' => 'Total Accounts (Non Facebook)', 'type' => 'number')
                ),
                'rows' => $rows
            );


            $chart = new Chart('LineChart');
            $options = array('title' => 'Accounts');
            $chart->load(json_encode($data));
            echo $chart->draw('rchart', $options);

What I was trying to do was use the same query result ($data) to populate another chart, this one a pie chart... So I simply pasted the last 4 lines of code, changing the parameter when creating a new instance of Chart:

$chart = new Chart('PieChart');
            $options = array('title' => 'Accounts');
            $chart->load(json_encode($data));
            echo $chart->draw('pchart', $options);

After this, I close the php tag and use 2 divs to show my charts...

<div id="rchart"></div>
        <div id="pchart"></div>

Everything here comes from this index.php class, I haven't seen html files at all... What happens is I can see the pie chart right below the line chart, but it comes with no data within, i.e., the whole chart is grey and labeled as "Other"

What may I've done wrong? Thanks in advance!

[EDIT] Let's say I want a Pie Chart with only 2 slices, why the code below doesn't works? (I get "Cannot read property '1' of undefined" error)

$data = array(
                'cols' => array(
                    array('label' => 'Pie slice labels', 'type' => 'string'),
                    array('label' => 'Pie slice values', 'type' => 'number')
                ),
                'rows' => array(
                    array('v' => intval($facebook_accounts[0]), 'type' => 'int'),
                    array('v' => intval($default_accounts[0]), 'type' => 'int')
                )
            );
            $chart = new Chart('PieChart');
            $options = array('title' => 'Accounts');
            $chart->load(json_encode($data));
            echo $chart->draw('piechart', $options);

[EDIT] Maybe this can help you helping me :) https://groups.google.com/forum/#!topic/google-visualization-api/Wi_WOVOgzG8

Creating Pie char from mysql database using php and google charts

Upvotes: 0

Views: 1296

Answers (1)

asgallant
asgallant

Reputation: 26340

PieCharts don't use the same data structure as the LineCharts. PieCharts expect two columns of data: the first is the pie slice labels, and the second is the pie slice values. You will have to reconfigure your data pull to put the data in the correct format.

Edit:

Here's some sample PHP pseudo-code for building a DataTable for a PieChart:

$data = array(
    'cols' => array(
        array('label': 'Pie slice labels', type: 'string'),
        array('label': 'Pie slice values', type: 'number')
    ),
    'rows' => array()
);
while (/* loop over SQL results */) {
    $data['rows'][] = array('c' => array(
        array('v' => /* pie slice label */),
        array('v' => /* pie slice value */) // use (int) or (float) to parse appropriately, as many databases output numbers as strings
    ));
}

Upvotes: 2

Related Questions