# d3, x axis label format problem

**URL:** <https://talk.observablehq.com/t/d3-x-axis-label-format-problem/8417>\
**Category:** Help\
**Created:** [October 24, 2023, 12:04pm UTC](https://talk.observablehq.com/t/d3-x-axis-label-format-problem/8417 "2023-10-24T12:04:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wintran](https://avatars.discourse-cdn.com/v4/letter/w/9fc29f/32.png) [@wintran](https://talk.observablehq.com/u/wintran)\
**Post date:** [October 24, 2023, 12:04pm UTC](https://talk.observablehq.com/t/d3-x-axis-label-format-problem/8417/1 "2023-10-24T12:04:11Z")

</div>

I’m trying to label my x axis but having some trouble.

my data is as below.

```auto
const dataset = [
        { date: 2018, value: 200 },
        { date: 2019, value: 250 },
        { date: 2020, value: 180 },
        { date: 2021, value: 300 },
        { date: 2022, value: 280 },
        { date: 2023, value: 220 },
      ];

```

my axis setup is as below

```auto
// set up the x, scale
const x = d3.scaleLinear().range([0, width]);
const y = d3.scaleLinear().range([height, 0]);

//define x,y domain
x.domain(d3.extent(dataset, (d) => d.date));
y.domain([0, d3.max(dataset, (d) => d.value)]);

```

and appended the axis as below

```auto
// add x axis
      svg.append('g')
        .attr('transform', `translate(0,${height - margin.bottom})`)
        .attr('class', 'x-label-text')
        .call(d3.axisBottom(x));

```

and I am getting this.

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/observablehq/original/2X/1/162e28834c7047db6d22202caa3227106fb70620.png)

I can’t seem to figure out why it’s giving me decimals, a mid point(0.5) and a comma at the thousand mark. I tried changing the data into strings but didn’t work. Tried various scales but didn’t work.

any help would great.

---

<div class="post-metadata">

**Author:** ![eagereyes](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/eagereyes/32/6255_2.png) [@eagereyes](https://talk.observablehq.com/u/eagereyes)\
**Post date:** [October 24, 2023, 4:42pm UTC](https://talk.observablehq.com/t/d3-x-axis-label-format-problem/8417/2 "2023-10-24T16:42:07Z")

</div>

D3 doesn’t know that these numbers are years and creates too many tick marks. You can add `.tickFormat(d3.format('0f'))` to your x axis to get full numbers, and `.ticks(6)` in case you still get too many tick marks.

---

<div class="post-metadata">

**Author:** ![wintran](https://avatars.discourse-cdn.com/v4/letter/w/9fc29f/32.png) [@wintran](https://talk.observablehq.com/u/wintran)\
**Post date:** [October 25, 2023, 6:35am UTC](https://talk.observablehq.com/t/d3-x-axis-label-format-problem/8417/3 "2023-10-25T06:35:53Z")

</div>

thank you for that. .ticks(6) worked like a charm.  
changing the format to 0f didn’t work. it added a bunch of zeros. But .format('d) worked. not sure why though
