dshp9580656
2014-02-16 17:47
浏览 36
已采纳

如何在CakePHP中使用GoogleCharts插件?

I have been messing around with different visualization plugins which allows you to represent data in the form of charts.. and following my previous post, It seems that it is a bit outdated, and out of desperation I looked for other plugins which may offer similar options.

Now, I have realised that googleCharts might have just what I need, and as it turns out, there is a cake googlechart plugin which should be able to do what I need. However, I am having a little bit of trouble understanding how I will implement it.

From what I have seen here and in the plugin doc, it accepts data in the form of an array of results, from which is then iterated, and displaying the fields which match, specified in the columns key. (If I'm not correct, please do explain.)

Otherwise, I am looking for a way I can generate a pie chart using values returned from 3 queries as follows:

        $users_donor = $this->User->find('count' , array(
            'conditions' => array('User.role' => 'donor') 
        ));

        $users_staff = $this->User->find('count' , array(
            'conditions' => array('User.role' => 'staff') 
        ));
        $users_admin = $this->User->find('count' , array(
            'conditions' => array('User.role' => 'admin') 
        ));

Basically I want to show a visual representation of all the current registered users, grouped by their role.. Is there a way I can pass in the latter 3 count values (i.e users_donor,users_staff,users_admin)

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douhan0562 2014-02-16 20:08
    已采纳

    I had to think backwards for this one. The problem was that I was still trying to make use of the code I was using for the other plugin mentioned in my previous post (ie, I was retrieving the count values in separate variables.)

    As you might have guessed, all I needed to do is retrieve that same information in an array using cakePHP query method to perform an SQL query and groupBy role!

    Here it is:

    $results = $this->User->query(
            "SELECT users.role as user_role, 
                Count(users.role) AS user_count
                FROM users
                GROUP BY users.role"
             );
    

    Allowing me to use the results array in conjunction with googleCharts plugin!

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题