sql - Create PieChart with google chart api and php -


i'm kinda new google chart api, , still wiping dust out php knowledge, maybe can (imo) basic question...

so have php class wich queries data server , should post page. there working line chart in it, can see @ 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(*) totalaccounts,                            sum(case when facebook_username != '' 1 else 0 end) facebook,                            sum(case when facebook_username = '' 1 else 0 end) standard                     account                     entered between '". $_post['from'] ."' , '". $_post['to'] ."'                     group 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 trying use same query result ($data) populate chart, 1 pie chart... pasted last 4 lines of code, changing parameter when creating new instance of chart:

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

after this, close php tag , use 2 divs show charts...

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

everything here comes index.php class, haven't seen html files @ all... happens can see pie chart right below line chart, it comes no data within, i.e., whole chart grey , labeled "other"

what may i've done wrong? in advance!

[edit] let's want pie chart 2 slices, why code below doesn't works? (i "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 can helping me :) https://groups.google.com/forum/#!topic/google-visualization-api/wi_wovogzg8

creating pie char mysql database using php , google charts

piecharts don't use same data structure linecharts. piecharts expect 2 columns of data: first pie slice labels, , second pie slice values. have reconfigure data pull put data in correct format.

edit:

here's sample php pseudo-code building datatable piechart:

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

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -