Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8964481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:36:01+00:00 2026-06-15T16:36:01+00:00

My chart is working completely as expected, except that my parseDate function doesn’t want

  • 0

My chart is working completely as expected, except that my parseDate function doesn’t want to give me correct dates on the x-axis. I’m sure this is something simple.

Currently I’m adding d3.v2.min.js, without any additional helper libraries – do I need something else to get d3.time.format() working?

Without parsing the date my data returns an x-axis with:

.960   .965   .970   .975

rather than

1960   1965   1970   1975

The JSON is structured like this:

[{"year":1959,"average":315.97},
{"year":1960,"average":316.91},
{"year":1961,"average":317.64},
...etc
{"year":2011,"average":391.57}]

The code with the // commented sections being the issue:

<script>
var margin = {top: 20, right: 20, bottom: 30, left: 50},
    width = 920 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

// parseDate below not working

var parseDate = d3.time.format("%Y").parse;

var x = d3.time.scale()
    .range([0, width]);

var y = d3.scale.linear()
    .range([height, 0]);

var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom");

var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left");

// data taking format  
d3.json("{{ asset('js/data_co2.json') }}", function(data) {

// parseDate() not working below

Gives error: Uncaught TypeError: Object 1959 has no method ‘substring’

  data.forEach(function(d) {
    d.year = parseDate(d.year);
    d.average = +d.average;
  });

  x.domain(d3.extent(data, function(d) { return d.year; }));
  y.domain([260, 420]);

var area = d3.svg.area()
    .x(function(d) { return x(d.year); })
    .y0(height)
    .y1(function(d) { return y(d.average); });

var svg = d3.select("#co2").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
  .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

svg.append("path")
  .datum(data)
    .attr("class", "area")
    .attr("d", area);

svg.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
  .call(xAxis);

svg.append("g")
    .attr("class", "y axis")
  .call(yAxis)
    .append("text")
    .attr("transform", "rotate(-90)")
    .attr("y", 6)
    .attr("dy", ".71em")
  .style("text-anchor", "end")
  .text("CO2 Levels (ppm)");

});

</script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:36:02+00:00Added an answer on June 15, 2026 at 4:36 pm

    parseDate, the function “prepared” by d3, expects to be passed a string for parsing. Your years are (appropriately) numbers, so you would need to convert them to strings:

    parseDate(String(d.year));

    or

    parseDate(d.year.toString());

    However, keep in mind that the same result – a Date object – could be achieved natively, like this:

    var date = new Date(d.year, 0);// The required 0, is for January

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a view that selects an image (chart) based on user
I'm currently working a problem that requires my web application to generate a chart
I am having trouble using the connect() function. My code was completely working before,
Im working on a polar chart with mschart, and I need to make this
I am working on Highcharts application where chart will be displayed from XML data.
I have been working on getting this seat mapping chart for a while and
I create excel file with chart and it's working on my local machine with
I got the chat example working from Atmosphere, and now I want to get
So I'm trying to create a simple file transfer method. It's completely working for
I'm working on a section of code that has many possible failure points which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.