【问题标题】:d3 tickFormat, remove comma as thousands separatord3 tickFormat,删除逗号作为千位分隔符
【发布时间】:2018-05-18 14:12:47
【问题描述】:

在 d3 图表中,我们默认将逗号作为千位分隔符,我想删除它们并将 x 轴刻度显示为例如“2 000 000”而不是“2,000,000”

这是我尝试过的:

    private scaleX: ScaleLinear<number, number>;
    private xAxisComponent: any;

    // Define an x Axis
    const axisNumberFormat: FormatLocaleDefinition = {
        'decimal': '.',
        'thousands': ' ',
        'grouping': [3],
        'currency': ['', ''],
    };
    const axisFormatLocale: FormatLocaleObject = this.d3.formatDefaultLocale(axisNumberFormat);
    this.xAxisComponent = this.d3.axisBottom(this.scaleX).tickFormat(axisFormatLocale);

但是,最后一行的赋值不起作用,它说“'FormatLocaleObject' 类型的参数不能分配给'null' 类型的参数。” 有什么提示吗?还是我这样做是错误的方式,有没有另一种方法可以将格式字符串放在一起发送到tickFormat() 方法?

【问题讨论】:

  • 如果是 Typescript/Angular/React/Whatever,请添加相应的标签。

标签: d3.js


【解决方案1】:

如果将 .format('') 添加到axisFormatLocale,最后一行将起作用:

this.xAxisComponent = this.d3.axisBottom(this.scaleX).tickFormat(axisFormatLocale.format(''));

【讨论】:

    猜你喜欢
    • 2013-05-09
    • 2012-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多