1

I have a data file, named data.js which is like this :

For working in fiddle, I have used this data in the main code only, but otherwise it is form of data.js

var data = [{
      year: "first",
      redDelicious: "10",
      mcintosh: "0",
      oranges: "19"
    }, {
      year: "second",
      redDelicious: "12",
      mcintosh: "0",
      oranges: "15"
    }, {
      year: "third",
      redDelicious: "05",
      mcintosh: "0",
      oranges: "28"
    }, {
      year: "fourth",
      redDelicious: "14",
      mcintosh: "0",
      oranges: "12"
    },

  ];

As we can see, mcintosh is zero in all the cases, this is how, it is initialized. I want to update this mcintosh, every time I run a function which will generate the value to be updated into mcintosh. I am using this to make stacked bar chart where value mcintosh will be stacked on redDelicious on it. Working fiddle for above work is here where you will get the stacked bar chart.

5
  • So you want to edit a file with js? Commented May 12, 2016 at 8:10
  • Yup. That will make code look good otherwise there will be lots of data in the script itself. Commented May 12, 2016 at 8:12
  • I don't think you can do that with client side js. Maybe with localStorage or nodejs (server-side). stackoverflow.com/questions/22087881/…, stackoverflow.com/questions/339679/edit-a-file-using-javascript Commented May 12, 2016 at 8:16
  • @echonax Is it possible to update it, if the data is there in the same js file, like I have used the data in my fiddle. Not a separate js file but in the same js file. Commented May 12, 2016 at 8:19
  • Can you please guide me or help me to do that, may be by making changes into fiddle. I am not much into javascript. Commented May 12, 2016 at 8:22

1 Answer 1

1

OK here's the result : fiddle

I've wrapped your function with update() so we can call it again.

I've added a button so we can increment the mcintosh key in your data. Then on button click I removed the <g> elements under the svg, incremented your second mcintosh value and called update to redraw the updated data.

I've used jquery to bind the click event but you can change it to d3 if you want.

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

2 Comments

That's great, I checked it by updating other elements also like data[2].mcintosh. Its working fine. Thanks a lot :-)
Hope I can update with some variable also which will be passed inside the function through some parameter, as I can't fix it, what number to be updated.

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.