点击并重绘条形图的D3更新数据集 [英] D3 update dataset on click and redraw the bar chart
问题描述
我刚刚接触d3以及javascript,我在更新数据集时遇到问题,以及重新绘制条形。这是我一直在看的代码到目前为止。
I am new to d3 as well as javascript, and I am having trouble updating the dataset, as well as redrawing the bars. This is the code I have been looking at so far.
我有一个clickEvent函数。此函数提示输入值。
I have a function clickEvent, that is evoked upon a click on any bar. This function prompts for a value.
function clickEvent()
{
var op = prompt("Please enter the value", "");
};
我需要做的是在点击的索引更新数据集,以便它们反映对数据集的更改。
What I need to do is to update the dataset at the index of the click, and redraw the rects so that they will reflect the change to the dataset.
任何帮助将不胜感激。感谢。
Any help would be appreciated. Thanks.
推荐答案
对于一个非常简单的示例,您可以直接更新数据集, )函数,您可以调用它们来重新呈现更改。
For a very simple example, you could update the dataset directly, and put the bar drawing code in a render() function that you can call to re-render 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();
};
这是代码中的一个运行示例: http://jsfiddle.net/findango/TwEhT/4/
Here's a running example in your code: http://jsfiddle.net/findango/TwEhT/4/
这篇关于点击并重绘条形图的D3更新数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!