Javascript 与SVG rect内联的Divs

Javascript 与SVG rect内联的Divs,javascript,jquery,html,css,svg,Javascript,Jquery,Html,Css,Svg,我想要这个 到目前为止,我所取得的成就是: 所有我需要的是添加编号的一面和文字销售和利润,我试图添加几个div,但他们带来的空间上面,什么是最好的方式添加这些数字在这里,我想这样做在JS最好的框数可能会小心 代码 HTML 根据另一个问题的答案,您可以使用d3.svg.axis 另一个解决方法是增加SVG的宽度/高度,在SVG的右侧和底部插入与背景颜色相同的矩形,这将允许您将标签放置在侧面新添加的矩形上 下面是一个基于您在chrome上测试的代码的JSFIDLE演示 不使用SVG文本元素有什么原

我想要这个

到目前为止,我所取得的成就是:

所有我需要的是添加编号的一面和文字销售和利润,我试图添加几个div,但他们带来的空间上面,什么是最好的方式添加这些数字在这里,我想这样做在JS最好的框数可能会小心

代码

HTML

根据另一个问题的答案,您可以使用d3.svg.axis

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

下面是一个基于您在chrome上测试的代码的JSFIDLE演示


不使用SVG文本元素有什么原因吗?
$("#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');");
<div id="myConta">
</div>
  .hover_group:hover {
      opacity: 0.5;
  }
  #myConta {
      background-color:black;
      text-align:right;
  }