【问题标题】:d3 tickFormat error - converting v3 to v4d3 tickFormat 错误 - 将 v3 转换为 v4
【发布时间】:2017-11-25 23:40:37
【问题描述】:

我正在尝试将 d3 从版本 3 转换为版本 4,但出现此错误:

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

谁能告诉我如何解决这个问题?

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);

更新: 我想在 v4 d3 的 x 轴上显示年份,在 John 的帮助下,我在一行中做到了:

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);

【问题讨论】:

  • 关于您的编辑;如果您的年份是整数值,您甚至可能不需要指定刻度格式化程序。
  • 哦,是的,你是对的。谢谢。

标签: javascript d3.js scatter-plot


【解决方案1】:

d3.formatPrefix 没有被调用。它需要一个说明符字符串来确定 SI 前缀。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 2019-08-11
    • 2021-09-22
    • 1970-01-01
    • 2017-06-06
    相关资源
    最近更新 更多