简体   繁体   English

单击D3更新数据集并重新绘制条形图

[英]D3 update dataset on click and redraw the bar chart

I am new to d3 as well as javascript, and I am having trouble updating the dataset, as well as redrawing the bars. 我是d3以及javascript的新手,我无法更新数据集,也无法重新绘制条形图。 This is the code I have been looking at so far. 这是我到目前为止所看到的代码。

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

I have a function clickEvent, that is evoked upon a click on any bar. 我有一个clickEvent函数,只需点击任何一个条就会引发。 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. 我需要做的是在点击索引处更新数据集,并重新绘制rects,以便它们反映对数据集的更改。

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. 对于一个非常简单的示例,您可以直接更新数据集,并将条形图绘制代码放在render()函数中,您可以调用该函数来重新呈现更改。

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's a running example in your code: http://jsfiddle.net/findango/TwEhT/4/ 以下是代码中的运行示例: http//jsfiddle.net/findango/TwEhT/4/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM