Skip to content
Advertisement

D3 element not showing up in DOM

I’m using this Observable post to create a calendar heatmap with D3.js. My problem is that the calendar is not appearing once it has been created. I have a demo set up on StackBlitz that is set up as suggested in the blog post. I’m not sure if I missed something in the post or if something isn’t set up properly, but any advice or direction would be greatly appreciated.

index.js

JavaScript

Calendar.js

JavaScript

Advertisement

Answer

Adding an Element

ObservableHQ posts operate differently from conventional JavaScript. If an expression returns a DOM element, it automatically shows up in the post. Outside of that environment, in regular JavaScript, you have to add them to your DOM explicitly.

In your case, add document.body.appendChild(chart); to the bottom of index.js.

Fixing the Parsing

Once you do this, you’ll notice that only one column of dates shows up. The calendar seems to expect date objects, not date strings. In that case, you can change the x parsing function in index.js from:

JavaScript

to

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement