gpt4 book ai didi

javascript - 如何在 d3.js 中创建动态条形图?

转载 作者:行者123 更新时间:2023-11-30 05:43:48 25 4
gpt4 key购买 nike

这是完整应用程序的 fiddle :http://jsfiddle.net/RG4Eg/3/

enter image description here

这是javascript:

var HU = (function() {

var data = [
{"time": "13:24:20", "level_1": "5553", "level_2": "4682", "level_3": "1005"},
{"time": "14:24:20", "level_1": "6553", "level_2": "5682", "level_3": "2005"},
{"time": "15:24:20", "level_1": "7553", "level_2": "6682", "level_3": "3005"},
{"time": "16:24:20", "level_1": "8553", "level_2": "7682", "level_3": "3131"},
{"time": "17:24:20", "level_1": "9953", "level_2": "5500", "level_3": "5005"},
{"time": "18:24:20", "level_1": "8565", "level_2": "7682", "level_3": "6005"},
{"time": "19:24:20", "level_1": "7753", "level_2": "4546", "level_3": "4405"}
];

init = function() {

var margin = {
top: 10,
right: 10,
bottom: 30,
left: 50
},
width = 950 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;

var parseDate = d3.time.format("%H:%M:%S").parse;

var x = d3.time.scale()
.domain(d3.extent(data, function(d) { return parseDate(d.time); }))
.range([0, width]);

var y = d3.scale.linear()
.domain([0, d3.max(data, function(d) { return d.level_1; })])
.range([height, 0]);

var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickFormat(d3.time.format(("%H:%M:%S")));

var yAxis = d3.svg.axis()
.scale(y)
.orient("left");

var svg = d3.select("#chart")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");

svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0, " + height + ")")
.call(xAxis);

svg.append("g")
.attr("class", "y axis")
.call(yAxis);

var bars = svg.selectAll("rect")
.data(data)
.enter();
bars.append("rect")
.attr("x", function(d) { return x(parseDate(d.time)); })
.attr("y", function(d) { return y(d.level_1); })
.attr("width", 15)
.attr("height", function(d) { return height - y(d.level_1); })
.style("fill", "steelblue");
bars.append("rect")
.attr("x", function(d) { return x(parseDate(d.time)); })
.attr("y", function(d) { return y(d.level_2); })
.attr("width", 15)
.attr("height", function(d) { return height - y(d.level_2); })
.style("fill", "green");
bars.append("rect")
.attr("x", function(d) { return x(parseDate(d.time)); })
.attr("y", function(d) { return y(d.level_3); })
.attr("width", 15)
.attr("height", function(d) { return height - y(d.level_3); })
.style("fill", "red");

};

return {
init: init
};

})();

HU.init();

随着每一秒的滴答声,我需要最左边的列应该添加一个新的数据点,将以前的数据点推到右边。

我还想每秒对数据本身进行动态更改,例如:

  • 每个 level_1 数据点 = 当前 level_1 val +(之间的随机值+7% 和 -7% 当前 level_1 val)
  • 每个 level_2 随机 10-30% 的折扣
  • 每个 level_3 随机 level_2 值的 10-30% 的折扣

我怎样才能做到这一点?

最佳答案

您可以使用 setInterval(function, period) 来执行每个级别的绘制。你可以这样做:

drawLevel = function(level) {
var random;

svg.selectAll("rect")
.data(data)
.transition()
.duration(1000)
.attr("y", function(d) {
var min = d.level[level] - (d.level[level-1] * 0.03);
var max = d.level[level] - (d.level[level-1] * 0.01);
random = Math.floor(Math.random() * (max - min + 1) + min);

return height - y(random) - .5;
})
.attr("height", function(d) {
return y(random);
});
}

drawLevel(1);
var l = 2;
setInterval(function() {
drawLevel(l);
l = l + 1;
}, 1000);

这是一个使用 setInterval() 的例子。

http://vida.io/documents/kZ2RzrYwZyvWTny2X

关于javascript - 如何在 d3.js 中创建动态条形图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19212170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com