-3

I'm making a pie chart of expenses using JavaScript and CodeIgniter.

Controller

chart_c
function getchart1() {
    $result['query']=$this->model_m->getchart2();
    $this->load->view('chartview',$result);
}
model_m
function getchart2() {
    $query = $this->db->get('qpaper');
    return $query->result();
}

View

foreach($query as $row) {
    $ex=$row->expenses;
    $cat=$row->category;
}

How can I access $row->expenses and $row->category from the script above in the following JavaScript?

var data = google.visualization.arrayToDataTable([
    ['Task', 'Hours per Day'],////how i will insert the data from db 
    ['Work',     11],///my doubt here $e how i will put here
    ['Eat',      2],
    ['Commute',  2],
    ['Watch TV', 2],
    ['Sleep',    7]
]);

The full code:

<html>
    <head>
        <script type="text/javascript" src="https://www.google.com/jsapi"></script>
        <script type="text/javascript">
            google.load("visualization", "1", {packages:["corechart"]});
            google.setOnLoadCallback(drawChart);
            function drawChart() {
                var data = google.visualization.arrayToDataTable([
                    ['Task', 'Hours per Day'],////how i will insert the data from db 
                    ['Work',     11],///my doubt here $e how i will put here
                    ['Eat',      2],
                    ['Commute',  2],
                    ['Watch TV', 2],
                    ['Sleep',    7]
                ]);

                var options = {
                  title: 'My Daily Activities'
                };

                var chart = new google.visualization.PieChart(document.getElementById('piechart'));

                chart.draw(data, options);
            }
        </script>
    </head>
    <body>
        <div id="piechart" style="width: 900px; height: 500px;"></div>
    </body>
</html>
manjusha
  • 113
  • 4
  • 6
  • 15
  • `please give code` is not a question and doesn't explain what problems you are having with code shown – charlietfl Aug 01 '15 at 00:43
  • Use [JQPLOT](http://www.jqplot.com/tests/pie-donut-charts.php) for Pie charts or any type of graph in codeigniter.Its easy. – Bugfixer Aug 01 '15 at 05:01
  • possible duplicate of [How to pass variables and data from PHP to JavaScript?](http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) –  Aug 01 '15 at 05:16
  • store your chart information in $data and pass it to view file.Your can echo your chart info.This can also be done in a inprofessional way be running query in view itself and storing info inside js variable. – Bugfixer Aug 01 '15 at 05:18
  • sir my doubt is how will put $data in var data = google.visualization.arrayToDataTable([ ['Task', 'Hours per Day'],////how i will insert the data from db ['Work', 11],///my doubt here $e how i will put here ['Eat', 2], ['Commute', 2], ['Watch TV', 2], ['Sleep', 7] ]); – manjusha Aug 01 '15 at 09:22

1 Answers1

0

If its just you want a pie chart using php array, you could use Highcharts. Check their Edit in Jsfiddle page. You just need to pass data in json format to generate the chart. I think you already have php array, convert it into json format and pass it to this highchart function.

jones
  • 749
  • 7
  • 34