javascript - 如何在一个 html 页面上应用两个 svg?

标签 javascript html css d3.js svg

所以我有 2 个图表:折线图和箱线图,但我希望箱线图位于折线图旁边,但它位于下方。我如何使它位于折线图旁边?任何想法表示赞赏。谢谢!

我尝试使用 2 个不同的 SVG,但没有用。这是我的代码:http://next.plnkr.co/edit/mWGOEy0GHp1olZgNgtpA?preview

<!DOCTYPE html>
<meta charset="utf-8">
<style>

.line {
    fill: none;                 
  stroke: url(#grad);    
  stroke-width: 2px; 
}
.zoom {
  cursor: move;
  fill: none;
  pointer-events: all;
}

</style>
<svg width="900" height="700">  
</svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>

var svg = d3.select("svg"),
    margin = {top: 90, right: 50, bottom: 100, left: 40},
    margin2 = {top: 630, right: 50, bottom: 30, left: 40},
    width = +svg.attr("width") - margin.left - margin.right,
    height = +svg.attr("height") - margin.top - margin.bottom,
    height2 = +svg.attr("height") - margin2.top - margin2.bottom;

...more code 
</script>

//end of line graph 

//beginning of boxplot
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>

<!-- Create a div where the graph will take place -->
<div id="svg2"></div>

<!-- To use the monochrome scale -->
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>

<!-- Tooltip style -->
<style>
.tooltip {
  background-color: black;
  border: 1px black solid;
  display: inline-block;
  border-radius: 5px;
  padding: 15px;
  min-width: 1000px;
  text-align: left;
  color: white;  
}
</style>

<script>
// set the dimensions and margins of the graph
var margin = {top: 70, right: 30, bottom: 50, left: 80},
    width = 460 - margin.left - margin.right,
    height = 400 - margin.top - margin.bottom;

...more code, end of boxplot
</script>

This is what it looks like right now

最佳答案

有两种基本的 HTML 元素—— block 元素和内联元素。 block 元素(例如 div)会将跟随它们的元素向下推到下一行。内联元素(例如 span 的)则不会,而是与它后面的内容共享一行,除非没有足够的空间容纳它们。

如果您希望 block 元素实际上表现得像内联元素(关于换行符),您可以应用 float: left到该元素的 CSS。

编辑: 我这样做的方法是添加一个名为 float-left 的类在第一个 SVG 上,然后修改你的一个 <style>要包含的标签

.float-left {
    float: left;
}

这也将允许您添加 float-left类到您想要对其执行此行为的任何其他元素。希望对您有所帮助。

关于javascript - 如何在一个 html 页面上应用两个 svg?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56692977/

相关文章:

javascript - 每次重新渲染时都会计算 useState 和 useRef 初始值

javascript - api未在ajax方法中加载,但在脚本标签中加载

CSS:获取父级内部的所有可用高度而不会溢出

html - 将负边距与社交分享按钮(计数器)一起使用可以吗?

html - 我可以更改特定 aspx 的母版页 css 吗?

javascript - 如何使用 css 将不同颜色应用于 HTML 中的不同行 [不是交替行,而是任何行的任何颜色]

javascript - 在 UIWebView/WKWebView 中启用拖动

javascript - 提取网页的调色板

html - 更改 select 标签中单独选项的 css 字体系列

html - 滚动条什么都没有 - 我怎样才能让我的页面更短?