Tips for effectively extracting information from a d3.js nested array

I am currently working on visualizing data from a large array and aiming to create a multi-line chart.

The data is in CSV format for easier handling with D3.js.

The structure of my data looks like this:

category, date, frequency <-- column headers
shopping, 23/7/90, 9
dating, 23/3/96, 3

To handle each category individually for the multi-line chart, I transformed the data into another array shown below:

Check out DataNest using console.log()

My current challenge is figuring out how to extract the information needed to draw the lines on my chart.

Here's the code snippet I'm using:

var dataNest = d3.nest()
.key(function(d) {return d.category;})
.entries(data_out);

dataNest.forEach(function(d,i) {

var line = d3.line() 
.x(d3.values(dataNest).map(function(d) { return d.date; }).filter(function(key) { return key !== "date"; }))
.y(d3.values(dataNest).map(function(d) { return d.frequency; }).filter(function(key) { return key !== "frequency"; }))
;

chartGroup.append('path').attr('d',line(d));

});

Answer №1

It is important to ensure that your data in a CSV or TSV file is formatted correctly, such as:

category,date,frequency
shopping,23/7/90,9
shopping,28/7/90,9
shopping,27/7/90,9
shopping,23/8/90,9
shopping,23/9/90,9

Instead of:

category,date,frequency shopping,23/7/90,9
category,date,frequency shopping,28/7/90,9
category,date,frequency shopping,27/7/90,9
category,date,frequency shopping,23/8/90,9
category,date,frequency shopping,23/9/90,9

Once the data is properly formatted, you can easily reference it after importing the CSV using d.category, d.date, etc.

For a great example of a multi-series line chart using a TSV, check out this link: https://bl.ocks.org/mbostock/3884955

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

Exploring Dynamic 2D Arrays in C++ and Preventing Memory Leaks

Having written the following code, I find that it runs smoothly. However, when running it under Valgrind, I encounter 2 issues. Unfortunately, I struggle to decipher Valgrind's messages and would greatly appreciate someone explaining them to me and pi ...

Angular 6 canvas resizing causing inaccurate data to be retrieved by click listener

The canvas on my webpage contains clickable elements that were added using a for loop. I implemented a resizing event that redraws the canvas after the user window has been resized. Everything works perfectly fine when the window is loaded for the first ti ...

Node.js library for Interactive Brokers TWS API - Custom order builder

Currently utilizing the node-ib npm package, I am aiming to execute a Combination Order. Here are the steps I followed: Obtaining the contract IDs for both leg definitions. Once the program successfully obtains the conId value for each leg, it is th ...

AngularJS component not functioning properly due to issues with 'bindings'

.component( 'testComponent', {bindings: {name:'<'}, template: `{{$ctrl.name}}<br/> {{$ctrl.title}}<br/> {{test.name}}<br/> {{test.title}}<br/> {{name}}& ...

Protractor tests encounter issues when working with Select2 within a modal in Angular

I am facing a challenge while running my angular e2e tests using protractor. In some cases, I have a select element inside a modal. The issue arises when the test fails to locate the select element with the following error message: NoSuchElementError: No ...

Is it possible to manipulate angularjs data without using a controller?

In my main webpage, there are just two sections: a navigation bar and the changing content. index.html: <div ng-controller='MainCtrl'> <li ng-repeat="nav in navs"> <a href="#{{nav.url}}">{{nav.name}}</a> ...

Guide to dynamically generating Angular watchers within a loop

I'm looking to dynamically create angular watches on one or more model attributes. I attempted the following approach: var fields = ['foo', 'bar']; for (var i=0, n=fields.length; i<n; i++) { $scope.$watch('vm.model.&ap ...

How to incorporate material-ui tabs in nextjs framework?

I'm currently working on a project using material-ui, nextjs, and typescript. My main focus right now is getting the navbar to function properly within nextjs: import * as React from 'react'; import AppBar from '@material-ui/core/A ...

Launch a new pop-up window with a designated keyboard language

My JavaScript code opens a new window using this syntax: window.open(URL); However, I've noticed that even when the browser's keyboard language is set to 'fa' during opening, the new window still defaults to 'en'. Is there ...

Delete class at waypoints

I am currently using waypoints.js to manage a single-page website that includes a highlighted navigation feature. When the viewport reaches the element with the class "content", the corresponding navigation point is given the class "active". The script i ...

Map on leaflet with popup next to key

I am facing a scenario where I have a map with a unique legend that is styled either as an SVG or a PNG. The legend is always positioned in the bottom left corner but can be quite large, as users have the option to toggle it on and off. Additionally, the ...

variable value remains constant after being updated

Here is the code snippet I am working with: function change(initial) { let a = initial; console.log(a); return [ a, (v) => { a = v; } ]; } const [val, setter] = change("initial"); console.log(val); setter("s&qu ...

Eliminate the presence of Null within an ArrayList's For loop

I am currently in the process of converting an ArrayList into a String for the purpose of sending it to a database. The intention is to retrieve it on the receiving end and later convert it back to an ArrayList. My current approach involves converting the ...

Maximizing HTML5 Game Performance through requestAnimationFrame Refresh Rate

I am currently working on a HTML5 Canvas and JavaScript game. Initially, the frames per second (fps) are decent, but as the game progresses, the fps starts decreasing. It usually starts at around 45 fps and drops to only 5 fps. Here is my current game loo ...

Just starting to explore Node.js and wondering how to create a dynamic dropdown menu in a web application that pulls

Here is my node code: app.get('/block_name', function (req,res){ var sql='SELECT `block_name`,`block_id` FROM `tbl_block` '; connection.query(sql,function(err, result) { if (err) throw err; res.json(result); }); }); app.ge ...

Struggling to retrieve the Object from a JSON file located at a specific URL

Apologies if this comes across as naive, but my venture into javascript and json is just starting. I'm eager to access the JSON object from the twitter API after executing var myjson; $.getJSON('url of the Object', function(data) { ...

When a textbox is emptied, AngularJS has the ability to enable or disable all other textboxes

I am new to web development and I am eager to learn more about how to control my inputs and select options on my website. I have one select input and four text input fields. My goal is to disable all the other input fields and the select option if a choic ...

Setting values in 2D arrays in C++ without automation

Essentially, an array can be created using the following syntax: int arrayValues = {1,2,3,4,5}; How can a similar method be used to define a double array without having to manually assign each value like this... int magicArray[rowSize][colSize]; magicA ...

Remove the chosen products from the shopping cart

I've created something similar to a shopping cart where you can drag items to a selected section and they will appear there. I am now attempting to add the option to delete a specific selected item, but I haven't been able to find a suitable way ...

What are the specific instances in which jQuery Ajax error codes are triggered?

According to the jQuery documentation, there are 4 potential error codes: parserror timeout abort error I have observed that parserror occurs when the response content-type is application/json but jQuery is unable to parse it. Timeout occurs when the s ...