score:0
var data_set = [{
'date': '2009-03-23',
'raw': 25,
'raw2': 25,
'asset': 'a'
},
{
'date': '2009-03-24',
'raw': 28,
'raw2': 25.4,
'asset': 'a'
},
{
'date': '2009-03-25',
'raw': 26,
'raw2': 25.37,
'asset': 'a'
},
{
'date': '2009-03-26',
'raw': 22,
'raw2': 25.03,
'asset': 'a'
},
{
'date': '2009-03-27',
'raw': 19,
'raw2': 24.42,
'asset': 'a'
},
{
'date': '2009-03-28',
'raw': 23,
'raw2': 24.28,
'asset': 'a'
}
]
var margin = {
top: 30,
right: 50,
bottom: 30,
left: 50
};
var svgwidth = 600;
var svgheight = 1000;
var graphwidth = svgwidth - margin.left - margin.right;
var graphheight = svgheight - margin.top - margin.bottom;
// var parsedate = d3.timeparse("%d/%m/%y");
var parsedate = d3.timeparse("%y-%m-%d");
var x = d3.scaletime().range([0, graphwidth]);
var y = d3.scalelinear().range([graphheight, 0]);
var z = d3.scaleordinal(d3.schemecategory10); // for colours
var xaxis = d3.axisbottom().scale(x).ticks(10);
var yaxis = d3.axisleft().scale(y).ticks(10);
// need to create the lines manually for each bit of data
var line = d3.line()
.x(function(d) {
return x(d.date);
})
.y(function(d) {
return y(d.y);
});
// creates the svg area within the div on the dom
// just doing this once
var svg = d3.select("#graphdiv")
.append("svg")
.attr("width", svgwidth)
.attr("height", svgheight)
var g = svg.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")")
.call(d3.zoom().on("zoom", function() {
svg.attr("transform", d3.event.transform)
}));
// add the x axis
g.append("g").attr("class", "x axis")
.attr("transform", "translate(0," + graphheight + ")")
.call(xaxis);
// text label for x axis
g.append("text")
.style("text-anchor", "middle")
.text("timeseries dates")
.attr("transform", "translate(" + (graphwidth / 2) + " ," + (graphheight + margin.top) + ")");
// add the y axis
g.append("g")
.attr("class", "y axis")
.call(yaxis);
// text label for the y axis
g.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0 - margin.left)
.attr("x", 0 - (graphheight / 2))
.attr("dy", "1em")
.style("text-anchor", "middle")
.text("price points");
function drawgraph(data_set) {
let pathdata = []
//assume 2 paths
pathdata.push([])
pathdata.push([])
// pass in the data here
data_set.foreach(function(d) {
let path0 = {}
let path1 = {}
path0.date = parsedate(d.date)
path1.date = parsedate(d.date)
path0.y = +d.raw
path1.y = +d.raw2
pathdata[0].push(path0)
pathdata[1].push(path1)
});
x.domain(d3.extent(data_set, function(d) {
return parsedate(d.date);
}));
y.domain([
d3.min(data_set, function(d) {
return math.min(d.raw, d.raw2)
}),
d3.max(data_set, function(d) {
return math.max(d.raw, d.raw2)
})
]);
var lines = g.selectall(".path")
.data(pathdata)
lines.exit().remove()
var enter = lines.enter()
.append("path")
.attr("class", "path")
.style("stroke", (d, i) => z(i))
var merge = enter.merge(lines)
.attr("d", line)
}
// display initial chart
window.onload = drawgraph(data_set)
// push new data every 5 seconds for a specific date
var h = setinterval(function() {
data_set.push({
'date': '2009-03-29',
'raw': math.floor(math.random() * 50),
'raw2': math.floor(math.random() * 25),
'asset': 'a'
}, {
'date': '2009-03-30',
'raw': math.floor(math.random() * 50),
'raw2': math.floor(math.random() * 25),
'asset': 'a'
}, {
'date': '2009-03-31',
'raw': math.floor(math.random() * 50),
'raw2': math.floor(math.random() * 25),
'asset': 'a'
}, {
'date': '2009-04-01',
'raw': math.floor(math.random() * 50),
'raw2': math.floor(math.random() * 25),
'asset': 'a'
}, {
'date': '2009-04-02',
'raw': math.floor(math.random() * 50),
'raw2': math.floor(math.random() * 25),
'asset': 'a'
}, {
'date': '2009-04-03',
'raw': math.floor(math.random() * 50),
'raw2': math.floor(math.random() * 25),
'asset': 'a'
});
drawgraph(data_set);
clearinterval(h); //doing this so that it doesnt spam - if i uncomment this, it will keep spamming new lines
}, 5000); >
body {
margin: 0;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
path {
fill: none;
stroke: black
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.13.0/d3.min.js"></script>
<div id="graphdiv"></div>
there are few issues with your current code that need addressing:
to create 2 lines based on data, it would be best for the array to contain 2 elements, each with its own array of data points. this will allow the line function to work as intended and be resuable across each line you want to create. in the example below, i create a new dataset ('pathdata') to achieve this.
the enter/update/merge pattern needs to updating so that you only create the lines once, and thereafter just update the path ('d' attr) when new data arrives
Source: stackoverflow.com
Related Query
- d3js - How to update an existing svg path in a simple d3 line chart with new data flowing through?
- How to update d3.js bar chart with new data
- D3 How to update the chart after selection from drop down menu with new data
- How to update an svg path with d3.js
- How to update the fill color on existing svg elements with d3.js?
- d3js stack chart with simple data
- D3.js: plot dots on the existing line in a multiseries line chart with long formatted data
- update d3 chart with new data
- Simple D3.js line chart with data that are values collect every hours
- How to update all data representing an SVG line using d3?
- How to smoothly update a D3 v4 force diagram with new data
- How do you update an existing svg element with the d3v5 join syntax
- How can I scale my map to fit my svg size with d3 and geojson path data
- How do you get JSON data and create a line chart with d3.js?
- how to update d3 chart when receiving new DATA
- How do you add multiple lines to a D3JS line chart where the depedent variable data sources start at different positions along the range?
- Making a d3 chart update with new data
- How to update data in d3 chart with an onchange slider event?
- Update chart with new data not working
- How do I update my multiple line chart data in D3?
- Line chart - how to display data points whilst line path is being drawn
- How to modify tags of an already existing svg object with data from json file
- How to update a d3 grouped barchart with new data by using join in v6
- D3.JS how to load 3 csv files and change data on line chart with button click event?
- replacing null data d3js with focus context chart line
- d3 Animated Line Chart with Path and SVG
- How to update axis in a chart with dispatch events d3js
- How to update (but not redraw) a d3-geomap with new data
- How to render multi line chart with DC.js with the following type of data - JAVASCRIPT (DC.js)
- How to update selection with new data in D3?
More Query from same tag
- d3.js Horizontal bar graph - keeping Y axis in the middle of the graph
- How to zoom and translate to center of force directed graph in D3
- Error with .compose using dc.js
- Detecting line closest to the mouse pointer with interrupted lines
- One of four bar charts showing up, legend missing D3
- How to add string text and a variable in d3.js text attribute?
- D3 mouse interactivity issues, circles not appearing at data points
- Angular 1.5: How to pass async data to child component
- d3.js on-demand data loading to get the next node for a tree
- Tick marks on both sides of axis on d3 scale
- Code to add mouseover functionality to line chart
- What is the correct way to import and use d3 and its submodules in ES6?
- Drawing a fixed-length circle in D3.js
- Dynamic Node Addition/Updation on D3 js Network Graph on button click
- Integrating D3 with Angular-cli
- D3 X-value mouseover - function returns NaN
- Changing text attribute based upon event handler in d3.js
- Parsing nodes from links in a graph
- right JSON format structure for Streamgraph example D3
- Odd JavaScript Syntax that looks like a Python tuple
- Changing position of d3 svg box in browser window
- Confusion over TopoJSON Format
- Create a network graph using d3.js
- d3.nest() sum for object array data
- Data Join with Custom Key does not work as expected
- Curved line on d3 force directed tree
- D3 Pie Chart Label Data Not Working
- d3 axis tick alignment
- How to show only limited number of records in box plot dc.js
- D3: How to pull JSON from Github API