Friday, March 30, 2012

Re: cakePHP Data into Google Charts

I've outputted some charts (combocharts - near identical to column chart) using the visualization api and got the model to output an array like:

Array
(
    [labels] => Array
        (
            [0] => Age
            [1] => Clients
            [2] => Not Offered
            [3] => Accepted Offer
            [4] => Declined Offer
        )

    [16] => Array
        (
            [0] => 16
            [1] => 10
            [2] => 7
            [3] => 5
            [4] => 0
        )

    [17] => Array
        (
            [0] => 17
            [1] => 27
            [2] => 22
            [3] => 13
            [4] => 4
        )

    [18] => Array
        (
            [0] => 18
            [1] => 13
            [2] => 8
            [3] => 7
            [4] => 2
        )

    [19] => Array
        (
            [0] => 19
            [1] => 12
            [2] => 6
            [3] => 6
            [4] => 1
        )

    [20] => Array
        (
            [0] => 20
            [1] => 8
            [2] => 2
            [3] => 6
            [4] => 0
        )

    [21] => Array
        (
            [0] => 21
            [1] => 7
            [2] => 4
            [3] => 3
            [4] => 0
        )

)

I then ran this through the following
foreach($arrayFromModel AS $row) {
  $chartData[] = json_encode($row);
}

and ended up with:
Array
(
    [0] => ["Age","Clients","Not Offered","Accepted Offer","Declined Offer"]
    [1] => ["16",10,7,5,0]
    [2] => ["17",27,22,13,4]
    [3] => ["18",13,8,7,2]
    [4] => ["19",12,6,6,1]
    [5] => ["20",8,2,6,0]
    [6] => ["21",7,4,3,0]
)

With an array of this type you use the following google function to get your data table:
var data = google.visualization.arrayToDataTable([".join(',', $chartData)."]);

Hope this is of some help, phpMagpie

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: