D3 refresh dataset when clicked and redraw the histogram

I am new to d3 as well as javascript and I am having problems updating the dataset as well as redrawing the bars. This is the code I've reviewed so far.

http://jsfiddle.net/TwEhT/2/

I have a clickEvent function that is called when I click on any bar. This function requests a value.

function clickEvent() { var op = prompt("Please enter the value", ""); }; 

What I need to do is update the dataset by click index and redraw the rectangles so that they reflect the change in the dataset.

Any help would be greatly appreciated. Thanks.

+6
source share
1 answer

For a very simple example, you can directly update the dataset and put the stroke drawing code in the render () function, which you can call to re-visualize the changes.

 var dataset = [...]; function render() { // bind dataset to rects and draw here } function clickEvent(d, i) { var op = prompt("Please enter the value", d); dataset[i] = parseInt(op, 10); render(); }; 

Here is a sample code in your code: http://jsfiddle.net/findango/TwEhT/4/

+10
source

All Articles