score:1
You need just one modification:
Since you have 3 inner arrays, each SVG has one of such arrays bound as data. So, when it comes to append the circles, instead of...
var circles = canvas.selectAll("circle")
.data(pathData)
.enter()
.append("circle")
..., which points to the whole data array, it should be...
var circles = canvas.selectAll("circle")
.data(d => d)
.enter()
.append("circle")
... which makes the inner arrays (that is, the data bound to each SVG) as the data array for each group of circles.
Here is your working code with that modification only (also, you had a typo on heightScale
):
var canvasWidth = 200,
canvasHeight = 150;
var pathData = [
[
{ "x": 0, "y": 50}, { "x": 20, "y": 20},
{ "x": 60, "y": 40}, { "x": 80, "y": 30},
{ "x": 120, "y": 90}, { "x": 150, "y": 70},
{ "x": 180, "y": 30}, { "x": 190, "y": 90},
],
[
{ "x": 0, "y": 90}, { "x": 20, "y": 20},
{ "x": 30, "y": 80}, { "x": 80, "y": 30},
{ "x": 120, "y": 90}, { "x": 150, "y": 70},
{ "x": 180, "y": 30}, { "x": 190, "y": 90},
],
[
{ "x": 0, "y": 40}, { "x": 20, "y": 20},
{ "x": 30, "y": 80}, { "x": 80, "y": 30},
{ "x": 120, "y": 90}, { "x": 150, "y": 70},
{ "x": 180, "y": 30}, { "x": 190, "y": 90},
]
];
var widthScale = d3.scaleLinear()
.domain([0, 190])
.range([0, canvasWidth - 30]);
var heightScale = d3.scaleLinear()
.domain([0, 90])
.range([0, canvasHeight - 10]);
// Canvas
var container = d3.select("body")
// Create accessor function
var pathFunction = d3.line()
.x(function(d) { return widthScale(d.x); })
.y(function(d) { return heightScale(d.y); })
.curve(d3.curveCatmullRom);
var canvas = container.selectAll("svg")
.data(pathData)
.enter()
.append("svg")
.attr("width", canvasWidth)
.attr("height", canvasHeight)
.append("g")
.attr("transform","translate(20)");
var sparklines = canvas.append("path")
.attr("d", pathFunction)
.attr("stroke", "#19ff9f")
.attr("stroke-width", 2)
.attr("fill","none");
var circles = canvas.selectAll("circle")
.data(d=>d)
.enter()
.append("circle")
.attr("cx", function(d,i) { return widthScale(d.x); })
.attr("cy", function(d,i) { return heightScale(d.y); })
.attr("fill", "black")
.attr("r", 3);
<script src="https://d3js.org/d3.v4.min.js"></script>
score:0
One useful way I've found to inspect a problem like this is to log your "d" value to the console:
.attr("cx", function(d,i) { console.log(d); return widthScale(d.x) })
That will probably highlight that, because you are now using a multidimensional array, the data bound to your circle elements doesn't provide you with immediate access to the data points anymore. Something like the following will probably work:
.attr("cx", function(d,i) { return widthScale(d[i].x) })
Source: stackoverflow.com
Related Query
- How to access the data in an array
- How do I access the array sum here?
- How to access the inner array resulting from d3.groups?
- d3 nested selection : how to access array data
- How to reshaping Data in Javascript from array to object without losing some of the data
- How do I structure the Data array for the following d3 chart, in order to populate with real data
- how to access data from nested json array D3
- How to create a new array using the data from the old one?
- How to make sure that all data are pushed to the array before resolving promise?
- how to access specific data in an array using D3.js v5 for a stacked bar graph tooltip
- How to get the data from given array of objects in d3.js
- How to access the DOM element that correlates to a D3 SVG object?
- How to access the parentNode of a selection to raise an element?
- How to find the max/min of a nested array in javascript?
- D3.js: Get an array of the data attached to a selection?
- How to update elements of D3 force layout when the underlying data changes
- In d3, how to get the interpolated line data from a SVG line?
- D3.js - JSON data array is binding the same array element to everything
- How to load data from an internal JSON array rather than from an external resource / file for a collapsible tree in d3.js?
- How do you access an array of Objects using D3?
- D3 force layout: How to index data by its ID instead of its index in "nodes" array
- how to access data of a d3 SVG Element
- NVD3 - How to refresh the data function to product new data on click
- How to update elements of an HTML that the elements are created using data from a CSV file?
- D3: How to dynamically refresh a graph by changing the data file source?
- How to plot animated line chart using d3 while the data table is required to be updated every 1 second?
- How to change color of the dots in D3.js to reflect data on y scale?
- dc.js - how to get the average of a column in data set
- D3 Library — How To Access Data from JSON in this example
- How to get the data from the c3.js
More Query from same tag
- SVG animation inside svg:use element. Works in Firefox, not in Chrome
- Adjust style for a rectangle as in the image
- Multiline Chart
- D3 enable mouseover AFTER transition
- D3 force layout graph with nodes positioned in a grid
- <circle> attribute transform: Expected number, "translate(NaN,NaN)" in D3 legend of Covid data tracker legend
- Remove vertical lines from the plot d3.js
- Right-justifying an HTML table created in D3
- D3js "bars.enter().attr" is not a function
- Read formatted data with d3.dsv.parseRows
- In SVG, is <path /> identical to <path></path>?
- D3 JS - Uncaught TypeError: Cannot read property 'length' of undefined - seems related to data issue
- Append Image to Existing Table Header
- New layout after opening a group not base on the last layout with cola.js
- C3js Quit curve on data point with zero value
- How to set a tooltip width dynamically?
- Browser tooltip not working in d3.js bar chart
- How to make all the nodes circle the center node?
- Axis/Scale with multiple quantitative (but non-contiguous) domains
- d3 circlepacking how to not zoom out when clicking the same node twice
- D3 line chart doesn't do transition while appending data to chart
- Show a specific row of my (large) CSV File
- Need to connect two nodes of different circle packed layout in d3 and pack layout collapse/expand
- Pretty hover effect (D3 chart)
- How to use json object in d3.js reusable chart pattern
- Add correct value after nesting with d3
- d3js Grid outside the chart area
- Why 'scaleLinear' never used? and Why I have to included as a <script d3.v4.js>?
- d3.js - using slider to dynamically change graph
- Is there a way to apply transitions to numbers formated as currency in D3?