|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <!-- |
| 4 | + (c) 2020 Copyright, Real-Time Innovations. All rights reserved. |
| 5 | + No duplications, whole or partial, manual or electronic, may be made |
| 6 | + without express written permission. Any such copies, or revisions thereof, |
| 7 | + must display this notice unaltered. |
| 8 | + This code contains trade secrets of Real-Time Innovations, Inc. |
| 9 | + --> |
| 10 | + <head> |
| 11 | + <meta charset="utf-8" /> |
| 12 | + <title>RTI Connector for Javascript Example - Chart</title> |
| 13 | + <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x --> |
| 14 | + <script src="/socket.io/socket.io.js"></script> |
| 15 | + <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script> |
| 16 | + <script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v6.0.1/build/ol.js"></script> |
| 17 | + </head> |
| 18 | + <body> |
| 19 | + <!-- Load the D3 library --> |
| 20 | + <script src="http://d3js.org/d3.v3.min.js"></script> |
| 21 | + <script> |
| 22 | + // The number of datapoints to be displayed |
| 23 | + var n = 40; |
| 24 | + // The dataset to be displayed by the line |
| 25 | + var dataset = d3.range(n).map(() => d3.random.normal(0, .2)); |
| 26 | + |
| 27 | + var margin = { top: 20, right: 20, bottom: 20, left: 40 }, |
| 28 | + width = 960 - margin.left - margin.right, |
| 29 | + height = 500 - margin.top - margin.bottom; |
| 30 | + |
| 31 | + // Configure the X scale to use the range of use our dataset |
| 32 | + var x = d3.scale.linear() |
| 33 | + .domain([0, n - 1]) |
| 34 | + .range([0, width]); |
| 35 | + |
| 36 | + // Configure the Y scale |
| 37 | + var y = d3.scale.linear() |
| 38 | + .domain([0, 250]) |
| 39 | + .range([height, 0]); |
| 40 | + |
| 41 | + var line = d3.svg.line() |
| 42 | + .x(function(d, i) { return x(i); }) |
| 43 | + .y(function(d, i) { return y(d); }); |
| 44 | + |
| 45 | + var svg = d3.select("body").append("svg") |
| 46 | + .attr("width", width + margin.left + margin.right) |
| 47 | + .attr("height", height + margin.top + margin.bottom) |
| 48 | + .append("g") |
| 49 | + .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); |
| 50 | + |
| 51 | + svg.append("defs").append("clipPath") |
| 52 | + .attr("id", "clip") |
| 53 | + .append("rect") |
| 54 | + .attr("width", width) |
| 55 | + .attr("height", height); |
| 56 | + |
| 57 | + svg.append("g") |
| 58 | + .attr("class", "x axis") |
| 59 | + .attr("transform", "translate(0," + y(0) + ")") |
| 60 | + .call(d3.svg.axis().scale(x).orient("bottom")); |
| 61 | + |
| 62 | + svg.append("g") |
| 63 | + .attr("class", "y axis") |
| 64 | + .call(d3.svg.axis().scale(y).orient("left")); |
| 65 | + |
| 66 | + var path = svg.append("g") |
| 67 | + .attr("clip-path", "url(#clip)") |
| 68 | + .append("path") |
| 69 | + .datum(dataset) |
| 70 | + .attr("class", "line") |
| 71 | + .attr("d", line); |
| 72 | + |
| 73 | + function tick(number) { |
| 74 | + // Push a new data point onto the back |
| 75 | + dataset.push(number); |
| 76 | + // Redraw the line, and slide it to the left |
| 77 | + path |
| 78 | + .attr("d", line) |
| 79 | + .attr("transform", null) |
| 80 | + .attr("fill", "none") |
| 81 | + .attr("stroke", "steelblue") |
| 82 | + .attr("stroke-width", 1.5) |
| 83 | + .transition() |
| 84 | + .attr("transform", "translate(" + x(-1) + ",0)") |
| 85 | + .each("end", null); |
| 86 | + // Pop the old data point off the front |
| 87 | + dataset.shift(); |
| 88 | + } |
| 89 | + </script> |
| 90 | + |
| 91 | + <h1>Chart D3 Example</h1> |
| 92 | + <h2>Current Data</h2> |
| 93 | + <p id="topic"></p> |
| 94 | + <p id="x-position"></p> |
| 95 | + <script src="/socket.io/socket.io.js"></script> |
| 96 | + |
| 97 | + <script> |
| 98 | + var socket = io.connect('http://127.0.0.1:7400'); |
| 99 | + var xPos = document.getElementById('x-position'); |
| 100 | + var topic = document.getElementById('topic'); |
| 101 | + socket.on('square', function (data) { |
| 102 | + console.log('Got udpate from the server for square'); |
| 103 | + xPos.innerHTML = data.x |
| 104 | + topic.innerHTML = "Square" |
| 105 | + tick(data.x); |
| 106 | + }); |
| 107 | + socket.on('triangle', function (data) { |
| 108 | + console.log('Got udpate from the server for triangle'); |
| 109 | + xPos.innerHTML = data.x |
| 110 | + topic.innerHTML = "Triangle" |
| 111 | + tick(data.x); |
| 112 | + }); |
| 113 | + socket.on('circle', function (data) { |
| 114 | + console.log('Got udpate from the server for circle'); |
| 115 | + xPos.innerHTML = data.x |
| 116 | + topic.innerHTML = "Circle" |
| 117 | + tick(data.x); |
| 118 | + }); |
| 119 | + </script> |
| 120 | + </body> |
| 121 | +</html> |
0 commit comments