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.

hawkeye
  • 349
  • 4
  • 21
  • So you want to edit a file with js? – eko May 12 '16 at 08:10
  • Yup. That will make code look good otherwise there will be lots of data in the script itself. – hawkeye May 12 '16 at 08:12
  • I don't think you can do that with client side js. Maybe with localStorage or nodejs (server-side). http://stackoverflow.com/questions/22087881/how-to-save-and-edit-a-file-using-javascript, http://stackoverflow.com/questions/339679/edit-a-file-using-javascript – eko May 12 '16 at 08: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. – hawkeye May 12 '16 at 08: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. – hawkeye May 12 '16 at 08:22

1 Answers1

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.

eko
  • 39,722
  • 10
  • 72
  • 98
  • That's great, I checked it by updating other elements also like data[2].mcintosh. Its working fine. Thanks a lot :-) – hawkeye May 12 '16 at 08:36
  • 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. – hawkeye May 12 '16 at 08:39