Variation in Map Behavior in d3 Due to Datum-Data Discrepancy

I'm relatively new to utilizing d3js and I am endeavoring to grasp the distinction between employing data and datum for associating data with elements. Despite having spent quite some time reading various materials online, I still lack an instinctive comprehension. Specifically, I am faced with a scenario where I am constructing a map using topojson in d3 v7.

Initially, here is the code snippet I have to generate the map within a div (making the assumption that height, width, projection, etc. are correctly set up):

var svg = d3.select("div#map").append("svg")
    .attr("width", width)
    .attr("height", height)
    .attr("transform", "translate(" + 15 + "," + 0 + ")"); 

var path = d3.geoPath()
          .projection(projection);

var mapGroup = svg.append("g");

d3.json("json/world-110m.json").then(function(world){
  console.log(topojson.feature(world, world.objects.land))

  mapGroup.append("path") 
     .datum(topojson.feature(world, world.objects.land))
     .attr("class", "land") 
     .attr("d", path); 

});

The console log output for the topojson feature resembles this: https://i.sstatic.net/jKdwM.png

In addition, the map displays perfectly fine (with styling specified in a CSS file): https://i.sstatic.net/UPrcC.png

However, when I switch from datum to data, the map vanishes. I am aiming to enhance my understanding of how this operates, but I find myself struggling slightly even after scouring the resources available online. Can anyone elaborate on the disparity between data and datum as utilized in this instance and elucidate why one yields results while the other does not?

Appreciate your assistance!

Answer №1

When it comes to data() and datum(), there are various distinctions, but for clarity on the particular topic at hand, the key variance is that data() only accepts 3 things:

  • An array;
  • A function;
  • Nothing (in which case, it functions as a getter);

In the context of using

topojson.feature(world, world.objects.land)
since it's an object, to utilize data() here (even though it may not be considered idiomatic D3), you simply need to wrap it within an array:

.data([topojson.feature(world, world.objects.land)])

The code snippet below demonstrates how to incorporate data():

var svg = d3.select("div#map").append("svg")
  .attr("width", 500)
  .attr("height", 300)
  .attr("transform", "translate(" + 15 + "," + 0 + ")");

var path = d3.geoPath();

var mapGroup = svg.append("g");

