Skip to content

Instantly share code, notes, and snippets.

@jyoshida-sci
Created August 3, 2017 15:51
Show Gist options
  • Save jyoshida-sci/292f2d3297d6e41ebff880682070c870 to your computer and use it in GitHub Desktop.
Save jyoshida-sci/292f2d3297d6e41ebff880682070c870 to your computer and use it in GitHub Desktop.
<?php
// This is just an example of reading server side data and sending it to the client.
// It reads a json formatted text file and outputs it.
//$string = file_get_contents("data.json");
//echo $string;
// Instead you can query your database and parse into JSON etc etc
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
//echo json_encode($arr);
//{"a":1,"b":2,"c":3,"d":4,"e":5}
echo <<< EOM
{
"cols": [
{"id":"val1","label":"","pattern":"","type":"number"},
{"id":"val2","label":"","pattern":"","type":"number"},
{"id":"val2","label":"","pattern":"","type":"number"}
],
"rows": [
{"c":[{"v":"-4.0"},{"v":"4"},{"v":null}]},
{"c":[{"v":"-5.0"},{"v":null},{"v":"2"}]},
{"c":[{"v":"-6.0"},{"v":"5"},{"v":null}]}
]
}
EOM;
?>
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
// Load the Visualization API.
google.load('visualization', '1', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
function drawChart() {
var jsonData = $.ajax({
url: "getDatascat.php",
dataType:"json",
async: false
}).responseText;
// Create our data table out of JSON data loaded from server.
var data = new google.visualization.DataTable(jsonData);
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.ScatterChart(document.getElementById('chart_div'));
var options = {
width: 800,
height: 800,
title: 'AAA vs BBB',
hAxis: {title: 'AAA', minValue: -10, maxValue: 10},
vAxis: {title: 'BBB', minValue: -10, maxValue: 10},
legend: 'none'
};
chart.draw(data, options);
}
</script>
</head>
<body>
<!--Div that will hold the chart-->
<div id="chart_div"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment