Javascript d3.js x轴上的刻度时间不工作

Javascript d3.js x轴上的刻度时间不工作,javascript,d3.js,Javascript,D3.js,我不熟悉javascript和d3。我正在使用中的数据创建折线图可视化。基于在线教程,我已经设置了y轴并计算了直线的数据点。因为这个数据集中有很多日期,所以我试图通过解析时间来显示x轴上的年份。但y轴上没有显示任何内容。我认为问题来自var x=d3.scaleTime().rangeRound([0,width])或x.domain(d3.extent(DomesticCount,函数(d){返回d.name;})),但我不知道它们是如何工作的 报告期的日期格式为01/01/2006 12:0

我不熟悉javascript和d3。我正在使用中的数据创建折线图可视化。基于在线教程,我已经设置了y轴并计算了直线的数据点。因为这个数据集中有很多日期,所以我试图通过解析时间来显示x轴上的年份。但y轴上没有显示任何内容。我认为问题来自
var x=d3.scaleTime().rangeRound([0,width])
x.domain(d3.extent(DomesticCount,函数(d){返回d.name;})),但我不知道它们是如何工作的

报告期的日期格式为01/01/2006 12:00:00 AM。(年月日)

以下是csv文件前几行的屏幕截图: 任何帮助都将不胜感激

var保证金={
前20名,
右:20,,
底数:30,
左:80
};
var svg=d3。选择('body')。追加('svg'))
.attr('width',window.innerWidth)
.attr('height',window.innerHeight);
var width=window.innerWidth-margin.left-margin.right;
var height=window.innerHeight-margin.top-margin.bottom;
var parseTime=d3.timeParse(“%d/%m/%Y%H:%m:%S%p”);
var x=d3.scaleTime().rangeRound([0,宽度]);
变量y=d3.scaleLinear().rangeRound([height,0]);
var g=svg.append('g')
.attr('transform','translate('+margin.left+','+margin.top+'));
var line=d3.line()
.x(函数(d){返回x(d.name);})
.y(函数(d){返回y(d.count);});
//读取我们的CSV文件
d3.csv('Los_Angeles_International_Airport_-'u Passenger_Traffic_By_Terminal.csv'),功能(数据){
var DomesticCountMap={};
var InternationalCountMap={};
数据.forEach(功能(基准){
datum.ReportPeriod=解析时间(datum.ReportPeriod);
if(DomesticCountMap[datum.ReportPeriod]==未定义){
国内统计图[datum.ReportPeriod]=0;
}
如果(datum.national_International==“national”){DomesticCountMap[datum.ReportPeriod]+=parseInt(datum.Passenger_Count);}
if(InternationalCountMap[datum.ReportPeriod]==未定义){
InternationalCountMap[datum.ReportPeriod]=0;
}
//聚合到我们的映射中,使用终端名称作为映射键
如果(datum.national_International==“International”){InternationalCountMap[datum.ReportPeriod]+=parseInt(datum.Passenger_Count);}
});
var DomesticCount=[];
Object.keys(DomesticCountMap).forEach(函数(mapKey){
推({
姓名:mapKey,
count:DomesticCountMap[mapKey]
});
});
x、 域(d3.extent(DomesticCount,函数(d){返回d.name;}));
y、 域([0,d3.max(DomesticCount,函数(d){return d.count;})]);
g、 附加('g')
.attr('class','axis--x')
.attr('transform','translate(0',+height+'))
.call(d3.axisBottom(x));
g、 附加('g')
.attr('class','axis--y')
.呼叫(d3.左(y))
.append('文本')
.attr(“填充”、“千”)
.attr('transform'、'rotate(-90'))
.attr('y',6)
.attr('dy','0.71em'))
.attr('text-anchor','end')
.文本(“乘客人数”);
g、 追加('路径')
.基准(国内统计)
.attr('填充','无')
.attr('笔划','蓝色')
.attr('stroke-linejoin','round')
.attr('stroke-linecap','round')
.attr('stroke-width',1.5)
.attr('d',行);

}); 正如您所假设的,问题不在于
xScale
函数。 相反,问题在于这个片段:

  DomesticCount.push({
        name: mapKey,
        count: DomesticCountMap[mapKey]
    });
当您使用d3.scaleTime时,它接受的是日期对象,而不是域的日期字符串

在上面的代码中,
mapKey
是对象
DomesticCountMap
键,该键是字符串。
正如我所提到的,scaleTime接受的是日期对象而不是字符串。因此,您的此行
name:mapKey
将名称值设置为
date
字符串

name:mapKey,
更改为
name:newdate(mapKey)
以日期格式将其转换为日期格式,所有操作都将按预期进行


这是您的工作片段

你能移植几行.csv文件吗?@nitte93user3232918当然可以。我已经添加了该文件的截图。谢谢你的帮助。我按照你的建议进行了编辑,现在正在显示行。