d3.json("https://raw.githubusercontent.com/d3/d3.github.com/master/world-110m.v1.json").then(function(world) {

  const projection = d3.geoEqualEarth()
    .fitExtent([
      [0, 0],
      [500, 300]
    ], topojson.feature(world, world.objects.land));

  path.projection(projection);

  mapGroup.append("path")
    .data([topojson.feature(world, world.objects.land)])
    .attr("class", "land")
    .attr("d", path);

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<script src="https://unpkg.com/topojson@3"></script>
<div id="map"></div>

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

Is there a way to allow for clicking on a row to redirect to a new tab and display the data of the selected row?

Currently, I am working with mui-datatable and trying to figure out how to enable the user to be directed to another page simply by clicking on a row. Additionally, I need the data of that specific row to be passed along to the new page as well. The error ...

What is the best way to update the tooltip array value on a Fly component?

TextboxButton is a collection of text input elements. TextboxButton = ["<input type='text' id='first_txt' value='Region 1' /> <input type='submit' id='first_edit' value='Edit'>", "< ...

Struggling to create a complete Absolute URL

www.baxter.com source page reveals that many of the href links start with the word baxter, for example: href="/baxter/corporate.page?">About Baxter< Attempting to construct an absolute URL by combining the base url, www.baxter.com, with the relativ ...

Vue.js failing to update in real-time

Within my HTML code, I have the following: <div class="pl_wrapper"> <div class="options_pl"> <input type="button" @click="optionButtonClicked" class="option_button_pl" value="List"> <input type="button" @click="opt ...

Is there a way to change the color of a row in react jsx based on a condition from another row?

I am looking to highlight rows in red color based on a specific condition. If the ratio value in a column is greater than or equal to 0.96, I want to apply this styling. However, I'm unsure about where exactly to insert the necessary lines of code to ...

Vue: Order Conflict. A new module has been incorporated into the system

Whenever I try to run my program, something unexpected happens; How can I resolve this issue? I don't want to overlook it; I searched online and found suggestions to change the component order, but after checking my code, it didn't work; Any i ...

An efficient way to extract individual elements from a JSON object such as {"response":[{"result":"rohit1"}]}{"response":[{"result":"rohit"}]}{"response":[{"result":"rohit"}]}, is

As someone who is just starting out in the world of Android development, I am facing a challenge. I am trying to access each element from a JSON array response, but so far I have only been able to retrieve the first value, "rohit1". Despite my background i ...

Can one extract request data from an rxjs-timeout-error?

Currently, I am working on enhancing our error handling system, particularly in providing better descriptions of errors in both general and testing environments. My focus is on an Ionic app, but I am facing challenges with the rxjs timeout method. One asp ...

Sending data in chunks using Vue.js

I'm interested in sending the data in chunks. Currently, what I send to the server looks like this: for loop - 1, 2, 3. However, the server receives it asynchronously as 3, 1, 2 and I need it to be received synchronously in the order of my for loop: 1 ...

The JSON file failed to load

Having some trouble running the JSON file with jQuery AJAX, always getting an error message. I am trying to run the code locally without any ASP.NET or PHP, aiming to run JSON without a server. I have set the URL through IIS on my local machine. JSON: / ...

Is it possible to update the parent state from a child component using the useEffect hook in React?

In a child component, there are buttons that, when clicked, toggle corresponding state values between true and false. This child component also contains a useEffect hook with dependencies on all these state values. When a button is clicked, the hook calls ...

React application experiencing asynchronous function that is not returning

In my React app, I have an asynchronous function in a file (someFile.js) and I need the returned string from that function (someFunction) to be displayed on the webpage once it's done. However, I'm facing an issue where the webpage appears blank ...

TS2688 Error: Type definition file for 'tooltip.js' not found

Why am I getting an 'undefined' error when trying to import the Tooltip class from the npm tooltip.js package in my TypeScript file? ...

Discovering the variance among two arrays of dates in JavaScript

I am attempting to identify the variance between two sets of dates stored as arrays, and then segregate all distinct dates into a separate array for future use. Additionally, I have a specific functionality in mind. I have a variable called diffDates. It n ...

Developing a dynamic web application using the Django framework along with the Vue.js library and Highcharts for

I am currently working on a data visualization web app using Django, Highcharts, and JQuery. I have recently transitioned from JQuery to Vue JS and I am struggling with fetching JSON data from a specific URL. Below is the code snippet: Template <!doc ...

Will the bootstrap carousel continue running in the background even if I hide it?

Currently, I have incorporated the twitter bootstrap carousel into my website with the following code: <div id="slider" class="carousel slide"> <!-- Wrapper for slides --> <div class="caro ...

Executing a function without using the eval() function

I am currently working on a JavaScript code that relies heavily on the eval function. eval(myString) The value of myString is equal to myFunc(arg), and I would like to find a way to call myFunc directly instead of using eval. Unfortunately, I have no co ...

After refreshing, Angular route using html5Mode leads to a 'Page Not Found' error page

I have created several Angular routes as illustrated in the code snippet below. app.config(function($routeProvider, $locationProvider, $provide) { $routeProvider .when('/', { templateUrl: 'home.html', controll ...

The div element is unable to activate the modal due to tabindex issues

Seeking Assistance with a Specific Scenario I have a specific layout where a div is enclosing an image. The desired functionality is that when the div is clicked with a mouse, a small tooltip modal should appear displaying address information. This can be ...

In Firefox, the CSS height and width values are displayed as X pixels, whereas in Internet Explorer, they are automatically set to 'auto'

When it comes to measuring div box dimensions, jQuery can be a handy tool. Here is an example code snippet: $(document).ready(function(){ var h = $("#testbox").css("height"); }); Interestingly, the same code can give different results in different brow ...