score:2
a friend of mine solved the problem! A chunk of data already inside array data drawline() other data coming across the websocket and function updateLine() is called! Remember to change the local ip address in html code. Enjoy :)
<!DOCTYPE html>
<meta charset="utf-8">
<style> /* set the CSS */
.line {
fill: none;
stroke: steelblue;
stroke-width: 2px;
}
</style>
<body>
<!-- load the d3.js library -->
<script src="d3/d3.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
//websocket get new data on close and when
var ws = new WebSocket("ws://192.168.1.118:5678/")
ws.onmessage = function (event) {
var messages = document.getElementsByTagName('ul')[0],
message = document.createElement('li'),
content = document.createTextNode(event.data);
//console.log(typeof(event.data));
var obj = jQuery.parseJSON(event.data);
var close = obj.volume
var when = obj.datetime;
// console.log(close,when);
var a = {"date":parseTime(when),"close":close};
data.push(a);
console.log(data);
updateLine(data);
};
// Preparo la prima sequenza di dati
var data = [
{"date":"9:22:43.111","close":1.98},
{"date":"9:23:43.111","close":2},
/*
{"date":"10:22:43.222","close":5.98},
{"date":"11:32:43.333","close":7.00},
{"date":"11:52:43.444","close":8.70},
{"date":"14:52:43.444","close":10.28},
{"date":"15:52:43.444","close":16.70},
{"date":"16:12:43.222","close":10.98},
{"date":"16:12:43.222","close":3.44},
{"date":"17:32:43.111","close":4.34},
*/
];
// parse the date / time
var parseTime = d3.timeParse("%H:%M:%S.%L");
//per ogni elemento converto la data
data.forEach(function(d) {
d.date = parseTime(d.date);
d.close = +d.close;
});
// Esempio di riferimento per il funzionamento
//http://bl.ocks.org/d3noob/6bd13f974d6516f3e491
// d3js
// Set the dimensions and margins of the graph
var margin = {top: 20, right: 20, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
// Set the ranges
var x = d3.scaleTime().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);
// Define the axes
var xAxis = d3.axisBottom(x)
.ticks(d3.timeMinute.every(30));
var yAxis = d3.axisLeft(y);
// Define the line
var valueline = d3.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.close); });
// append the svg object to the body of the page
// appends a 'group' element to 'svg'
// moves the 'group' element to the top left margin
var svg = d3.select("body")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
// funzione per prmo draw
function drawLine(line_data)
{
// Scale the range of the data
x.domain(d3.extent(line_data, function(d) { return d.date; }));
y.domain([0, d3.max(line_data, function(d) { return d.close; })]);
// Add the valueline path.
svg.append("path")
.attr("class", "line")
.attr("d", valueline(line_data));
// Add the X Axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Add the Y Axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
}
// funzione per aggiornamento
function updateLine(line_data)
{
// Scale the range of the data
x.domain(d3.extent(line_data, function(d) { return d.date; }));
y.domain([0, d3.max(line_data, function(d) { return d.close; })]);
// Select the section we want to apply our changes to
var svg = d3.select("body").transition();
// Make the changes
svg.select(".line") // change the line
.duration(250)
.attr("d", valueline(line_data));
svg.select(".x.axis") // change the x axis
.duration(250)
.call(xAxis);
svg.select(".y.axis") // change the y axis
.duration(250)
.call(yAxis);
}
// Disegno i primi dati
drawLine(data);
// Preparo la prima sequenza di dati
/*
var data = [
{"date":"9:22:43.111","close":2.98},
{"date":"10:22:43.222","close":3.98},
{"date":"11:32:43.333","close":4.00},
{"date":"11:52:43.444","close":7.70},
{"date":"14:52:43.444","close":11.28},
{"date":"15:52:43.444","close":15.70},
{"date":"16:12:43.222","close":12.98},
{"date":"16:12:43.222","close":2.44},
{"date":"17:32:43.111","close":1.34},
];
// parse the date / time
var parseTime = d3.timeParse("%H:%M:%S.%L");
//per ogni elemento converto la data
data.forEach(function(d) {
d.date = parseTime(d.date);
d.close = +d.close;
});
// Aggiorno i dati
updateLine(data);
*/
</script>
</body>
Source: stackoverflow.com
Related Query
- d3.js realtime updating svg line with python websocket data
- NVD3 line chart with realtime data
- d3 line and area charts not updating with new data array
- d3js - How to update an existing svg path in a simple d3 line chart with new data flowing through?
- Updating SVG Element Z-Index With D3
- In d3, how to get the interpolated line data from a SVG line?
- nvd3.js : unable to bind onClick event with the data points in the svg
- How to show a tooltip with value when mouseover a svg line graph using d3.js?
- C3.js SVG visualization to Canvas with canvg - Line charts filled with black rectangles , "ERROR: Element 'parsererror' not yet implemented"
- D3 line graph with arbitrarily many lines (and a specific data format)
- Visualize date specific data with a line chart in browser with javascript
- D3 new data at .data() makes svg to redraw instead of updating nodes position
- Date and time transition from data with line
- Dictionary data ito d3 line chart - Django Python in server side
- Get attributes of existing SVG elements and bind as data with d3.js
- C3js - combination chart with data labels only for line
- D3.js: plot dots on the existing line in a multiseries line chart with long formatted data
- Updating d3js Datamap with dynamic data
- transition a circle into a line by unrolling it with SVG and d3
- Add dots on a multi line D3.js Graph with nested data
- How to draw line charts with complex data structures in d3
- Updating D3 streamgraph with new data
- Updating data values with HTML input and changing the data array
- d3 with bootstrap tooltip: titles not working with data updating
- D3 Line Graph with JSON Data in ISO 8601 Format
- Match SVG text element id with data id using D3
- Updating mpld3 figure with JSON data
- Creating D3 nested JSON Data with Python
- Simple D3.js line chart with data that are values collect every hours
- How to make a mouseover interactive line graph with multiple data series and 2 y axes?
More Query from same tag
- d3 to create paths based on separate array of data
- how to return values from python to html and vice versa
- Loading Data Asynchronously in Python/Flask for d3.js
- d3.js - Bar chart won't display with scale
- How to set default value appearing in dropdown?
- Crossfiltering of JSON data by the most recent time
- Focus+context via brushing mouseovers zoom issue
- d3js cannot append to clipPath
- Why is a cloudflare script being inserted into source for Drupal domain?
- How to make a Web App for a Windows 8 and Android Tablet for D3js application
- Customise the number format in Apache superset
- How to zoom with d3
- Is it possible to display a d3 chart using the mouseover element of another d3 chart
- How to label my axis using words in d3.js
- how to change data via key not index in a dropdown in d3
- D3 exit transition: animation translate to left before remove
- How do I scroll d3 elements while keeping a portion of the svg in a fixed position?
- D3: Scale graph according the data points
- D3.js v5 - Swarm Tree - How to iteratively center swarms around tree nodes
- D3: x-axis with time interval of a year
- D3 force layout from CSV with multiple edges per row
- d3.js v4 minimap or how to have shared zoom on two elements?
- Show Legend in Plotly Python
- d3 Bar Chart not showing up or giving me any errors to work from
- How to render dates on x axis prior to 1900 with d3 .js scatter plot
- Suitable library for combining with D3js , to allowing drawing to webgl (2D)
- Interpolate y-value of a d3.svg.line at a given x-value
- D3js: How to generate X axis labels/ticks if data for one of axis is 0?
- Update textPath after tween transition in D3
- Creating an utility class with imported libraries in Javascript