2

My D3 js Chart is not showing the data please guide me how I have to populate the data in line and area properly,

Data code - this is how I am generating my data

var data = [];
var currentValue = 0;
var random = d3.random.normal(0, 20);
var obj = {};
for (var i = 0; i < 20; i++) {
  var currentDate = new Date();
  currentDate.setDate(currentDate.getDate() + i);

  if (i === 0) {
    currentValue = 0;
  } else if (i === 1) {
    currentValue = 0;
  } else if (i === 5) {
    currentValue = 0;
  } else if (i === 10) {
    currentValue = 0;
  } else if (i === 19) {
    currentValue = 0;
  } else {
    currentValue = currentValue + random();
  }
  obj.startTime = currentDate;
  obj.magnitude = currentValue;
  data.push(obj);
}

console.log(data);

I have a code sandbox here please guide me - https://codesandbox.io/s/frosty-tu-18isr

Thanks. I am new in D3js.

1 Answer 1

2

The issue here has nothing to do with D3, this is just the way object references work in Javascript. When you do this...

obj.startTime = currentDate;
obj.magnitude = currentValue;
data.push(obj);

... you are changing the same object, which is this...

var obj = {};

... and pushing it to the data array. Because of that, all properties are the same (the last iteration). We can easily demonstrate it. Have a look at this array, it has 10 objects:

const obj = {foo:42};
const myArray = d3.range(10).map(()=>obj);
console.log(JSON.stringify(myArray))
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>

Now let's change just the third object:

myArray[2].foo = 17;

However, this will change all of them:

const obj = {foo:42};
const myArray = d3.range(10).map(()=>obj);
myArray[2].foo = 17;
console.log(JSON.stringify(myArray))
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>

And that happens because they are all references to the same object.

Solution: the simplest solution is just pushing the whole object, a new one, at each iteration:

data.push({startTime : currentDate, magnitude: currentValue});

Here is the demo:

var data = [];
var currentValue = 0;
var random = d3.random.normal(0, 20);
for (var i = 0; i < 20; i++) {
  var currentDate = new Date();
  currentDate.setDate(currentDate.getDate() + i);

  if (i === 0) {
    currentValue = 0;
  } else if (i === 1) {
    currentValue = 0;
  } else if (i === 5) {
    currentValue = 0;
  } else if (i === 10) {
    currentValue = 0;
  } else if (i === 19) {
    currentValue = 0;
  } else {
    currentValue = currentValue + random();
  }
  data.push({
    startTime: currentDate,
    magnitude: currentValue
  });
}

console.log(data);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the help, I didn't check the data references.
I am directly passing the data in my chart, why it is not taking, codesandbox.io/s/frosty-tu-18isr anything I am missing
Well, that's a different problem. I suggest you post a new question, with the relevant details.
sure, no problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.