# D3 Heat Map - Data Starts in the Wrong Month?

**URL:** <https://forum.freecodecamp.org/t/d3-heat-map-data-starts-in-the-wrong-month/225190>\
**Category:** General\
**Created:** [September 9, 2018, 1:36pm UTC](https://forum.freecodecamp.org/t/d3-heat-map-data-starts-in-the-wrong-month/225190 "2018-09-09T13:36:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![javineya](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/javineya/32/83511_2.png) [@javineya](https://forum.freecodecamp.org/u/javineya)\
**Post date:** [September 9, 2018, 1:36pm UTC](https://forum.freecodecamp.org/t/d3-heat-map-data-starts-in-the-wrong-month/225190/1 "2018-09-09T13:36:56Z")

</div>

Don’t worry, @SpaniardDev, I got the y-axis this time! Actually, I passed all the tests, but I’m still confused:

Why do my temperature values begin in September, 1753 when the data we’re given starts in January, 1753? What did I mess up?

> **[FCC: D3 Heat Map](https://codepen.io/JesterVae/pen/yxVKqm?editors=0010)**
>
> ...

---

<div class="post-metadata">

**Author:** ![SpaniardDev](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/spaniarddev/32/89850_2.png) [@SpaniardDev](https://forum.freecodecamp.org/u/SpaniardDev)\
**Post date:** [September 10, 2018, 9:47am UTC](https://forum.freecodecamp.org/t/d3-heat-map-data-starts-in-the-wrong-month/225190/2 "2018-09-10T09:47:57Z")

</div>

LOL. Good to know the y-axis is doing fine! 😃

I’ll have a look and see if I can find the bug.

---

<div class="post-metadata">

**Author:** ![SpaniardDev](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/spaniarddev/32/89850_2.png) [@SpaniardDev](https://forum.freecodecamp.org/u/SpaniardDev)\
**Post date:** [September 10, 2018, 11:42am UTC](https://forum.freecodecamp.org/t/d3-heat-map-data-starts-in-the-wrong-month/225190/3 "2018-09-10T11:42:08Z")

</div>

I just reviewed your code. As usual when dealing with bugs, just a tiny little thing was giving you unexpected results. As much as 12 characters… LOL. That’s what I love about coding!

## Why

So, why is it showing bad results? Because you’re first building the legend inside your SVG element, like this:

```auto
// First you are creating a group for the legend.
const legend = svg.append("g")
  attr( ... );
// Then, you are creating 8 'rect' elements (the length of 'legendInfo' array) under that group.
legend.selectAll("rect")
  .data(legendInfo)
  .enter()
  .append("rect")

```

And then, you’re building the actual _heatmap_ by doing this:

```auto
svg.selectAll('rect')
  .data(data)
  .enter()
  .append('rect')

```

So, inside the SVG element, you’re first adding a `g` with some `rect` elements. Then, you’re selecting all `rect` elements **inside the `svg` element** and creating as many as required. But, since you have already created 8 elements under the `svg`, the _heatmap_ will create n - 8 new `rect` elements ( from January to August).

## How to fix it

I’m sure you already know how to fix it. On line `164` change `svg.selectAll('rect')` to `svg.append('g').selectAll('rect')`.

That’s it! Everything working great.

Happy coding!

---

<div class="post-metadata">

**Author:** ![javineya](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/javineya/32/83511_2.png) [@javineya](https://forum.freecodecamp.org/u/javineya)\
**Post date:** [September 10, 2018, 2:05pm UTC](https://forum.freecodecamp.org/t/d3-heat-map-data-starts-in-the-wrong-month/225190/4 "2018-09-10T14:05:04Z")

</div>

You’re my D3 Hero. That’s some impressive debugging; I don’t think I would ever have figured that out. I was focused on the fact that the data both started and ended in September…which was just a coincidence.

And since I’m on the subject of…you, I have to say that I love your coding style. It’s really easy to read the way you use whitespace and I think I’m gonna steal that style. 🙂

---

<div class="post-metadata">

**Author:** ![SpaniardDev](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/spaniarddev/32/89850_2.png) [@SpaniardDev](https://forum.freecodecamp.org/u/SpaniardDev)\
**Post date:** [September 10, 2018, 2:09pm UTC](https://forum.freecodecamp.org/t/d3-heat-map-data-starts-in-the-wrong-month/225190/5 "2018-09-10T14:09:10Z")

</div>

Thanks! I think I have an obsession when it comes to organize and make things pretty lol

You’re not stealing, I’m giving it away to you! 😃

Good luck on the next D3 challenge!

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 8:16pm UTC](https://forum.freecodecamp.org/t/d3-heat-map-data-starts-in-the-wrong-month/225190/6 "2021-01-17T20:16:34Z")

</div>

Closed on January 17, 2021, 8:16pm UTC


