score:1
The problem is caused by the fact that tick
is called immediately and synchronously at the end of the transition. The CPU process executing the JavaScript remains busy updating data and chart, and is not available to do anything else on this tab.
One way to fix this is to use Window.requestAnimationFrame()
.
.on('end', () => {
requestAnimationFrame(tick)
})
The updated snippet below shows this solution in action.
It does not fix other issues not mentioned in the question, like the fact that no data is shown in the chart.
<!DOCTYPE html>
<head></head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js">
</script>
<script>
var t = -1;
var n = 40;
var duration = 750;
var data = [];
console.log('hello');
function next() {
return {
time: ++t,
value: Math.random() * 10
}
}
var margin = {
top: 6,
right: 0,
bottom: 20,
left: 40
},
width = 560 - margin.right,
height = 120 - margin.top - margin.bottom;
var xScale = d3.scaleTime()
.domain([t - n + 1, t])
.range([0, width]);
var yScale = d3.scaleLinear()
.domain([0, 10])
.range([height, 0]);
var line = d3.line()
.x((d) => xScale(d.time))
.y((d) => yScale(d.value));
var svg = d3.select('body').append('p').append('svg');
var chartArea = svg.append('g')
.attr('transform', `translate(${margin.left}, ${margin.top})`);
chartArea.append('defs').append('clipPath')
.attr('id', 'clip2')
.append('rect')
.attr('x', 0)
.attr('y', 0)
.attr('width', width)
.attr('height', height);
chartArea.append('rect')
.attr('class', 'bg')
.attr('x', 0)
.attr('y', 0)
.attr('width', this.chartWidth)
.attr('height', this.chartHeight);
var xAxis = d3.axisBottom(xScale);
var xAxisG = chartArea.append('g')
.attr('class', 'x-axis')
.attr('transform', `translate(0, ${height})`);
xAxisG.call(xAxis);
d3.selectAll('x-axis path').style('stroke', 'red')
.style('stroke-width', 2);
var yAxis = d3.axisLeft(yScale);
var yAxisG = chartArea.append('g').attr('class', 'y-axis');
yAxisG.call(yAxis);
var grids = chartArea.append('g')
.attr('class', 'grid')
.call(d3.axisLeft(yScale).tickSize(-(width)).tickFormat((domain, number) => {
return ""
}));
var pathsG = chartArea.append('g')
.attr('id', 'paths')
.attr('class', 'paths')
.attr('clip-path', 'url(#clip2)');
tick();
function tick() {
console.log('working');
var newValue = {
time: ++t,
value: Math.random() * 10
};
data.push(newValue);
xScale.domain([newValue.time - n + 2]);
xAxisG.transition().duration(500).ease().call(xAxis);
console.log('is it?');
var minerG = pathsG.selectAll('.minerLine').data([data]);
var minerGEnter = minerG.enter()
.append('g')
.attr('class', 'minerLine')
.merge(minerG);
var minerSVG = minerGEnter.selectAll('path').data((d) => [d]);
var minerSVGEnter = minerSVG.enter()
.append('path')
.attr('class', 'line')
.merge(minerSVG)
.transition()
.duration(500)
.ease(d3.easeLinear, 2)
.attr('d', line(data))
.on('end', () => {
requestAnimationFrame(tick)
})
}
</script>
</body>
</html>
Source: stackoverflow.com
Related Query
- D3 line chart and adding data points real time
- Real time data graphing on a line chart with the help of rickshaw and d3.js
- D3 line chart does not show tool tip and data points properly
- Time in x-axis and data for the y-axis for line chart in d3.js doesn't match/show with what (data) is on JSON
- How to put data on line chart when x axis and data has a time range
- How to plot, in Javascript and on a D3.js line graph, real time time data coming from web socket messages via stomp.js?
- how to make real time multi line chart using d3 where data is from api in json format
- Adding Data Label and Marker to the Line chart using d3
- Date and time transition from data with line
- Specifying Ticks on D3 Bar chart with Time Series data and scaleBand
- Adding additional data points to a chart
- d3 adding circles on the highest and lowest value on line chart
- Create a padding for the first and last data points using a time scale
- Incomplete line chart with missing points and missing lines
- Adding tooltips to line graph data points AFTER lines animate
- d3 line and points on map from csv data
- Adding a data overlay to a time series chart
- How do you get JSON data and create a line chart with d3.js?
- Simple Real Time D3 Line chart
- Adding data label in D3 Line chart (for Power BI)
- d3 line and points on map from geojson or csv data
- Line chart - how to display data points whilst line path is being drawn
- D3.JS how to load 3 csv files and change data on line chart with button click event?
- D3 V4 Multi series line chart: Adding data points to lines with same color as lines
- How to Feed Real time data for Live chart in d3
- How to synchronize scatter points and line chart during an update to the data?
- How to toggle data points on and off along with lines in multi-line chart
- d3 real time graph ploting using mysql and streaming sensor data
- d3 real time line chart with labels
- Adding new data point entries to a line chart
More Query from same tag
- D3 color change on mouseover using classed("active",true)
- Make Gist from Github repo for display on bl.ocks.org
- Multi-layered comparison histogram
- Add links between gray nodes - Directed Graph Editor d3.js
- d3js write labels on multiline
- how to update d3 chart when receiving new DATA
- Update d3 data react hooks
- D3 force directed graph node - text is being duplicated when expading
- Extjs + D3; Load and draw onto image
- How can i bind an json data with a key and Name for display
- Getting a d3 time.scale object to have ticks and be quantized
- D3: Links between spouses in a family tree
- Error: <rect> attribute y: Expected length, "NaN"
- how to update data from props to d3 function?
- Detecting line closest to the mouse pointer with interrupted lines
- how to convert d3.html result into element to append?
- D3.js Passing CSV through modified Lee Byron's test algorithm generator in stacked-to-grouped bar graph example
- Arrays not equal
- Date format issue in d3 js
- Dashed line transforms the null values into zeros on the chart
- D3 line chart - Number against time coming straight line (No Error)
- Define JSON Data Attributes to use in Tooltip (Tooltip Shows "Undefined")
- Unable to sort the element in nested json
- Prevent mouseout action after click on an element with D3.js
- Collapse d3js tree to a specified depth
- Update, enter and exit selections in a single chain
- D3 javascript not running in html div
- Pagination with D3.js
- Mapping: Click on a marker, show content next to the map?
- Functions don't recognize variables