hawkeye hawkeye - 6 months ago 9
Javascript Question

Want to update data.js with some function (No Database)

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.

Answer

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.

Comments