Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8390763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:05:23+00:00 2026-06-09T19:05:23+00:00

In my controller I create the array to be charted function chart() { $results

  • 0

In my controller I create the array to be charted

function chart() {  
    $results = $this->Visit->query(
        "SELECT date(visits.created) as visit_date, 
            Count(visits.id) AS count_visits
            FROM visits
            GROUP BY date(visits.created)"
    );

    foreach($results AS $result) {
        $row = array(
            $result[0]['visit_date'],
            $result[0]['count_visits']          
        );
        $chartData[] = json_encode($row);
    }

    pr($chartData);

}

pr($chartData) gives following array

Array
 (
    [0] => ["2012-07-11","5"]
    [1] => ["2012-07-13","1"]
    [2] => ["2012-07-14","1"]
)

in chart view i have

google.load('visualization', '1.0', {'packages':['corechart']});

google.setOnLoadCallback(drawChart);

function drawChart() {
    // Create the data table.

    var data = google.visualization.arrayToDataTable($chartData);

    var options = {
      title: 'Visits by Date',
      hAxis: {title: 'Date', titleTextStyle: {color: 'black'}}
    };

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

but this doesn’t get me a chart .. no errors … just no chart ..

if I manually type in an array to test my view, it works fine … eg if i replace ..

var data = google.visualization.arrayToDataTable($chartData);

with the following format from Google Chart example it generates a nice column chart

    var data = google.visualization.arrayToDataTable([
              ['Date',  'Count'],  
      ['2012-07-11',  5],
      ['2012-07-13',  1],
      ['2012-07-14',  1]
    ]);

couple of questions:
– how do i get the header titles in the var data array?
– do double quotes or single quotes in array make a difference?
– am i passing the array $chartData from php to Google chart javascript correctly?
– the Google example has ‘[]’ around the array, how do i get those around my array too?

(bwt, i know all about cakephp’s Find(all) but I just got lost in documentation about how to get what i wanted and sql was much easier )

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:05:24+00:00Added an answer on June 9, 2026 at 7:05 pm

    you are using json_encode in your controller, but in your view you are using –

    google.visualization.arrayToDataTable($chartData);
    

    json_encode creates json object, not and javascript array.

    using json_encode in your controller function chart() try:

    $chartData['cols'] = array(
        array('id' => 'visit_date', 'label' => 'Visit date', 'type' => 'date'),
        array('id' => 'count_visits', 'label' => 'Count', 'type' => 'number')
    );
    foreach($results AS $result) {
        $time = strtotime($result[0]['visit_date']);
        $dateJs = 'Date('.date("Y", $time).', '.(date('n', $time) - 1).', '.date('j', $time).')';
        $row = array(
            'c' => array(
                array('v' => $dateJs),
                array('v' => $result[0]['count_visits']),
             )
        );
        $chartData['rows'][] = $row;
    }
    //make the data available for view
    $this->set('chartData', json_encode($chartData);
    

    in your view use :

    var data = new google.visualization.DataTable($chartData);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Form element: $Form=new Zend_Form; $Form->setAction($this->view->url(array('controller'=>'auth','action'=>'create'),null,true)) ->setMethod('post') ->setAttrib('id','auth-form') ->removeAttrib('enctype'); As can be
The comment form => echo $this->Form->create('Comment',array('url'=>array('controller' => 'comments', 'action' =>'add', $listposts['Post']['id']) ) ); echo
This is the form for comment: echo $this->Form->create('Comment',array('url'=>array('controller' => 'comments', 'action' =>'add', $listposts['Post']['id']) )
This is one => echo $this->Html->link('Edit', array('controller'=>'comments','action'=>'edit',$comment['Comment']['id'])); This is another one in form =>
I am trying to create an admin instance through my admins controller create action,
If i have a controller action Create that returns a view with the following
I tried to create base controller and write in it: Ext.define('My.Users.controller.Role', { extend :
I'm trying to create a controller to upload photos in my MVC4 application. But
I'm trying to create a controller which retrieves certain blogposts from the blog. I
What's the best way to dynamically create a controller in Rails. I've got a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.