gpt4 book ai didi

javascript - 重置或清除 Cubism 图表

转载 作者:行者123 更新时间:2023-12-03 04:36:20 27 4
gpt4 key购买 nike

我正在尝试重置或清除 D3.js 立体图,以便它重新开始。这是代码:

$(function(){

// create new cubism.js context to render
var graphContext = cubism.context()
.serverDelay(1000) // allow 1second delay
.step(1000) // 1 second steps
.size(650); // 50px wide

// create a new metric
// this allows you to retrieve values from some source
// this is data-source agnostic, so this does not matter.
var graphMetric = graphContext.metric(function(start, stop, step, callback) {
var values = [];
start = +start;
stop = +stop;
while (start < stop) {
start += step;
values.push(Math.random());
}
callback(null, values);
});


// here we create a new element and then append it to our
// parent container. Then we call d3 to select the newly created
// div and then we can create a chart
var graphElement = document.createElement("div");
$("#insertElement").append(graphElement);
d3.select(graphElement).call(function(div) {
div.append("div")
.attr("class", "axis top")
.call(graphContext.axis().orient("top"));
div.append("div")
.attr("class", "rule")
.call(graphContext.rule());
div.selectAll(".horizon")
.data([graphMetric])
.enter().append("div")
.attr("class", "horizon")
.call(graphContext.horizon()
.height(150)
);
});

});

这是 jsfiddle 上的演示

最佳答案

我只能建议你自己清理 Canvas 。像这样

$(graphElement).find('canvas')[0].getContext('2d').clearRect(0, 0, canvasWidth, canvasHeight);

完整代码 ( jsfiddle )

console.clear();

$(function(){

var canvasWidth = 650, canvasHeight = 150;
// create new cubism.js context to render
var graphContext = cubism.context()
.serverDelay(1000) // allow 1second delay
.step(1000) // 1 second steps
.size(canvasWidth); // 50px wide

// create a new metric
// this allows you to retrieve values from some source
// this is data-source agnostic, so this does not matter.
var graphMetric = graphContext.metric(function(start, stop, step, callback) {
var values = [];
start = +start;
stop = +stop;
while (start < stop) {
start += step;
values.push(Math.random());
}
callback(null, values);
});


// here we create a new element and then append it to our
// parent container. Then we call d3 to select the newly created
// div and then we can create a chart
var graphElement = document.createElement("div");
$("#insertElement").append(graphElement);
d3.select(graphElement).call(function(div) {
div.append("div")
.attr("class", "axis top")
.call(graphContext.axis().orient("top"));
div.append("div")
.attr("class", "rule")
.call(graphContext.rule());
div.selectAll(".horizon")
.data([graphMetric])
.enter().append("div")
.attr("class", "horizon")
.call(graphContext.horizon()
.height(canvasHeight)
);
});

$('#reset').on('click', function () {
$(graphElement).find('canvas')[0].getContext('2d').clearRect(0, 0, canvasWidth, canvasHeight);
});
});

关于javascript - 重置或清除 Cubism 图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43276962/

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