score:1
The approach I'd take is:
- extract all x values, where at least one line has a value specified.
- create scale for each line, where domain is its all x points, and range is its all y points
- loop through the x list, get interpolated y value on each line, and add together.
Admittedly I'm not too familiar with d3-interpolation package, so there might be a more preferable way to execute this. Still, this works.
const server1 = [
{ date: '2019-06-15T00:22:25.000Z', online: 451 },
{ date: '2019-06-15T01:08:58.000Z', online: 499 },
{ date: '2019-06-15T02:25:35.000Z', online: 464 },
{ date: '2019-06-15T13:25:42.000Z', online: 252 },
{ date: '2019-06-15T15:45:24.000Z', online: 247 },
{ date: '2019-06-15T17:02:09.000Z', online: 254 },
{ date: '2019-06-15T20:11:00.000Z', online: 300 },
{ date: '2019-06-15T21:22:33.000Z', online: 296 },
{ date: '2019-06-15T22:24:58.000Z', online: 298 },
{ date: '2019-06-15T22:58:18.000Z', online: 270 }
];
const server2 = [
{ date: '2019-06-15T00:14:18.000Z', online: 599 },
{ date: '2019-06-15T00:58:56.000Z', online: 554 },
{ date: '2019-06-15T02:15:13.000Z', online: 505 },
{ date: '2019-06-15T13:32:19.000Z', online: 221 },
{ date: '2019-06-15T15:19:08.000Z', online: 265 },
{ date: '2019-06-15T16:04:55.000Z', online: 277 },
{ date: '2019-06-15T17:31:16.000Z', online: 275 },
{ date: '2019-06-15T18:41:16.000Z', online: 303 },
{ date: '2019-06-15T20:05:41.000Z', online: 333 },
{ date: '2019-06-15T21:39:44.000Z', online: 288 },
{ date: '2019-06-15T22:46:01.000Z', online: 277 },
{ date: '2019-06-15T23:29:16.000Z', online: 264 }
];
const server3 = [
{ date: '2019-06-15T00:10:18.000Z', online: 321 },
{ date: '2019-06-15T00:54:56.000Z', online: 300 },
{ date: '2019-06-15T02:11:13.000Z', online: 280 },
{ date: '2019-06-15T13:28:19.000Z', online: 110 },
{ date: '2019-06-15T15:15:08.000Z', online: 130 },
{ date: '2019-06-15T16:01:55.000Z', online: 133 },
{ date: '2019-06-15T17:25:16.000Z', online: 140 },
{ date: '2019-06-15T18:37:16.000Z', online: 172 },
{ date: '2019-06-15T20:01:41.000Z', online: 180 },
{ date: '2019-06-15T21:35:44.000Z', online: 201 },
{ date: '2019-06-15T22:41:01.000Z', online: 210 },
{ date: '2019-06-15T23:23:16.000Z', online: 197 }
];
var x_list = [].concat(server1, server2, server3)
.map(d => new Date(d.date))
.sort(d3.ascending);
var servers_scales = [server1, server2, server3].map(s => {
return d3.scaleLinear()
.domain(s.map(d => new Date(d.date)))
.range(s.map(d => d.online));
});
var combinedData = x_list.map(x => {
var val = 0;
for (var i = 0; i < servers_scales.length; i++)
val += servers_scales[i](x);
return {
date: x,
online: val
};
});
const combined = [].concat(server1, server2, server3);
const margin = {top: 50, right: 50, bottom: 50, left: 50};
const width = window.innerWidth - margin.left - margin.right;
const height = window.innerHeight - margin.top - margin.bottom;
const dateMin = d3.min(combined, d => new Date(d.date));
const dateMax = d3.max(combined, d => new Date(d.date));
const onlineMin = d3.min(combined, d => d.online);
const onlineMax = d3.max(combined, d => d.online);
const xScale = d3
.scaleTime()
.domain([dateMin, dateMax])
.range([0, width]);
const yScale = d3
.scaleLinear()
.domain([0, onlineMax * 3])
.range([height, 0]);
const line = d3
.line()
.x(d => xScale(new Date(d.date)))
.y(d => yScale(d.online))
.curve(d3.curveMonotoneX);
const 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 + ")");
svg
.append('path')
.datum(server1)
.attr('class', 'line')
.attr('fill', 'none')
.attr('stroke', 'blue')
.attr('d', line);
svg
.append('path')
.datum(server2)
.attr('class', 'line')
.attr('fill', 'none')
.attr('stroke', 'red')
.attr('d', line);
svg
.append('path')
.datum(server3)
.attr('class', 'line')
.attr('fill', 'none')
.attr('stroke', 'green')
.attr('d', line);
svg
.append('path')
.datum(combinedData)
.attr('class', 'line')
.attr('fill', 'none')
.attr('stroke', 'orange')
.attr('d', line);
svg
.append("g")
.attr("class", "y axis")
.call(d3.axisLeft(yScale));
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(xScale));
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
Source: stackoverflow.com
Related Query
- How can I properly aggregate / group multiple line graphs into one overall graph with d3.js when the x-values aren't matching exactly?
- How to read multiple files to plot on one line graph in D3.js
- How can I draw one axis frequency distribution graph in d3?
- How can I connect two parent node into one child node and how to make a tooltip for each node in the tree pragmatically? in D3 js (SVG)
- How can I make convex hulls of multiple group network nodes in D3.js
- How can I clone and modify an existing group into a selection?
- how can i group JSON data into the weeks according to calender?
- How can I color one Y-axis line different from the other grid-lines?
- How can I determine the overall size of a force directed graph
- how can i get a label on my lines in a line graph using d3.js
- How can I display tooltips from multiple maps when mousover on one of the maps in D3js
- How to make a mouseover interactive line graph with multiple data series and 2 y axes?
- How to export multiple html tables and multiple d3 generated graphs into a single pdf
- How to create one group with multiple dimension on dc.js
- Using D3, how can I transition a line one point at time?
- How do i plot multiple lines in the same line graph using the D3 framework?
- How can I append text to and render that text from a line in a force directed graph in D3.js?
- how can i group JSON data and generate a graph of each group in D3js
- How can I get my two d3 graphs to appear next to one another?
- How to hide all graphs with one click of a button in a scatterplot graph legend
- How can I add extra range to d3.js line graph
- How can I zoom into this graph using D3v6
- d3js - How to plot a multi line chart chart for multiple groups of items that can be updated?
- How to link multiple graph networks in d3js so that an event in one calls the same event in other networks
- How to split single line into multiple lines?
- How can I make an svg circle always appear above my graph line path?
- How to set multiple attributes with one value function?
- How to properly get text width to center labels above graph bars?
- How can I show a graph using d3 force layout with initial positions set and no movement at all?
- Optimising a group of dc.js line graphs
More Query from same tag
- Formatting numbers with commas in D3
- Get computed histogram bin thresholds
- Get Angular to action ng-if in new elements generated by d3.js
- How to structure .csv file to allow D3.js to associate a line with a specified color?
- d3.js not reading JSON (d3.js + dc.js + crossfilter)
- Transition from x-axis:Bar Chart
- D3: How to use colors stocked within the array for slices of pie?
- d3: tree layout from flare.json - how to include only some of nodes?
- Interactive pie charts: Associate a click event with a slice of a pie chart
- Inserting title in line chart on d3.js
- D3 tooltip fix positioning
- Ajax for Javascript content not working
- Unable to add text to circles in d3
- D3 v4 collapsible tree using the d3 link generator
- Why is my reducer behaving differently between the first filter and subsequent filters applied in dc.js?
- Create contour map
- Word Clouds using Stanford NLP libraries
- Will chart that reads embedded JSON also read JSON file?
- D3 positive negative updating data
- Adding a drag icon to Focus+Context via Brushing Chart
- Why is there a triangle missing from the return value of d3.voronoi.triangles?
- How can i process huge javascript array (array of 20 mil coordinates) in browser side efficiently?
- Plotly.js Adding markers adds padding to x-axis
- How to load multiple files with Queue.js and D3.js?
- Export D3 Svg charts(i.e 5 charts in a Page) into single Image
- Displaying coordinates in D3 from JSON array
- d3.js: click event in a bubble/scatter chart
- Pushing D3.js output through fabric.js for IE8 support?
- Why does d3 date axis fail in Firefox for some particular date domains?
- How to set fixed no. of ticks on axis in d3.js?