Add Google Charts p Property to JSON in PHP

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Add Google Charts p Property to JSON in PHP



I'm trying to add the Google Charts p property to JSON data in PHP for a Table Chart but I can't get it to work.


$options = mysqli_query($con, $sql_sub[3]);

$rows = array();
$table = array();

$table['cols'] = array(
array('label' => 'Information','type' => 'string'),
array('label' => '#Number','type' => 'number'),
);

$row = mysqli_fetch_row($options);
$sub_array = array();
$sub_array = array("v" => 'Dropped by OPTIONS');
$sub_array = array("v" => $row[0]);
$sub_array = array("p" => "'className': 'subrows'");
$rows = array("c" => $sub_array);

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



Where "subrows" is defined as:


.subrows
background-color: darkblue;



On the client side I have the following Javascript function that requests the JSON data and draws the table:


function draw_table(pageID, chartID)

fetch(fullUrls[chartID]).then(function (response)
response.json().then(function (json)
let data = new google.visualization.DataTable(json);
let cssClassNames = 'headerRow': 'headerRow', 'tableCell': 'tableCell', 'oddTableRow': 'oddTableRow' ;
let table = new google.visualization.Table(document.getElementById(`table_div_$graphs[chartID]`));
table.draw(data, height: '100%', allowHtml: true, cssClassNames: cssClassNames );
)
)
.catch(error => console.error('Error:', error))



I expect the background color of this row to be dark blue, but it isn't.



Documentation I found here: https://developers.google.com/chart/interactive/docs/reference#dataview-class, just above the "DataView Class" section.



There are two related questions but I can't apply their solution on my case.



Thanks in advance.



Julian





This just creates some arrays. You haven't written any code to convert anything to JSON. You haven't written any code to output any data. You need to provide a Minimal, Complete, and Verifiable example with a clear problem statement (which should include what output you get and how it differs from what you expect).
– Quentin
Aug 6 at 8:52





If you try the stuff first with a fixed, prepared JSON (without PHP and dynamic values) and it works, then compare it to your dynamically generated JSON.
– DanFromGermany
Aug 6 at 11:26




2 Answers
2



check the Data Format for the Table chart



the className property can only be applied to the cell, not the row


className



you'll have to assign the className to every column,

in this case, use the following php...


className


$sub_array = array();
$sub_array = array("v" => 'Dropped by OPTIONS', "p" => array('className' => 'subrows'));
$sub_array = array("v" => $row[0], "p" => array('className' => 'subrows'));
$rows = array("c" => $sub_array);





Thank you, your solution works perfectly!
– Julian Fleischhauer
Aug 6 at 13:08



You mix already-to-JSON converted stuff with to-be-converted-to-JSON stuff.


$sub_array = array("p" => "'className': 'subrows'");



This should look like this, otherwise it would be double-encoded:


$sub_array = array("p" => array('className' => 'subrows')));



Furthermore, you need to make sure your visualization does make use of the p and className (from the docs):



If your visualization supports any datatable-level properties, it will
describe them; otherwise, this property will be available for
application use.



Also, this syntax looks like a mistake or mix of PHP and Javascript:


let table = new google.visualization.Table(document.getElementById(`table_div_$graphs[chartID]`));



Maybe it should have <?php echo ... ; ?> in it.


<?php echo ... ; ?>






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered