gpt4 book ai didi

html - d3 : Brush with an input field

转载 作者:可可西里 更新时间:2023-11-01 14:43:57 27 4
gpt4 key购买 nike

我正在制作类似于所示演示的 d3 画笔 here .但是,我想让画笔包含一个 <input>元素,当画笔移动时 <input>应该随之而动。

这可能吗,也许通过使用 foreignObject?

最佳答案

试试这段代码。

var bbox = gBrush.node().getBBox();
gBrush.select(".resize.e").append("foreignObject")
.attr("x", -bbox.width)
.attr("y", bbox.height)
.append("xhtml:body").append("xhtml:input").attr("size",4)
.attr("type", "text")
.html("Object in SVG");

var margin = {
top: 200,
right: 40,
bottom: 200,
left: 40
},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;

var x = d3.time.scale()
.domain([new Date(2013, 2, 1), new Date(2013, 2, 15) - 1])
.range([0, width]);

var brush = d3.svg.brush()
.x(x)
.extent([new Date(2013, 2, 2), new Date(2013, 2, 3)])
.on("brush", brushed);

var svg = d3.select("body").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("rect")
.attr("class", "grid-background")
.attr("width", width)
.attr("height", height);

svg.append("g")
.attr("class", "x grid")
.attr("transform", "translate(0," + height + ")")
.call(d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(d3.time.hours, 12)
.tickSize(-height)
.tickFormat(""))
.selectAll(".tick")
.classed("minor", function(d) {
return d.getHours();
});

svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(d3.time.days)
.tickPadding(0))
.selectAll("text")
.attr("x", 6)
.style("text-anchor", null);

var gBrush = svg.append("g")
.attr("class", "brush")
.call(brush);

var bbox = gBrush.node().getBBox();
gBrush.select(".resize.e").append("foreignObject")
.attr("x", -bbox.width)
.attr("y", bbox.height)
.append("xhtml:body").append("xhtml:input").attr("size",4)
.attr("type", "text")
.html("Object in SVG");

gBrush.selectAll("rect")
.attr("height", height);

function brushed() {
var extent0 = brush.extent(),
extent1;

// if dragging, preserve the width of the extent
if (d3.event.mode === "move") {
var d0 = d3.time.day.round(extent0[0]),
d1 = d3.time.day.offset(d0, Math.round((extent0[1] - extent0[0]) / 864e5));
extent1 = [d0, d1];
}

// otherwise, if resizing, round both dates
else {
extent1 = extent0.map(d3.time.day.round);

// if empty when rounded, use floor & ceil instead
if (extent1[0] >= extent1[1]) {
extent1[0] = d3.time.day.floor(extent0[0]);
extent1[1] = d3.time.day.ceil(extent0[1]);
}
}

d3.select(this).call(brush.extent(extent1));
}
.axis text {
font: 11px sans-serif;
}
.axis path {
display: none;
}
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.grid-background {
fill: #ddd;
}
.grid line,
.grid path {
fill: none;
stroke: #fff;
shape-rendering: crispEdges;
}
.grid .minor.tick line {
stroke-opacity: .5;
}
.brush .extent {
stroke: #000;
fill-opacity: .125;
shape-rendering: crispEdges;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

关于html - d3 : Brush with an input field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33208666/

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