简体   繁体   中英

Google Visualizations Pie Chart PHP array

I have to make a pie chart that will display this data:

 var data = google.visualization.arrayToDataTable([
           ['Type', 'Amount'],
           ['Option1',   11],
           ['Option2',     6],
         ]);

I'm using Codeigniter to make queries and create an array which is fed into the JS like this:

    function drawChart(){
        var data = google.visualization.arrayToDataTable(<?php echo $type_array_json; ?>);

        var options = {
            title: 'Type',
            legend: {position: "bottom"},
        };

        var chart = new google.visualization.PieChart(document.getElementById('type_chart'));
        
        chart.draw(data, options);
    }

I'm querying the data like this:

function get_chart_data($ID = '', $start_date = '', $end_date = '')
        {
            if (!empty($start_date) && !empty($end_date))
            {           
                $this->db->select("Option1 and Option2 as Type, COUNT(ID) as Amount", FALSE); // This line is the problem
                $this->db->from('TABLE');
                populate_where($this->db, 'ID', $ID, $ID);
                
                $this->db->group_by("DATALABEL");
                $this->db->order_by("DATASORT ASC");
                
                $query = $this->db->get();
                return $this->_create_report_array($query->result_array());
            }

Option1 and Option2 are both fields in a record that can be 1 or 0, and I need to compare the total amount of Option1=1 vs Option2=1.

How can I make the resulting array the same as the example one, so that the graph will display the 2 options in the first column and the values for them in the second?

Please help me

You can use the model to only fetch data from the database and pass the data to a controller for formatting.

MODEL

public function option_model()
      {   
         $this->db->select('option, COUNT(*) as frequency');
         $this->db->group_by('label'); 
         $this->db->order_by('frequency', 'desc');
         $query = $this->db->get($this->tablename);
         return $query->result();    
      }

CONTROLLER

public function option_controller(){
        $result = $this->Database_model->option_model();
        $array = array();
        $cols = array();
        $rows = array();
        $cols[] = array("id"=>"","label"=>"Option1","pattern"=>"","type"=>"string");
        $cols[] = array("id"=>"","label"=>"Count","pattern"=>"","type"=>"number");  

        foreach ($result as $object) {
            $rows[] = array("c"=>array(array("v"=>(string)$object->label),array("v"=>(int)$object->frequency)));
          }
        $array = array("cols"=>$cols,"rows"=>$rows);
    
        return json_encode($array);
        }

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