To learn more, I’m gridding a Java script and experimenting with various projects. I am stuck at passing value. Where I am generating random numbers through the setinterval function, I am able to generate random numbers, and it is working fine. But I want to pass that random number to Google Charts so that charts work like real-time data. So what I was planning was to generate that random number, and through inner html, I will insert it into the span element, and later, through “.value,”, I will get the value of the span element and pass it to the charts. but it was not working so i console.log(output.value ) i am getting the output as undefined
so when i console.log(value.output) inside that setinterval function it shows the correct output .
here’s my code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<title>Document</title>
</head>
<body>
<h3>
Random number from 0 to 9:
<h1 id="foo"></h1>
</h3>
<div id="chart_div" style="width: 400px; height: 120px;"></div>
<script>
window.onload = function() {
var output = document.getElementById('foo');
setInterval( function () {
output.innerHTML =Math.floor(Math.random() * 100);
}, 500);
}
google.charts.load('current', {'packages':['gauge']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Label', 'Value'],
['Memory', 34],
['CPU', 55],
['Network', 68]
]);
var options = {
width: 400, height: 120,
redFrom: 90, redTo: 100,
yellowFrom:75, yellowTo: 90,
minorTicks: 5
};
var chart = new google.visualization.Gauge(document.getElementById('chart_div'));
chart.draw(data, options);
console.log(foo.value);
}
</script>
</body>
</html>
In this programme basically number will generate randomly and it will be disaplyed in h1 tag . So I’m going to get that value from h1 and feed it into Google Charts. But it wont work rather charts get hide. So i just want to pass the output value to ['Memory', {here}],
here . As a result, Google Charts operate in real-time because they derive their value from randomly generated random numbers.
Thank You