Reputation: 15494
Im trying to build a very easy table:
Sex %
M 40
F 60
Using php to encode an array but no graph is drawn assuming that the input data is wrong. Can you tell where is the problem?
$table = array();
$table['cols'] = array(
array('label' => 'Sesso', 'type' => 'string'),
array('label' => 'Quantita', 'type' => 'number'));
$rows = array();
$temp = array();
$temp[] = array('v' => 'M');
$temp[] = array('v' => 60);
$rows[] = array('c' => $temp);
$temp = array();
$temp[] = array('v' => 'F');
$temp[] = array('v' => 40);
$rows[] = array('c' => $temp);
$table['rows'] = $rows;
$jsonTable = json_encode($table);
Upvotes: 0
Views: 373
Reputation: 15494
This is what i did:
$table = array(
'cols' => array(
array( 'id' => '', 'label' => 'Sesso', 'pattern' => '', 'type' => 'string' ),
array( 'id' => '', 'label' => '#', 'pattern' => '', 'type' => 'number' )
),
'rows' => array(
array( 'c' => array( array( 'v' => 'Uomini', 'f' => null ), array('v' => 60, 'f' => null) ) ),
array( 'c' => array( array( 'v' => 'Donne', 'f' => null ), array('v' => 40, 'f' => null) ) )
)
);
Upvotes: 0
Reputation: 11588
What happens if you try:
$table = array(
'cols' => array(
array(
'id' => '1',
'label' => 'Sesso',
'type' => 'string'
),
array(
'id' => '2',
'label' => 'Quantita',
'type' => 'number'
)
),
'rows' => array(
array(
'c' => array(
array(
'v' => 'M',
'f' => 60
)
)
),
array(
'c' => array(
array(
'v' => 'F',
'f' => 40
)
)
)
)
);
$json = json_encode($table);
Check out Google's example in their documentation. The JSON structure you should be aiming for is:
{
"cols": [
{"id":"","label":"Topping","pattern":"","type":"string"},
{"id":"","label":"Slices","pattern":"","type":"number"}
],
"rows": [
{"c":[{"v":"Mushrooms","f":null},{"v":3,"f":null}]},
{"c":[{"v":"Onions","f":null},{"v":1,"f":null}]},
{"c":[{"v":"Olives","f":null},{"v":1,"f":null}]},
{"c":[{"v":"Zucchini","f":null},{"v":1,"f":null}]},
{"c":[{"v":"Pepperoni","f":null},{"v":2,"f":null}]}
]
}
Upvotes: 1