Application of date formatting on x-axis text

whytheq

I'm trying to apply d3.time.format("%b-%Y") to the dates used on the x-axis.

Here is the code which adds the axis and labels

var xLabels = svg
  .append("g")
  .attr("transform", "translate(" + margin.left + "," + (margin.top + height + 12) + ")");

var formatDateOutputX = d3.time.format("%b-%Y");

xLabels.selectAll("text.xAxis")
  .data(BarData)
  .enter()
  .append("text")
  .text(function(d) {
    return d.dt;                      //<< returns dates
    //return formatDateOutputX(d.dt); //<< NOTHING RETURNED
  })
  .attr({
    'text-anchor': "middle",
    transform: function(d, i) {
      var x = (i * (width / BarData.length)) + ((width / BarData.length - barPadding) / 2);
      var y = 20;
      return 'translate(' + x + ',' + y + ')rotate(-90)';
    },
    dy: "0.35em", //dx: "-1.05em",
    'class': "xAxis"
  });

The above relates to the lines of code 285-309 of this visualisation: https://plnkr.co/edit/3d5UhM?p=preview

Hoping someone can help as this will be a fairly common manipulation that I will want to apply. What am I doing wrong?

Aurelio

You are not passing a valid date to formatDateOutputX so it doesn't know how to process it. You have to parse the value appropriately and you can do it in many ways. Since d3 provides a parse method to time.format that is probably the best way to follow.

In your case something like this will work:

    var formatDateOutputX = d3.time.format("%b-%Y");
    var readDate = d3.time.format("%d/%m/%Y").parse;

    xLabels.selectAll("text.xAxis")
      .data(BarData)
      .enter()
      .append("text")
      .text(function(d) {
        return formatDateOutputX(readDate(d.dt));
      })

Check the Plunkr.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to put date on barplot() x-axis?

From Dev

Highcharts Date/Time and X-Axis

From Dev

Plot of minute tick data with correct x-axis formatting?

From Dev

R plotting, date on x axis

From Dev

AmChart x axis data date formatting to display per day values with hours minutes and seconds

From Dev

Google chart timeline with date in x axis

From Dev

Gnuplot Date and Time on the x axis

From Dev

Formatting X axis label in R for a specific condition

From Dev

Change Number Formatting on X-Axis Matplotlib

From Dev

How to move Date under X axis?

From Dev

Formatting a scale_x_continuous axis with quarterly data

From Dev

Date-Time on x-axis of ZingChart

From Dev

Seaborn/Matplotlib Date Axis barplot minor-major tick formatting

From Dev

JQuery Flot X axis label formatting

From Dev

Disable date-formatting for domain axis in JFreeChart when x-data is Long

From Dev

X and Y axis plot date

From Dev

jqPlot not showing x-axis label or formatting axis

From Dev

SSRS bar char x-axis formatting

From Dev

X axis label formatting in Shinobi charts

From Dev

Formatting X axis with dates format Matplotlib

From Dev

Spline graph date on x axis

From Dev

x axis text overlapping in matlab

From Dev

Date as X-axis in Excel

From Dev

JQuery Flot X axis label formatting

From Dev

Disable date-formatting for domain axis in JFreeChart when x-data is Long

From Dev

Formatting X axis labels Pandas time series plot

From Dev

Error parsing date for X axis

From Dev

Formatting MySQL long text to Date

From Dev

Formatting Bokeh x axis from decimal to percentage with PrintfTickFormatter