1

I am trying to convert d3 from version 3 to version 4 but I get this error:

Uncaught Error: invalid format: function (d) {
            var prefix = d3.formatPrefix(d);
            return prefix.scale(d) + prefix.symbol;
        }

Can someone tell me how to fix this?

var xScale = d3.scaleLinear()
        .range([0, width])
    .domain([2000, 2018])
        .domain(d3.extent(countries, function(d) { return d.published_year; }))

//Set new x-axis
var xAxis = d3.axisBottom()
    .ticks(10)
    .tickFormat(function (d) {
        return xScale.tickFormat((mobileScreen ? 4 : 8),function(d) {
            var prefix = d3.formatPrefix(d);
            return prefix.scale(d) + prefix.symbol;
        })(d);
    })

    .scale(xScale);
//Append the x-axis
wrapper.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(" + 0 + "," + height + ")")
    .call(xAxis);

Update: I wanted to show year on x-axis in v4 d3 and with the help of John I did it in one line:

var xScale = d3.scaleLinear()
    .range([0, width])
    .domain([2000, 2018])
    .domain(d3.extent(countries, function(d) { return d.published_year; }))

//Set new x-axis
var xAxis = d3.axisBottom()
    .ticks(10)
    .tickFormat(d3.format(""))
    .scale(xScale);
parastoo
  • 372
  • 1
  • 4
  • 20

1 Answers1

2

d3.formatPrefix is not being called as it should. It is expecting a specifier string which determines the SI prefix.

https://github.com/d3/d3-format/blob/master/README.md#locale_formatPrefix

John
  • 1,313
  • 9
  • 21