score:3
Accepted answer
Instead of using your x
array as a base for mapping the y
values, do it the other way around:
const yValueArray = [
[0, 20, 30, 40, 50],
[0, 200, 250, 400, 450]
];
const xValueArray = [0, 10, 20, 30, 40];
const data = yValueArray.map(data =>
data.map((d, i) => ({
x: xValueArray[i],
y: d
}))
);
console.log(data);
This will work for how many inner arrays you have on yValueArray
.
Then use that data in your enter selection:
const yValueArray = [
[0, 20, 30, 40, 50],
[0, 200, 250, 400, 450]
];
const xValueArray = [0, 10, 20, 30, 40];
const data = yValueArray.map(data =>
data.map((d, i) => ({
x: xValueArray[i],
y: d
}))
);
const x = d3.scaleLinear();
const y = d3.scaleLinear();
const lineFunction = d3.line()
.x(function(d) {
return x(d.x);
})
.y(function(d) {
return y(d.y);
})
.curve(d3.curveLinear);
const path1 = d3.select("svg").selectAll(null)
.data(data)
.enter()
.append("path")
.attr("d", lineFunction)
.attr("stroke", "blue")
.attr("stroke-width", 2)
.attr("fill", "none");
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<svg width="300" height="400"></svg>
Source: stackoverflow.com
Related Query
- How to draw a dynamic line chart based on an infotable?
- How to draw a line / link between two points on a D3 map based on latitude / longitude?
- How do I draw a zoomable d3 line chart over time?
- how to draw line chart by d3.js?
- d3.js How to draw line chart with vertical x axis labels
- Need to draw a quarterly line chart based on monthly data using d3
- How to draw line plus bar chart using highcharts/d3.js or any other chart library?
- How to draw a d3.js line chart using angularjs directives
- How to Use sql queries to draw dynamic chart in NV D3?
- On common x-axis how to draw a chart with different unit of dataset array (multi line chart)
- How to send dynamic data to c3chart angular line chart
- How can I use d3.layout.stack with this code to draw a line chart using D3.js?
- How to draw straight line in d3.js (horizontally and vertically)
- How to draw line with arrow using d3.js
- Draw a vertical line representing the current date in d3 gantt chart
- How to draw a *simple* line segment with d3.js?
- How to draw logarithmic line charts with nvd3
- how to style style line chart points in c3js
- How to avoid overlapping tooltips of multi-series line chart d3.js
- how do you draw linear line in scatter plot with d3.js
- Draw D3 Simple Line chart With an Array
- How do I get my area filled beneath my d3 line chart to be a gradient?
- How to add a line on x-axis on a horizontal bar chart in d3
- How to plot animated line chart using d3 while the data table is required to be updated every 1 second?
- How to style slider on chart in D3.js and show label of dynamic y-value?
- How do I always keep my d3 line chart rollover text always visible?
- How to add a fixed range vertical line to a D3.js chart
- How to change the viewfinder (focus chart) of a NVD3 line chart programmatically?
- D3: How to use exit().remove() for Multi-Series Line Chart
- draw a horizontal line in d3. the following code shows a line drawn based x n y value of pixels
More Query from same tag
- d3.js not being referenced property in HTML
- csv filsize limit when importing with d3.csv
- How to read(get) Y-Axis Scale Values from D3.js Chart?
- How to draw a vertical line with dimple?
- I am not able to add put labels in 3d donut chart using d3.js
- Adapting a streamgraph to a stock portfolio visualization using d3 js
- Text appended to path does not show
- Y axis some text cut of in d3js bar chart
- d3.js label bars of bar chart
- D3.js - layouts - value accessor
- D3.JS Y-axis label issue
- horizontal legend using d3 gets cut off on right side
- How can i convert sunburst graph as a Directive to use with angularjs?
- d3.js Stacked Bar Chart working for one dataset but not the other
- How do I create a dynamic domain?
- Show associated metric from CSV in tooltip when county is hovered
- Stacked-to-Grouped Bars Transition
- How can I remove non-integer ticks while zooming in with D3.js?
- Completed a row before a selectall
- How to scale SVG elements separately.
- d3 dependency loading before d3
- How to add a properly scaled y axis to a stacked d3 bar chart
- Zero-index problem and filtering issue in shiny
- D3 zoomable sunburst not zooming (with data from R)
- How to decide dimensions and groups in dc.js?
- Turn string to array name in D3 Javascript
- Dynamically Change the Append D3
- D3.js ~ Drop down lists with dependency based on dataset
- cs.js - displaying only one line at the same time (in multi-line chart)?
- D3 not adding path to a group over a map