更新while循环中的参数D3
update parameter in while loop D3
我希望有人可以帮助,我是全新的javascript,但一直在学习它,以便开始在D3中产生交互输出。
我已经从基础开始,制作了线形图等,现在我想添加一个交互元素。
我有一个线形图,一个滑动条和一个函数,问题是如何将它们连接起来?通过一些在线示例,我了解了如何让滑块更新对象(如文本)的属性,但我希望它在循环中更新参数以执行计算,然后运行并给出线形图输出。
我的代码如下,我已经注释了我想要更新的循环:
<!DOCTYPE html>
<style type="text/css">
path {
stroke-width: 2;
fill: none;
}
line {
stroke: black;
}
text {
font-family: Arial;
font-size: 9pt;
}
</style>
<body>
<p>
<label for="repRate"
style="display: inline-block; width: 240px; text-align: right">
R = <span id="repRate-value">…</span>
</label>
<input type="range" min="0.0" max="1.0" step="0.01" id="repRate">
</p>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
d3.select("#repRate").on("input", function() {
update(+this.value);
});
update(0.1);
function update(repRate) {
// adjust slider text
d3.select("#repRate-value").text(repRate);
d3.select("#repRate").property("value", repRate);
}
//This is the function I want to update when the slider moves, I want the parameter R to update
//with the slider value, then loop through and produce a new graph
function parHost (R){
var i = 0;
var result = [];
do {
//I want to be able to keep it as a loop or similar, so that I can add more complex
//equations into it, but for now I've kept it simple
Nt1 = R*i
result.push(Nt1++) ;
Nt = Nt1
i++;
}
while (i < 50);
return result};
var data = parHost(0.5),
w = 900,
h = 200,
marginY = 50,
marginX = 20,
y = d3.scale.linear().domain([0, d3.max(data)]).range([0 + marginX, h - marginX]),
x = d3.scale.linear().domain([0, data.length]).range([0 + marginY, w - marginY])
var vis = d3.select("body")
.append("svg:svg")
.attr("width", w)
.attr("height", h)
var g = vis.append("svg:g")
.attr("transform", "translate(0, 200)");
var line = d3.svg.line()
.x(function(d,i) { return x(i); })
.y(function(d) { return -1 * y(d); })
g.append("svg:path").attr("d", line(data)).attr('stroke', 'blue');
g.append("svg:line")
.attr("x1", x(0))
.attr("y1", -1 * y(0))
.attr("x2", x(w))
.attr("y2", -1 * y(0))
g.append("svg:line")
.attr("x1", x(0))
.attr("y1", -1 * y(0))
.attr("x2", x(0))
.attr("y2", -1 * y(d3.max(data)))
g.selectAll(".xLabel")
.data(x.ticks(5))
.enter().append("svg:text")
.attr("class", "xLabel")
.text(String)
.attr("x", function(d) { return x(d) })
.attr("y", 0)
.attr("text-anchor", "middle")
g.selectAll(".yLabel")
.data(y.ticks(4))
.enter().append("svg:text")
.attr("class", "yLabel")
.text(String)
.attr("x", 0)
.attr("y", function(d) { return -1 * y(d) })
.attr("text-anchor", "right")
.attr("dy", 4)
g.selectAll(".xTicks")
.data(x.ticks(5))
.enter().append("svg:line")
.attr("class", "xTicks")
.attr("x1", function(d) { return x(d); })
.attr("y1", -1 * y(0))
.attr("x2", function(d) { return x(d); })
.attr("y2", -1 * y(-0.3))
g.selectAll(".yTicks")
.data(y.ticks(4))
.enter().append("svg:line")
.attr("class", "yTicks")
.attr("y1", function(d) { return -1 * y(d); })
.attr("x1", x(-0.3))
.attr("y2", function(d) { return -1 * y(d); })
.attr("x2", x(0))
</script>
</body>
如果有任何帮助,我将不胜感激。
在每个滑块输入事件上,您必须根据您的数据更新图表的部分(例如line
, axisTicks
等)。例如,你可以这样扩展你的更新函数:
function update(repRate) {
// adjust slider text
d3.select("#repRate-value").text(repRate);
d3.select("#repRate").property("value", repRate);
// Generate new Data depending on slider value
var newData = parHost(repRate);
// Update the chart
drawChart(newData);
}
,其中drawChart(newData)可以像这样:
function drawChart(newData) {
// Delete the old elements
g.selectAll("*").remove();
g.append("svg:path").attr("d", line(newData)).attr('stroke', 'blue');
...
}
另一种方法是将数据依赖元素声明为变量,并在更新时更改其属性(我建议):
...
var dataLine = g.append("svg:path");
...
function drawChart(newData) {
path.attr("d", line(newData)).attr('stroke', 'blue');
}
这是一个示例柱塞。
相关文章:
- d3通过改变参数化函数使折线图水平动画化
- d3-append()带有函数参数
- 传递的参数D3提示回调是完整的数据数组
- D3 调度传递参数和调用上下文
- 传递其他参数以过滤回调 D3 和 javascript
- D3:在 IE9 中使用 d3.js 出现“无效参数”错误
- 当方向为顶部时,如何在 d3 js 中定义 ticksize 参数
- 将参数添加到 d3 拖动行为后,元素不再可拖动
- d3.js如何将查询条件的参数传递给php
- 什么是输入参数'd'D3.js函数中的平均值
- 如何设置用于在d3中加载数据或嵌入数据的参数
- 如何将d3.json调用中作为变量存储的返回数据作为参数传递给circular radius
- D3树映射多参数叠加堆栈
- 将javascript参数传递到D3中
- 在“点击”时将参数传递给d3事件回调
- 在D3中对数据对象调用带有多个参数的函数
- 如何获得访问变量/传递参数回调与d3
- 为了使用d3.js,骨干视图中的包装器不能被参数化
- 更新while循环中的参数D3
- AngularJS的模式+ d3 - open() str参数