【发布时间】:2013-12-20 13:01:16
【问题描述】:
我正在尝试通过 html 滑块修改直方图布局的 bin,但没有成功。
我尝试为滑块运行的代码是:
<div id = "range">
<input type="range" min="1" max="50" step="1" value="25">
</div>
然后我将直方图的bins 设置为var bin。
用范围滑块的值更新 bin 的 js 代码:
d3.select("#range")
.select("input")
.on("change", function () {
this.value == bin;
//The two histogram variables, line and path generators
histogSans = d3 .layout.histogram()
.bins(bin)
.value(function (d) { return d.Peso; })
(FontSans);
histogSerif = d3 .layout.histogram()
.bins(bin)
.value(function (d) { return d.Peso; })
(FontSans);
line = d3.svg.line()
.x(function (d) { return x(d.x); })
.y(function (d) { return y(d.y); });
SansPath = svg
.transition()
.duration(1500)
.attr("d", line(histogSans));
});
其中histogSans & histogSerif 是直方图值的生成器,line 是线生成器,SansPath & SerifPath 是遵循直方图值的路径生成器。
编辑:感谢 cuckovic,我现在可以使用 bin = this.value; 从滑块中获取正确的值,但直方图的控制台日志现在返回一个错误的数组,仅包含数据集中所有值的数组,因此未正确绘制值!有谁知道为什么会这样?
完整代码在这里:http://dl.dropboxusercontent.com/u/37967455/confronto_pesi.html
【问题讨论】:
标签: javascript d3.js histogram