Discovering the Data Structures within the d3.js Illustrations by the Talented Mike Bostock

Wondering how to decipher the structure of JSONs in examples by Mike Bostock, like this one (after being transformed using d3):

http://bl.ocks.org/mbostock/3886208

I aim to replicate it with my own fabricated array of JSON objects without relying on loading csv data. However, I'm struggling to comprehend what the format of the JSONs should be.

Data manipulation by Mike Bostock:

d3.csv("data.csv", function(d, i, columns) {
  for (i = 1, t = 0; i < columns.length; ++i) t += d[columns[i]] = +d[columns[i]];
  d.total = t;
  return d;
}, function(error, data) {
  if (error) throw error;

I attempted to store the CSV locally and execute the above code in a node.js command, but unfortunately, it didn't yield results. Is there a simpler method available?

Answer №1

The d3 documentation provides an illustration of how a CSV file can be parsed into an array of objects. Here is a sample CSV file:

Year,Make,Model,Length
1997,Ford,E350,2.34
2000,Mercury,Cougar,2.38

This results in the following JavaScript array:

[
  {"Year": "1997", "Make": "Ford", "Model": "E350", "Length": "2.34"},
  {"Year": "2000", "Make": "Mercury", "Model": "Cougar", "Length": "2.38"}
]

(source: https://github.com/d3/d3-dsv/blob/master/README.md#dsv_parse)

This means that within your data set, the first element would resemble the following structure:

[
  {
    "State": "AL",
    "Under 5 Years": "310504",
    "5 to 13 Years": "552339",
    "14 to 17 Years": "259034",
    "18 to 24 Years": "450818",
    "25 to 44 Years": "1231572",
    "45 to 64 Years": "1215966",
    "65 Years and Over": "641667"
  }
]

An alternative approach could involve converting Bostock's CSV into JSON using a tool like a CSV to JSON converter (such as this one I've used before, and many others are available). This converted JSON data can then be utilized in your local programming code.

If you opt for utilizing D3's CSV loading functions, accessing data from a local file could be complex due to cross-origin request restrictions without a simple server setup.

Regarding running commands in node.js, it may be beneficial to include your JavaScript code and JSON object in a '.js' file and link it to an HTML file, or alternatively embed the JavaScript directly within the HTML if the project is straightforward (similar to Bostock's approach in the linked visualization).

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

Mastering the correct usage of Express middleware functions in routers

Displayed in this instance, are the csrfProtection and parseForm functions being utilized as parameters/callbacks within the GET and POST requests... var cookieParser = require('cookie-parser') var csrf = require('csurf') var bodyParse ...

Please assist with utilizing the combination of Facebook SDK and JSON

After going through numerous discussions on this problem, I realized that none of them truly explains the issue at hand. To resolve the conflicts arising from using facebookSDK with JSON, one effective solution is to alter the names of the JSON classes ca ...

Is there a way for me to make my Note element automatically update whenever I modify its text content?

Feeling a bit stuck with my project, especially this part. I'm currently using React to develop a notes application, and I'm having trouble updating the note when changing the text within the modal popup. The commented sections are where I need h ...

Creating a custom filter in an ng-repeat table using AngularJS

Utilizing a custom filter, I am able to filter values in a table based on a specific column. The data is sourced from an array of a multiple select input. For a complete example, please refer to: http://jsfiddle.net/d1sLj05t/1/ myApp.filter('filterM ...

Connecting radio buttons to data in Vue using render/createElement

How do you connect the value of a selected radio button to a specific variable in the data object within a Vue application when using the render/createElement function? ...

Stock Chart that resembles the functionality of Google's popular line chart feature

Can anyone recommend a Javascript or SVG chart library that has a style similar to a Google Chart? I have been searching without much luck and would appreciate some guidance on how to achieve a similar look. I've looked into the Google Visualization ...

A guide on clearing the selected value in a dropdown menu with Angular.js

Can someone assist me with setting the drop-down value to blank after completing an action in Angular.js? Below is my code explanation: <div style="height:270px; overflow-x:hidden; overflow-y:scroll;" ng-show="viewOrderTable"> <div class="table-r ...

How can I refresh the node server during runtime?

In my Express server, I am currently working on defining an endpoint that triggers a restart of the server automatically during runtime. Here is a snippet showcasing how this could be implemented: var express = require('express') var app = expre ...

Disable Autocomplete Chip functionality when only one can be selected

When multiple is set to true, I prefer the Chip option. Is there a way to enable the Chip functionality even when multiple is set to false? <Autocomplete className={classes.search} options={top100Films} ge ...

Adjusting the view to focus solely on the visible portion of the webpage

Is there a way to achieve zooming in and out on a website similar to how it works on the site ? I want only the visible area to zoom in or out when users interact with their browser. I searched online for a solution but couldn't find one. Any suggesti ...

Is it possible to prevent a webpage from being refreshed?

I need help with an HTML page that puts the worker on pause time which will be subtracted from his/her day job. The issue is that when he/she refreshes the page, the timer starts from the beginning automatically. I want it to continue without resetting. Is ...

Execute a JavaScript function repeatedly, with a specified delay incorporated into it

I am currently working on a JavaScript function that cycles through background images within a div. The function works well, but it stops once all the images have been displayed. How can I make it start over again after going through all the images? $(do ...

Abbreviating Column Labels in Google Visualization

Is there a way to use the google visualization API to display column headers in an abbreviated form in a table, but show the full labels in a pie chart using the same dataset? Check out this snippet of JavaScript: //create the dashboard and table chart ...

Is it possible to activate the identical drop-down or popover with multiple buttons?

Is it possible to activate the same drop-down menu with multiple buttons? For example, I want a button at the top of the page labeled special menu, another in the middle also labeled special menu, and one at the bottom as well labeled special menu. When a ...

Events triggered when the mouse hovers over an element and then moves

After spending hours reading articles and watching YouTube videos, I am completely lost. No matter what code I try, it seems like I can never get it right. It's frustrating how such a simple task can be so confusing. I just can't seem to wrap my ...

The random number generator often omits both the upper and lower limits

I am working with an array that contains letters from A to H. However, when using a random number generator, I have noticed that the letters A and H are rarely selected. How can I adjust my approach to make sure these two bounds are included more often? ...

While v-carousel adjusts to different screen sizes, the images it displays do not adapt to

Whenever I implement v-carousel, everything seems to be working well, but there is an issue on mobile. Despite the carousel itself being responsive, the images inside do not resize properly, resulting in only the center portion of each image being displaye ...

What is preventing me from making zero the initial key in this array?

What the following code produces: for($i = 0; $i <= 7; $i++){ $eachone[] = array ('a' => '1', 'b' => '2', 'c' => '3'); $a[] = array($i => $eachone); unset($eachone); } $js ...

Failure to register Express Route

I am currently using express and facing some challenges with creating routes using express.Router. Below is my index.js file (npm main file): require('dotenv').config() const express = require('express') const loaders = require('. ...

The issue at hand in Ionic 2 / Angular 2 is that the NPM module is being utilized as a value instead of referring to a type

I'm currently working on integrating the npm module "ajv" into my Ionic 2 (Angular 2) project. You can find more information about this module at . After running "npm install ajv --save", I proceeded to make some modifications to my app.modules.js fi ...