Struggling with sketching lines in d3.js?

As a first-time user of d3.js and new to JavaScript, I am trying to create a chart that plots three waves, with the third wave partially deconstructing the first two. I have stored the necessary calculations in a JSON object labeled data and have defined three lines - line1, line2, and line3. Despite watching numerous YouTube tutorials and reviewing code snippets on Stack Overflow and Mike Bostock's Wikipedia page, I am struggling to understand why the lines are not being displayed. Could it be an issue with how I am binding the data? While the console does not show any errors, I have noticed that the path contains values of NAN (not a number) for what seems to be the time component in the JSON object. The sample code snippet below showcases a portion that may be causing the problem:

    var line1 = d3.svg.line()
    .x(function(d) { return widthScale(d.time); })
    .y(function(d) { return verticalScale(d.y1); });

var line2 = d3.svg.line()
    .x(function(d) { return widthScale(d.time); })
    .y(function(d) { return verticalScale(d.y2); });

var line3 = d3.svg.line()
    .x(function(d) { return widthScale(d.time); })
    .y(function(d) { return verticalScale(d.y3); });

data.forEach(function(d) {
    d.time = + d.time;
    d.wave1 = d.wave1;
    d.wave2 = d.wave2;
    d.wave3 = d.wave3;
});

In addition, the following code segment may also be contributing to the issue:

    // Why aren't the lines drawing? Is something missing?
canvas.append("g")
    .datum(data)
    .attr("class", "line")
    .attr("d", line1);

I would greatly appreciate any assistance and can provide further details if needed. Thank you in advance!

Link to the jsfiddle of my attempt: https://jsfiddle.net/fdhnqh1d/3/

Answer №1

I've made some corrections to your code snippet in the fiddle https://jsfiddle.net/fdhnqh1d/4/ to address the issue with the generation.

Previously, your line generators were using an undefined property d.y1. I have updated it to d.wave1 to align with the data model you are working with.

var line1 = d3.svg.line()
.x(function(d) { return widthScale(d.time); })
.y(function(d) { return verticalScale(d.wave1); });

With these changes, the output now looks like:

M0,263.5596698504196L1.8,263.5674382668522L3.6,263.5751378843530...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

A step-by-step guide on smoothly transitioning between elements using Vue-Carousel on a click event

Today marks my first question here and I'm excited! Can anyone guide me on how to implement sliding on click element with Vue-Carousel? I want to slide to the right with just a single click on my input button. Here's the code snippet I have base ...

Leveraging Angular to retrieve images from Google Feed API

I'm currently working on developing an RSS reader and trying to integrate images from the Google Feed API. While I have successfully extracted the publishedDate and contentSnippet, I am facing difficulty in getting the image src. The code snippets bel ...

Is the integer value included in the linear progression?

I have a unique setup where each time the user clicks a 'Done' button, 20 is added to a base number, x (..-40,-20,0,20,40,60..). This updated value of x is then saved in a database and displayed in real-time using Ajax. However, I am facing a ch ...

Error-free ngClick doesn't trigger AngularJS controller method

I am struggling to trigger the removePlayer(playerId) method upon clicking a button. However, it seems that the method is not being called, as I have placed a console.log() statement at the top and the console remains empty. This situation has left me puz ...

Ways to deactivate an individual menu option?

Here I am with another query related to Webix. I am attempting to deactivate a single menu item, but the onItemClick function for the submenu is still active. Check out my code snippet below: webix.ui({ view:"menu", id:'menu', data:[ ...

*NgFor toggle visibility of specific item

Here is a snippet of HTML code that I'm working with: <!-- Toggle show hide --> <ng-container *ngFor="let plateValue of plateValues; let i=index"> <button (click)="toggle(plateValue)">{{i}}. {{ btnText }}</button> ...

Issues with $.getjson and trouble with storing JSON data

My current issue involves attempting a getJSON call to a page on my domain that contains a simple JSON array. However, the script in my webpage is not returning anything as expected. <script> $('#genButton').click(function() { ...

Having trouble establishing a new local Windows directory structure with Selenium

After following the guidelines provided here and here, I am striving to ensure that the directory where my results reports are stored is consistently available for each user. new File(sampleFolder).mkdir(); The sampleFolder path displayed in the Eclipse ...

What is the use of the typeof operator for arrays of objects in TypeScript?

How can I update the any with the shape of the options's object below? interface selectComponentProps { options: { label: string; value: string; }[]; } const SelectComponent: React.FC<selectComponentProps> = ({ options, }) => ...

What is the best way to place content in a single div without it being divided into several separate boxes

Here is my code snippet: <div class="col-md-9"> <div id="statbox"> {% for obj in product_type %} {% for obj1 in vastu %} <script type="text/javascript"&g ...

Revise: Anticipated output missing at conclusion of arrow function

Here is the code snippet that I am having trouble with: <DetailsBox title={t('catalogPage.componentDetails.specs.used')}> {component?.projects.map(project => { projectList?.map(name => { if (project.id === name.id) { ...

Steps for inserting a title to a marker on the google maps of android through google maps api after the marker has been successfully added

I have been working on an app that utilizes the Google Maps API in Android to display navigation paths between two points. The user can add markers by clicking on the map, with a marker appearing each time they click. Once two markers are added, the applic ...

Having trouble communicating user ratings to the server and updating the rating bar with JSON response in Android Volley?

I implemented a RatingBar feature in my app where users can rate products. The user's rating is supposed to be sent to the server along with their username and the product ID. Once the server receives this data, it calculates the average overall ratin ...

res.send() triggers an error of TypeError: Attempting to convert circular structure to JSON

I've encountered an error message that has been discussed before, but none of the proposed solutions seem to work for my situation. My current project involves building a backend express server to manage API requests for a React application. I have c ...

Encountered an issue while parsing JSON using PHP

Having trouble interpreting the specific term in my JSON result. This is a sample of my JSON result: If "curriculum" has "__class": "chapter", I expect to see: <h2>Title from chapter</h2> and if "__class": "lecture" is present, I'm loo ...

Exploring JSON Array Data and Its Sub-Properties in React

I have a JSON array containing personal data that I want to access from my React app. This JSON file is already included in my React application. { "person": [ { "id": "userId", "sellerImage": "https://i.pravatar.cc/300", ...

Strategies for resolving the problem of null values from getParameter() being passed from AJAX to servlet

After reading numerous answers on Stack overflow, I have tried various solutions to fix this issue without success. My goal is to send a JavaScript variable to a servlet using AJAX within an else statement in JS. However, I keep receiving null in the alert ...

Error: Failed to set the 'src' property of null when attempting to change the image source

When I click on an image, I want to change its source. Here is the HTML code: <div class="character"> <input id="1200" name="vocation_select" type="radio" value="1200" style="display: none" ></input> <label id="label_profesji" for="12 ...

Exploring the wonders of math in Angular with ng-repeat

Exploring the realm of mathematics within an angular expression, let's consider a scenario where a user can either have credit on the site or receive a percentage discount. Below is the code snippet in question: <div ng-repeat="item in NewArrivals ...

Updating variable storage in React components

This is a project built with Next.js and React. Below is the folder structure: components > Navbar.js pages > index.js (/ route)(includes Navbar) > submitCollection.js (/submitCollection)(includes Navbar) The goal is to allow users to inpu ...