Advertisement
Guest User

Untitled

a guest
Jan 9th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.45 KB | None | 0 0
  1. <?php
  2. const DB_HOST = 'localhost';
  3. const DB_USERNAME = 'pi_select';
  4. const DB_PASSWORD = 'RaspPi';
  5.  
  6. function getData() {
  7.     try {
  8.         $dbh = new PDO('mysql:host='.DB_HOST.';dbname=measurements', DB_USERNAME, DB_PASSWORD);
  9.         $sth = $dbh->prepare('SELECT dtg, vel FROM sensors');
  10.         $sth->execute();
  11.         return $sth->fetchAll(PDO::FETCH_ASSOC);
  12.     }
  13.     catch(PDOException $e)
  14.     {
  15.         echo $e->getMessage();
  16.     }
  17. }
  18.  
  19. $data = getData();
  20. ?>
  21.  
  22.  
  23. <!DOCTYPE html>
  24.     <meta charset="utf-8">
  25.     <style> /* set the CSS */
  26.         body { font: 12px Arial;}
  27.             path {
  28.             stroke: steelblue;
  29.             stroke-width: 2;
  30.             fill: none;
  31.         }
  32.  
  33.         .axis path,
  34.         .axis line {
  35.             fill: none;
  36.             stroke: grey;
  37.             stroke-width: 1;
  38.             shape-rendering: crispEdges;
  39.         }
  40. </style>
  41. <body>
  42.     <!-- load the d3.js library -->
  43.     <script src="http://d3js.org/d3.v3.min.js"></script>
  44. <script>
  45. // Set the dimensions of the canvas / graph
  46. var margin = {top: 30, right: 20, bottom: 30, left: 50},
  47.     width = 800 - margin.left - margin.right,
  48.     height = 270 - margin.top - margin.bottom;
  49. // Parse the date / time
  50.  
  51. var parseDate = d3.time.format("%Y-%m-%d %H:%M:%S").parse;
  52.  
  53. // Set the ranges
  54. var x = d3.time.scale().range([0, width]);
  55. var y = d3.scale.linear().range([height, 0]);
  56.  
  57. // Define the axes
  58. var xAxis = d3.svg.axis().scale(x)
  59.     .orient("bottom");
  60.  
  61. var yAxis = d3.svg.axis().scale(y)
  62.     .orient("left").ticks(5);
  63.  
  64. // Define the line
  65. var valueline = d3.svg.line()
  66.     .x(function(d) { return x(d.dtg); })
  67.     .y(function(d) { return y(d.vel); });
  68.  
  69.  
  70. // Adds the svg canvas
  71. var svg = d3.select("body")
  72.     .append("svg")
  73.     .attr("width", width + margin.left + margin.right)
  74.     .attr("height", height + margin.top + margin.bottom)
  75.     .append("g")
  76.     .attr("transform",
  77.     "translate(" + margin.left + "," + margin.top + ")");
  78.  
  79.  
  80.  
  81. // Get the data
  82. <?php
  83. echo "data=".$json_data.";" ?>
  84. data.forEach(function(d) {
  85.         d.dtg = parseDate(d.dtg);
  86.         d.vel = +d.vel;
  87.     });
  88.  
  89.  
  90.  
  91. // Scale the range of the data
  92. x.domain(d3.extent(data, function(d) { return d.dtg; }));
  93. y.domain([0, d3.max(data, function(d) { return d.vel; })]);
  94.  
  95.  
  96.  
  97. // Add the valueline path.
  98. svg.append("path")
  99.  .attr("class", "line")
  100.     .attr("d", valueline(data));
  101.  
  102. // Add the X Axis
  103. svg.append("g")
  104.     .attr("class", "x axis")
  105.     .attr("transform", "translate(0," + height + ")")
  106.     .call(xAxis);
  107.  
  108. // Add the Y Axis
  109.     svg.append("g")
  110.         .attr("class", "y axis")
  111.         .call(yAxis);
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118. // ** Update data section (Called from the onclick)
  119. function updateData() {
  120.     // Get the data again
  121.     <?php
  122.     getData();
  123.     echo "data=".$json_data.";"
  124.     ?>
  125.     data.forEach(function(d) {
  126.         d.dtg = parseDate(d.dtg);
  127.         d.vel = +d.vel;
  128.  
  129.    });
  130.     // Scale the range of the data again
  131.     x.domain(d3.extent(data, function(d) { return d.dtg; }));
  132.     y.domain([0, d3.max(data, function(d) { return d.vel; })]);
  133.  
  134.     // Select the section we want to apply our changes to
  135.     var svg = d3.select("body").transition();
  136.  
  137.     // Make the changes
  138.         svg.select(".line")   // change the line
  139.             .duration(750)
  140.             .attr("d", valueline(data));
  141.         svg.select(".x.axis") // change the x axis
  142.             .duration(750)
  143.             .call(xAxis);
  144.         svg.select(".y.axis") // change the y axis
  145.             .duration(750)
  146.             .call(yAxis);
  147. }
  148.  
  149.  
  150. </script>
  151.  
  152.  
  153. <div id="option">
  154.     <input name="updateButton"
  155.            type="button"
  156.            value="Update"
  157.            onclick="updateData()" />
  158. </div>
  159.  
  160. </body>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement