Data column(s) for axis #0 cannot be of type string error in google chart
Asked Answered
C

6

5

I tried to populate google chart datatable in server side using PHP.I got JSON file properply, but the Chart not display in client Application. I got error-Data column(s) for axis #0 cannot be of type string . My coding is below here.

After fetching data from database,

$colarray=array(array("id"=>"","label"=>"userid","pattern"=>"","type"=>"number"),array("id"=>"","label"=>"name","pattern"=>"","type"=>"string"));

  $final=array();
    for($i=0;$i<$rows;$i++) 
    {
     $id[$i]=pg_fetch_result($res1,$i,'id');
     $name[$i]=pg_fetch_result($res1,$i,'name');
     $prefinal[$i]=array("c"=>array(array("v"=>$name[$i]),array("v"=>$name[$i])));
     array_push($final,$prefinal[$i]);
    }


    $table['cols']=$colarray;
    $table['rows']=$final;
    echo json_encode($table);

My Output Json:

{
  "cols":[
    {"id":"","label":"userid","pattern":"","type":"number"},
    {"id":"","label":"name","pattern":"","type":"string"}
   ],
  "rows":[
    {"c":[{"v":"101"},{"v":"Aircel"}]},
    {"c":[{"v":"102"},{"v":"Srini"}]},
    {"c":[{"v":"103"},{"v":"Tamil"}]},
    {"c":[{"v":"104"},{"v":"Thiyagu"}]},
    {"c":[{"v":"105"},{"v":"Vasan"}]},
    {"c":[{"v":"107"},{"v":"Senthil"}]},
    {"c":[{"v":"108"},{"v":"Sri"}]},
    {"c":[{"v":"109"},{"v":"Docomo"}]},
    {"c":[{"v":"106"},{"v":"Innodea"}]}
    ]
}

How to solve this issue?

Colonist answered 8/2, 2012 at 14:16 Comment(0)
P
6

You specify type of userid as number... but pass string.. thats causing the problem.

I just wasted 30 mins with the opposite problem ...

Your output json should look like :-

{
  "cols":[
    {"id":"","label":"userid","pattern":"","type":"number"},
    {"id":"","label":"name","pattern":"","type":"string"}
   ],
  "rows":[
    {"c":[{"v":101},{"v":"Aircel"}]},
    {"c":[{"v":102},{"v":"Srini"}]},
    {"c":[{"v":103},{"v":"Tamil"}]},
    {"c":[{"v":104},{"v":"Thiyagu"}]},
    {"c":[{"v":105},{"v":"Vasan"}]},
    {"c":[{"v":107},{"v":"Senthil"}]},
    {"c":[{"v":108},{"v":"Sri"}]},
    {"c":[{"v":109},{"v":"Docomo"}]},
    {"c":[{"v":106},{"v":"Innodea"}]}
    ]
}
Pliny answered 25/2, 2012 at 13:12 Comment(0)
P
13

To extend on @sajal's accurate answer: Change the last line of your code from:

echo json_encode($table);

to:

echo json_encode($table, JSON_NUMERIC_CHECK);

This will tell json_encode to recognize numbers and abstain from wrapping them in quotes (Available since PHP 5.3.3.). http://php.net/manual/en/json.constants.php#constant.json-numeric-check

Place answered 30/10, 2012 at 10:10 Comment(2)
Best solution so far.Unerring
Let me explain the answer so that people can take help of it, if there is no JSON_NUMERIC_CHECK, the data passed from controller to the view seems like: ["23", "362"] 4: (2) ["24", "413"] 5: (2) ["25", "398"] 6: (2) ["26", "516"] and when the check it performed it becomes like this : 21, 119] 2: (2) [22, 4] 3: (2) [23, 362] 4: (2) [24, 413] 5: (2) [25, 398], so it means that it converts the arrays of strings into the arrays of numbers.Unerring
P
6

You specify type of userid as number... but pass string.. thats causing the problem.

I just wasted 30 mins with the opposite problem ...

Your output json should look like :-

{
  "cols":[
    {"id":"","label":"userid","pattern":"","type":"number"},
    {"id":"","label":"name","pattern":"","type":"string"}
   ],
  "rows":[
    {"c":[{"v":101},{"v":"Aircel"}]},
    {"c":[{"v":102},{"v":"Srini"}]},
    {"c":[{"v":103},{"v":"Tamil"}]},
    {"c":[{"v":104},{"v":"Thiyagu"}]},
    {"c":[{"v":105},{"v":"Vasan"}]},
    {"c":[{"v":107},{"v":"Senthil"}]},
    {"c":[{"v":108},{"v":"Sri"}]},
    {"c":[{"v":109},{"v":"Docomo"}]},
    {"c":[{"v":106},{"v":"Innodea"}]}
    ]
}
Pliny answered 25/2, 2012 at 13:12 Comment(0)
T
2

On a BarChart, one of the columns (the second one) has to be a number. That can cause this error message.

Tucky answered 7/1, 2014 at 23:47 Comment(0)
E
1

In your drawChart() function, you are probably using google.visualization.arrayToDataTable, and this does not allow any nulls. Please use addColumn function explicitly

Extemporize answered 6/10, 2015 at 11:30 Comment(0)
Y
0

If the Data format should be like:

data: [
    ["string", "string"], //first Column
    ["string1", number],
    ["string2", number],
    ["string3", number],
]

then you can overcome this error.

Youngran answered 19/11, 2017 at 0:20 Comment(0)
U
0

when you are passing your data from controller you need to do like so: just take an example I have controller and I am sending data through it via group by.

controller: \DB::statement("SET SQL_MODE=''");//this is the trick use it just before your query

$Rspatients = DB::table('reports')
->select(
    DB::raw("day(created_at) as day"),
    DB::raw("Count(*) as total_patients"))

->orderBy("created_at")
->groupBy(DB::raw("day(created_at)"))
->get();


$result_patients[] = ['day','Patients'];
foreach ($Rspatients as $key => $value) {
$result_patients[++$key] = [$value->day,$value->total_patients];
}

    return view('Dashboard.index')
  ->with('result_patients',json_encode($result_patients,JSON_NUMERIC_CHECK));

if there is no JSON_NUMERIC_CHECK, so the data will be array of strings while if there is json check the data will be converted to array of numbers.

before JSON check data:

4: (2) ["24", "413"]
5: (2) ["25", "398"]

After JSON Check data:

4: (2) [24, 413]
5: (2) [25, 398]
Unerring answered 5/6, 2021 at 17:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.