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

  • Home
  • SEARCH
  • 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 8893509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:15:56+00:00 2026-06-14T23:15:56+00:00

This is basically javascript problem I am trying to get data by Ajax and

  • 0

This is basically javascript problem I am trying to get data by Ajax and put them into array but when I am logging the array in console I am getting Parse error. I am using d3.js library to create a dynamic graph. But the error is I guess purely due to Javascript concepts applied wrongly.

  var n = 40,
   random = function(){
   d3.json("/server/file/path", function(graphdata){// This is the way we perform ajax call in d3,js
       return graphdata;
     });
   },
   data = d3.range(n).map(random);// Here I am putting into the array n = 40 elements obtained in the ajax call. But I am getting Parse error NaN for number values.



  //Afterwards I am just drawing the graph    
   var margin = {top: 10, right: 10, bottom: 20, left: 40},
  width = 960 - margin.left - margin.right,
  height = 500 - margin.top - margin.bottom;

var x = d3.scale.linear()
.domain([0, n - 1])
.range([0, width]);

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

var line = d3.svg.line()
.x(function(d, i) { return x(i); })
.y(function(d, i) { return y(d); });

var svg = d3.select("body").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("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height);

 svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.svg.axis().scale(x).orient("bottom"));

svg.append("g")
.attr("class", "y axis")
.call(d3.svg.axis().scale(y).orient("left"));

var path = svg.append("g")
.attr("clip-path", "url(#clip)")
.append("path")
.data([data])
.attr("class", "line")
.attr("d", line);

tick();

function tick() {

// push a new data point onto the back
data.push(random());

// redraw the line, and slide it to the left
path
  .attr("d", line)
  .attr("transform", null)
  .transition()
  .duration(500)
  .ease("linear")
  .attr("transform", "translate(" + x(-1) + ")")
  .each("end", tick);

// pop the old data point off the front
data.shift();

}
  • 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-14T23:15:58+00:00Added an answer on June 14, 2026 at 11:15 pm

    Thanks everyone.

    As I was thinking it indeed was a JavaScript error, when tryng to bind the code to generate the graph with actual data coming from server the ajax call was fetching data a bit late. So the JavaScript interpreter was generating Parsing error since it expected the data values to be there but ajax being asynchronous could not yet manage to finish the data retrieval. Also I made some varaibles global variables to solve the problem.

    My solution is as follows

      $(function(){
     // data is a global variable
      data = $.ajax({ type:"GET", 
           url:"location/to/fetch/data",
           }).responseText;
       draw();
    
    }
       function draw(){
    
      var n = 40,
      random = function(){
                  var result = $.ajax({
                   url : /to/fetch/live data/,
                   type:'GET' 
                   }).rsponseText;
                  return result;
                 }
    
    
    
     //Afterwards I am just drawing the graph    
      var margin = {top: 10, right: 10, bottom: 20, left: 40},
       width = 960 - margin.left - margin.right,
      height = 500 - margin.top - margin.bottom;
    
      x = d3.scale.linear()
     .domain([0, n - 1])
     .range([0, width]);
    
     y = d3.scale.linear()
     .domain([-1, 1])
     .range([height, 0]);
    
    line = d3.svg.line()
     .x(function(d, i) { return x(i); })
     .y(function(d, i) { return y(d); });
    
      svg = d3.select("body").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("defs").append("clipPath")
       .attr("id", "clip")
       .append("rect")
       .attr("width", width)
       .attr("height", height);
    
      svg.append("g")
       .attr("class", "x axis")
       .attr("transform", "translate(0," + height + ")")
       .call(d3.svg.axis().scale(x).orient("bottom"));
    
       svg.append("g")
        .attr("class", "y axis")
        .call(d3.svg.axis().scale(y).orient("left"));
    
       path = svg.append("g")
       .attr("clip-path", "url(#clip)")
       .append("path")
       .data([data])
        .attr("class", "line")
      .attr("d", line);
    
     tick();
     }
     function tick() {
    
     // push a new data point onto the back
       data.push(random());
    
    // redraw the line, and slide it to the left
       path
      .attr("d", line)
      .attr("transform", null)
      .transition()
      .duration(500)
      .ease("linear")
      .attr("transform", "translate(" + x(-1) + ")")
      .each("end", tick);
    
      // pop the old data point off the front
       data.shift();
    }
    

    })

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

Sidebar

Related Questions

I've run into this problem before and couldn't figure it out, but it's basically
This is basically a math problem, but very programing related: if I have 1
I have a pretty weird problem with sorting in JavaScript. I've wandered into this
I have (basically) this code - <script type=text/javascript language=javascript> $(document).ready(function() { $(#loadDiv).load(mypage.html, function(){ alert($(#someText).text())
I am experimenting with JavaScript Inheritance. Basically, I am following this tutorial. I see
I'm pretty new to Javascript, so bear with me if this is obvious. Basically
I am trying to to build a Felix bundle in Eclipse. This basically includes
This is basically a duplicate of this question , but the accepted answer was
Details: I'm basically trying to implement the functionality of the example here ( http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/editondblclick/defaultvb.aspx
I have a form where fields get added via JavaScript. This is fairly easy

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.