gpt4 book ai didi

javascript - 与 SVG 矩形内联的 Div

转载 作者:太空宇宙 更新时间:2023-11-03 17:37:53 26 4
gpt4 key购买 nike

我要这个

enter image description here

到目前为止我所取得的成就是 - http://jsfiddle.net/nhe613kt/116/

所有我需要的是在边上添加编号和文本“销售”和“利润”,我尝试添加几个 div 但随后它们在上面带了空格,在这里添加这些数字的最佳方式是什么,我想最好在 JS 中这样做,因为盒子的数量可能会很谨慎。

代码

$("#myConta").height(window.innerHeight - (window.innerHeight / 40));

var width = window.innerWidth - (window.innerWidth / 10),
height = window.innerHeight - (window.innerHeight / 10),
boxW = width / 4,
boxH = height / 4,
boxSize = boxW + boxH,

xPos1 = 0,
xPos2 = boxW,
xPos3 = boxW * 2,
xPos4 = boxW * 3,
yPos1 = 0,
yPos2 = boxH,
yPos3 = boxH * 2,
yPos4 = boxH * 3;

var CreateRect = function (x, y, boxColor) {
svgContainer.append("rect")
.attr("x", x)
.attr("y", y)
.attr("width", boxW)
.attr("height", boxH)
.attr("fill", boxColor)
.attr("class", "hover_group");
};
var CreateRectWithLength = function (x, y, w, h, boxColor) {
svgContainer.append("rect")
.attr("x", x)
.attr("y", y)
.attr("width", w)
.attr("height", h)
.attr("fill", boxColor);
};

var CreateText = function(x, y, text, textColor) {
svgContainer.append("text")
.attr("x", x)
.attr("y", y)
.attr("stroke", textColor)
.text(text);
};

var CreateTextInMain = function(x, y, text, textColor) {
$("#myConta").append("div")
.attr("x", x)
.attr("y", y)
.attr("stroke", textColor)
.text(text);
};

var svgContainer = d3.select("#myConta")
.append("svg")
.attr("id", "myContasvg")
.attr("width", width)
.attr("height", height)
.attr("fill", "#2E2E2E")
.attr("float", "right")
.append("g");

CreateRect(xPos1, yPos1, "#C0FC3E");
CreateRect(xPos1, yPos2, "#60FC60");
CreateRect(xPos1, yPos3, "#64FE2E");
CreateRect(xPos1, yPos4, "#00FF00");

CreateRect(xPos2, yPos1, "#F6FF33");
CreateRect(xPos2, yPos2, "#AFFC3B");
CreateRect(xPos2, yPos3, "#00FF00");
CreateRect(xPos2, yPos4, "#64FE2E");

CreateRect(xPos3, yPos1, "#FDB500");
CreateRect(xPos3, yPos2, "#8DB723");
CreateRect(xPos3, yPos3, "#AFFC3B");
CreateRect(xPos3, yPos4, "#60FC60");

CreateRect(xPos4, yPos1, "red");
CreateRect(xPos4, yPos2, "#FDB500");
CreateRect(xPos4, yPos3, "#F6FF33");
CreateRect(xPos4, yPos4, "#C0FC3E");

//CreateText(xPos1 + (boxW / 2), height, "0", "black");
//CreateText(xPos2 + (boxW / 2), height, "1", "black");
//CreateText(xPos3 + (boxW / 2), height, "2", "black");
//CreateText(xPos4 + (boxW / 2), height, "3", "black");

//CreateText(xPos1 + 5, yPos1 + (boxH / 2), "3", "black");
//CreateText(xPos1 + 5, yPos2 + (boxH / 2), "2", "black");
//CreateText(xPos1 + 5, yPos3 + (boxH / 2), "1", "black");
//CreateText(xPos1 + 5, yPos4 + (boxH / 2), "0", "black");

//svgContainer.append("text")
// .attr("x", 85)
// .attr("y", 125)
// .attr("font-size", 55)
// .text("3")
// .attr("onclick", "alert('You clicked A');");

HTML

<div id="myConta">
</div>

CSS

  .hover_group:hover {
opacity: 0.5;
}
#myConta {
background-color:black;
text-align:right;
}

最佳答案

根据 this从另一个问题回答,你可以使用d3.svg.axis()

另一种解决方法是增加您的 SVG 宽度/高度,在 SVG 的右侧和底部插入与背景颜色相同的矩形,这将允许您将标签放置在两侧新添加的矩形上

这是一个基于您的代码的 jsfiddle 演示(在 chrome 上测试过)

jsfiddle

关于javascript - 与 SVG 矩形内联的 Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29554059/

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