'd3'中未找到导出'时间'(导入为'd3')

Lin*_*ang 8 d3.js reactjs

我是新手,使用D3和React.我的反应应用程序是由create-react-app创建的.在我的react组件中,我使用导入d3import * as d3 from 'd3'

我的代码如下:

    this.xScale = d3.time.scale()
        .domain(d3.extent(this.props.data, function (d) {
            return d[_self.props.xData];
         }))
    .rangeRound([0, this.w]);

    this.yScale = d3.scale.linear()
        .domain([0,d3.max(this.props.data,function(d){
            return d[_self.props.yData]+_self.props.yMaxBuffer;
        })])
        .range([this.h, 0]);

    this.area = d3.svg.area()
        .x(function (d) {
            return this.xScale(d[_self.props.xData]);
        })
        .y0(this.h)
        .y1(function (d) {
            return this.yScale(d[_self.props.yData]);
        }).interpolate(this.props.interpolations);
Run Code Online (Sandbox Code Playgroud)

得到了编译错误:

'd3'中未找到导出'时间'(导入为'd3')

npm install d3 --save在我的项目目录下导入d3 .

任何人都有想法是什么问题?

小智 10

你正在使用D3 v4.以下链接是您将旧版本转换为现在使用的解决方案,应该是直截了当的.

https://github.com/d3/d3/blob/master/CHANGES.md


Nic*_*eat 5

使用 d3.timeScale() 而不是 d3.time.scale()

D3.js 语法在版本 4 中发生了变化,因此不再使用 d3.time。

您要么需要导入 d3.js 版本 3

npm install d3@3

或更改您的代码以使用 d3.js 版本 4 语法

d3.timeScale();