0

With the following code I create two version of the date value (one is string, progressDateString, another is Date type, progressDate):

  var parseTime = d3.timeParse("%d-%b-%y");
  var formatTime = d3.timeFormat("%d-%b-%y");

  data = d3.csvParse(data, function (d) {
      return {
          progress: d.Progress,
          progressDate: parseTime(d.ProgressDate), 
          //Fri Jul 12 2019 00:00:00 GMT+0200 

          progressDateString: formatTime(parseTime(d.ProgressDate)),
          //12-Jul-19
      };
  });

I want to display the dates in 12-Jul-19 format in the x axis with the code below, however it does not print the date values

  const xScale = d3.scaleLinear()
      .range([0, width])
      .domain([d3.min(data, d => { return d.progressDateString }), d3.max(data, d => { return d.progressDateString })]);

However, instead, if I use progressDate, I get the following chart, where everything is overlapping in the x axis:

enter image description here

Any ideas, why I cannot get it work with progressDateString value? In this post, it seems that it should work. I appreciate any help.

Just in case it may help, I am sharing the whole code below:

    var parseTime = d3.timeParse("%d-%b-%y");
    var formatTime = d3.timeFormat("%d-%b-%y");

    data = d3.csvParse(data, function (d) {
        return {
            progress: d.Progress,
            progressDate: parseTime(d.ProgressDate), 
            progressDateString: formatTime(parseTime(d.ProgressDate)),
        };
    });
    console.log(data[2].progressDate);


    const height = 300,
        width = 400,
        margins = { top: 20, right: 100, bottom: 50, left: 50 };

    const chart = d3.select('.chart')
        .attr('width', width + margins.left + margins.right)
        .attr('height', height + margins.top + margins.bottom)
        .append('g')
        .attr('transform', 'translate(' + margins.left + ',' + margins.top + ')');


    const xScale = d3.scaleLinear()
        .range([0, width])
        .domain([d3.min(data, d => { return d.progressDate }), d3.max(data, d => { return d.progressDate })]);

    const yScale = d3.scaleLinear()
        .range([0, height])
        .domain([d3.max(data, d => { return d.progress }), 0]);

    const dots = chart.selectAll('dot')
        .data(data)
        .enter().append('circle')
        .attr('r', 5)
        .attr('cx', d => { return xScale(d.progressDate); })
        .attr('cy', d => { return yScale(d.progress) })
        .style('fill', 'indianred');


    chart.selectAll('text')
        .data(data)
        .enter().append('text')
        .text(d => { return d.Name; })
        .attr('x', d => { return xScale(d.progressDate); })
        .attr('y', d => { return yScale(d.progress); })
        .attr('transform', 'translate(10,5)');

    chart.append('g')
        .attr('transform', 'translate(0,' + height + ')')
        .call(d3.axisBottom(xScale));

    chart.append('g')
        .call(d3.axisLeft(yScale));

    chart.append('text')
        .style('font-size', '14px')
        .style('text-anchor', 'middle')
        .attr('x', width / 2)
        .attr('y', height + 50)
        .text('Seconds Behind Fastest Time')

    chart.append('text')
        .style('font-size', '14px')
        .style('text-anchor', 'middle')
        .attr('x', height / -2)
        .attr('y', -30)
        .attr('transform', 'rotate(-90)')
        .text('Ranking')
renakre
  • 8,001
  • 5
  • 46
  • 99

1 Answers1

-2

Using scaleTime instead of scaleLinear did the trick. I hope it helps someone.

renakre
  • 8,001
  • 5
  • 46
  • 99