简体   繁体   English

d3.js在x轴上的缩放时间不起作用

[英]d3.js scale time on x axis not working

I am new to javascript and d3. 我是javascript和d3的新手。 I am using data from https://data.lacity.org/A-Prosperous-City/Los-Angeles-International-Airport-Passenger-Traffi/g3qu-7q2u to create a line chart visualization. 我正在使用https://data.lacity.org/A-Prosperous-City/Los-Angeles-International-Airport-Passenger-Traffi/g3qu-7q2u中的数据来创建折线图可视化。 Based on tutorials online, I have already set up y axis and calculated the data points for the line. 根据在线教程,我已经设置了y轴并计算了该线的数据点。 Because there are a lot of dates in this dataset, I tried to just show years on x axis by parsing time. 由于此数据集中有很多日期,因此我尝试通过解析时间来仅显示x轴上的年份。 But nothing is shown on y axis. 但是y轴上没有任何显示。 I think the problem came from var x = d3.scaleTime().rangeRound([0, width]); 我认为问题来自var x = d3.scaleTime().rangeRound([0, width]); or x.domain(d3.extent(DomesticCount, function(d) {return d.name;})); x.domain(d3.extent(DomesticCount, function(d) {return d.name;})); , but I have no idea how those work. ,但我不知道它们是如何工作的。

Date format for ReportPeriod is 01/01/2006 12:00:00 AM. ReportPeriod的日期格式为01/01/2006 12:00:00 AM。 (dd/mm/yyyy) (日/月/年)

Here is a screenshot of first few rows of the csv file: 这是csv文件的前几行的屏幕截图: 在此处输入图片说明 Any help will be appreciated. 任何帮助将不胜感激。

 var margin = { top: 20, right: 20, bottom: 30, left: 80 }; var svg = d3.select('body').append('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, width]); var y = d3.scaleLinear().rangeRound([height, 0]); var g = svg.append('g') .attr('transform', 'translate(' + margin.left + ',' + margin.top + ')'); var line = d3.line() .x(function(d) { return x(d.name); }) .y(function(d) { return y(d.count); }); // read in our CSV file d3.csv('Los_Angeles_International_Airport_-_Passenger_Traffic_By_Terminal.csv', function(data) { var DomesticCountMap = {}; var InternationalCountMap = {}; data.forEach(function(datum) { datum.ReportPeriod = parseTime(datum.ReportPeriod); if (DomesticCountMap[datum.ReportPeriod] === undefined) { DomesticCountMap[datum.ReportPeriod] = 0; } if (datum.Domestic_International === "Domestic"){DomesticCountMap[datum.ReportPeriod] += parseInt(datum.Passenger_Count);} if (InternationalCountMap[datum.ReportPeriod] === undefined) { InternationalCountMap[datum.ReportPeriod] = 0; } // aggregate into our map, using the terminal name as the map key if (datum.Domestic_International === "International"){InternationalCountMap[datum.ReportPeriod] += parseInt(datum.Passenger_Count);} }); var DomesticCount = []; Object.keys(DomesticCountMap).forEach(function(mapKey) { DomesticCount.push({ name: mapKey, count: DomesticCountMap[mapKey] }); }); x.domain(d3.extent(DomesticCount, function(d) {return d.name;})); y.domain([0, d3.max(DomesticCount, function(d) {return d.count;})]); g.append('g') .attr('class', 'axis axis--x') .attr('transform', 'translate(0,' + height + ')') .call(d3.axisBottom(x)); g.append('g') .attr('class', 'axis axis--y') .call(d3.axisLeft(y)) .append('text') .attr("fill", "#000") .attr('transform', 'rotate(-90)') .attr('y', 6) .attr('dy', '0.71em') .attr('text-anchor', 'end') .text('Passenger_Count'); g.append('path') .datum(DomesticCount) .attr('fill', 'none') .attr('stroke', 'blue') .attr('stroke-linejoin', 'round') .attr('stroke-linecap', 'round') .attr('stroke-width', 1.5) .attr('d', line); }); 

As you assumed, the problem is not with your xScale function. 如您所料,问题不在于您的xScale函数。 Instead, the problem lies at this snippet: 相反,问题出在以下代码段:

  DomesticCount.push({
        name: mapKey,
        count: DomesticCountMap[mapKey]
    });

When you are using d3.scaleTime it accepts the date object not a date string for your domain. 使用d3.scaleTime它接受日期对象,而不是您的域的日期字符串。

In the above code, mapKey is a key for object DomesticCountMap , this key is a string. 在上面的代码中, mapKey是对象DomesticCountMapkey ,该键是字符串。 As I mentioned d3.scaleTime takes a date object not a string. 正如我提到的d3.scaleTime接受日期对象而不是字符串。 So, your this line name: mapKey make name value a date string. 因此,此行name: mapKey使name值成为date字符串。

Change name: mapKey, to name: new Date(mapKey) to convert it in date formate, and everything will work as you expected. name: new Date(mapKey)更改name: mapKey,name: new Date(mapKey)更改name: mapKey, name: new Date(mapKey)以将其转换为name: new Date(mapKey) ,一切将按预期工作。

Here's is your working snippet. 这是您的工作片段。 https://jsfiddle.net/g6kf99x7/1/ https://jsfiddle.net/g6kf99x7/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM