Chart Jquery entering array values

Asked

Viewed 161 times

0

So it’s the following guys, I have 2 array variables that have been converted from php to javascript, so far everything ok. I used Alert to see if it showed and worked. What I intended was to store these 2 arrays within the date variable.

< script type = "text/javascript"
src = "https://www.gstatic.com/charts/loader.js" > < /script>

  <
  script type = "text/javascript" >

  <?php
$sql2 = "SELECT tag,contador FROM tags";
$result2 = $mysqli->query($sql2);

$tagparatudo = array();
$contadorparatudo = array();
while ($row2 = $result2->fetch_assoc())
  {
  $tagparatudo[] = $row2["tag"];
  $contadorparatudo[] = $row2["contador"];


  }

  $js_array = json_encode($tagparatudo);
  $array2 = json_encode($contadorparatudo);
  echo "var tags = ". $js_array. ";\n";
  echo "var contagem = ". $array2. ";\n";
  ?>

alert(contagem);
alert(tags);



// Load google charts
google.charts.load('current', {
  'packages': ['corechart']
});
google.charts.setOnLoadCallback(drawChart);

// Draw the chart and set the chart values
function drawChart() {
  var data = google.visualization.arrayToDataTable([
    ['Something', 'Here'],
    ['Work', 5],
    ['Newthing', 5],
    ['Newthingz', 5]
  ]);
  // Optional; add a title and set the width and height of the chart
  var options = {
    'title': 'Tag Ranks',
    'width': 450,
    'height': 400
  };

  // Display the chart inside the <div> element with id="piechart"
  var chart = new google.visualization.PieChart(document.getElementById('piechart'));
  chart.draw(data, options);
} <
/script>
<div id="piechart"></div>

A google Chart will be created with the values of the variable date and what I intended was something like :

function drawChart() {
  var data = google.visualization.arrayToDataTable([
    ['Something', 'Here'],

um ciclo for{
        [array tags, array contagem]
}
  ]);

2 answers

0

Something that might be wrong is the following excerpt:

 echo "var tags = ". $js_array. ";\n";
 echo "var contagem = ". $array2. ";\n";

Because json_encode returns a string, it is necessary to quote in this way:

echo "var tags = '". $js_array. "';\n";
echo "var contagem = '". $array2. "';\n";
  • Bilico, it is not necessary to put the quotes because I am matching the variables within php and in javascript when I do Lert, the values within the variable appear correctly however we are deviating from the question.. How do I enter each array into that "date" variable. Example: instead of having ['Work',5] I wanted [variable tags, variable count].

  • try in PHP to do the following array: $array['tags' => $js_array, 'count' => $array2]; json_encode($array);

  • to then use in javascript: JSON.parse('$array'); then you will have a JSON object

0

If your "Alert" is working and showing the data correctly, then it seems to me that what you need is to adjust the structure of the arrays you are mounting.

For example, the variable "date" must be an array composed of arrays:

[
 ['Something', 'Here'],
 ['Work', 5],
 ['Newthing', 5],
 ['Newthingz', 5]
]

And what you’re generating in PHP > JS are two different arrays:

['Item 1', 'Item 2', 'Item 3']
['Valor 1', 'Valor 2', 'Valor 3']

Browser other questions tagged

You are not signed in. Login or sign up in order to post.