简体   繁体   English

如何使两个d3图形彼此相邻显示?

[英]How can I get my two d3 graphs to appear next to one another?

I'm trying to get these 2 divs called bar1 and bar2 to appear next to one another. 我试图使这两个称为bar1和bar2的div彼此相邻。 I tried messing a little bit with the svg and div ids. 我试着把svg和div id弄乱了。 Not sure how to get these two graphs to be side-by-side. 不确定如何使这两个图并排。

Here is bar1: 这是bar1:

var margin = {top: 20, right: 20, bottom: 30, left: 40};

  var width = 360 - margin.left - margin.right,
  height = 330 - margin.top - margin.bottom;

  var chart1 = d3.select("#bar1")
     .append("svg")
     .attr('id', 'bar1svg')
     .attr('width', width + margin.left + margin.right)
     .attr('height', height + margin.top + margin.bottom);

  var svg = d3.select("#bar1svg");

  var x = d3.scaleBand().rangeRound([0, width]).padding(0.1),
   y = d3.scaleLinear().rangeRound([height, 0]);

var g = chart1.append("g")
  .attr("transform", "translate(" + margin.left + "," + margin.top +       ")");

 d3.csv ( "data/housingdata.csv", function(d) {
  d.median_housing_prices = +d.median_housing_prices;
  return d;
 }, function(error, data) {
  if (error) throw error;

 x.domain(data.map(function(d) { return d.neighborhoods; }));
 y.domain([0, d3.max(data, function(d) { return      d.median_housing_prices; })]);

g.append("g")
    .attr("class", "axis axis--x")
    .attr("transform", "translate(0," + height + ")")
    .call(d3.axisBottom(x));

g.append("g")
    .attr("class", "axis axis--y")
    .call(d3.axisLeft(y).ticks(20, 's'))
  .append("text")
    .attr("transform", "rotate(-90)")
    .attr("y", 6)
    .attr("dy", "0.71em")
    .attr("text-anchor", "end")
    .text("Frequency");

   g.selectAll(".bar")
  .data(data)
  .enter().append("rect")
    .attr("class", "bar")
    .attr("x", function(d) { return x(d.neighborhoods); })
    .attr("y", function(d) { return y(d.median_housing_prices); })
    .attr("width", x.bandwidth())
    .attr("height", function(d) { return height - y(d.median_housing_prices); });

 });

Here is bar 2: 这是栏2:

 var chart2 = d3.select("#bar2")
   .append("svg")
   .attr('id', 'bar2svg')
   .attr('width', width + margin.left + margin.right)
   .attr('height', height + margin.top + margin.bottom);

    var g2 = chart2.append("g2")
    .attr("transform", "translate(" + margin.left + "," + margin.top +   ")");

 d3.csv("data/housingdata2.csv", function(d) {
 d.median_housing_prices = +d.median_housing_prices;
 return d;
  }, function(error, data) {
  if (error) throw error;

x.domain(data.map(function(d) { return d.neighborhoods; }));
y.domain([0, d3.max(data, function(d) { return d.median_housing_prices; })]);

g2.append("g2")
    .attr("class", "axis axis--x")
    .attr("transform", "translate(0," + height + ")")
    .call(d3.axisBottom(x));

g2.append("g2")
    .attr("class", "axis axis--y")
    .call(d3.axisLeft(y).ticks(10, 's'))
  .append("text")
    .attr("transform", "rotate(-90)")
    .attr("y", 6)
    .attr("dy", "0.71em")
    .attr("text-anchor", "end")
    .text("Frequency");

g2.selectAll(".bar")
  .data(data)
  .enter().append("rect")
    .attr("class", "bar")
    .attr("x", function(d) { return x(d.neighborhoods); })
    .attr("y", function(d) { return y(d.median_housing_prices); })
    .attr("width", x.bandwidth())
    .attr("height", function(d) { return height - y(d.median_housing_prices); });
 });

There is a lot of d3 code there, obscuring the fact that you don't have a d3 or even an SVG problem, but a standard HTML / CSS layout issue. 那里有很多d3代码,这掩盖了您没有d3甚至是SVG问题,但没有标准的HTML / CSS布局问题的事实。

Browsers try to position adajacent <svg> (and <img> ) elements side by side, but then switch to a vertical layout if they judge the elements together as "too wide" for the current width. 浏览器尝试将相邻的<svg> (和<img> )元素并排放置,但是如果它们将这些元素一起判断为当前宽度“太宽”,则切换到垂直布局。 In CSS layout lingo, they have display: inline by default. 在CSS布局术语中,它们display: inline默认情况下为display: inline <div> are block elements, however, so lay out vertically unless that is changed by CSS. 但是, <div>是块元素,因此,请垂直放置,除非CSS对其进行了更改。 The trick is subverting this, and insisting on the side by side layout, like so: 诀窍是颠覆这一点,并坚持并排布局,如下所示:

典型的例子

Note the second <svg> is clipped by the window border, because you insisted on side by side layout, come what may. 请注意,第二个<svg>被窗口边框剪裁了,因为您坚持并排布局,所以可能吧。

All of the approaches to get this are HTML / CSS fixes. 实现此目的的所有方法都是HTML / CSS修复。 Eg: 例如:

  • Put the SVGs in adjacent table cells 将SVG放在相邻的表格单元格中
  • Use the float property to make the SVGs float left or right. 使用float属性使SVG向左或向右浮动。
  • Add a container <div> and absolutely position the SVGs therein. 添加一个容器<div>并将SVG绝对放置在其中。
  • Add a container <div> and use text wrapping control to keep the SVGs side by side. 添加一个容器<div>并使用文本换行控件来保持SVG并排。
  • Probably a half-dozen others. 大概六个人。

Using a containing <div> with CSS property white-space: nowrap and then setting your SVG-continaing <div> elements to display: inline is an easy solution that nicely structures your HTML, yet is simple, reliable, and maintanable in ways the table, float, and absolute positioning approaches aren't. 将包含<div>的CSS属性设置为white-space: nowrap ,然后将要display: inline SVG的<div>元素设置为display: inline是一种简单的解决方案,可以很好地构造HTML,但通过以下方式是简单,可靠且可维护的:表格,浮动和绝对定位方法不是。 The quick and dirty formulation in your HTML follows. HTML中快速而肮脏的表述如下。 (It's neater to define the styling in your CSS file.) (在CSS文件中定义样式比较整齐。)

<div style="white-space: nowrap;">
    <div id="bar1" style="display: inline"></div>
    <div id="bar2" style="display: inline"></div>
</div>

Here is a running demo of the idea. 这是一个想法的演示

The disadvantage to this "always go side by side!" 这种“始终并存”的缺点! command is, well, you may be successful. 命令很好,您可能会成功。 Then the elements are side by side always, even if there isn't enough room for them to be fully displayed. 这样,即使没有足够的空间可以完全显示元素,元素也始终并排放置。 If you don't want to be so always and forever, try a float: left style on #bar1 , with very little margins on your <div> and <svg> . 如果您不想一直如此,请尝试在#bar1上使用float: left样式,在<div><svg>上留出很少的空白。 In the Fiddle , you can change the window width to see this dynamc layout decision in action. Fiddle中 ,您可以更改窗口宽度以查看此动态布局决策。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM