图例未附加到div中,但在div外部打印 [英] Legend not getting appended into div but printing outside the div

查看:67
本文介绍了图例未附加到div中,但在div外部打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<!DOCTYPE html>
<html>

<head>

  <meta http-equiv="Content-type" content="text/html; charset=utf-8">
  <title>Testing Donut Chart</title>
<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>

  <style type="text/css">
    #first {
      /*    width: 500px;
    height: 300px;*/
      height: 50%;
      width: 50%;
      border: 3px solid #73AD21;
      position: absolute;
    }
    #chart {
      position: absolute;
      height: 100%;
      width: 100%;
    }
    #chart legend {
      position: absolute;
      margin: 0%;
    }
    #first legend {
      position: absolute;
      margin: 0%;
    }
    .slice path {
      stroke: #fff;
      stroke-width: 1px;
    }
    .textTop {
      font-family: 'Segoe UI';
      font-weight: bold;
      font-size: 10pt;
      fill: #2c3218;
    }
    .textBottom {
      fill: #444;
      font-family: 'Segoe UI';
      font-weight: bold;
      font-size: 10pt;
    }
  </style>
</head>

<body>
  <div id="container">
    <svg id="chart" viewBox="0 0 960 500" perserveAspectRatio="xMinYMid">
      <div id="first">
        <script type="text/javascript">
          var w = document.getElementById('first').clientWidth;
           // alert(w);
          var h = document.getElementById('first').clientHeight;
           // alert(h);
          var r = Math.min(w, h) / 2 - 50;
           // alert(r);
          var inner = 70;
          var color = d3.scale.category10();

          var data = [
            ["192.168.12.1", 20],
            ["76.09.45.34", 40],
            ["34.91.23.76", 80],
            ["192.168.19.32", 16],
            ["192.168.10.89", 50],
            ["192.178.34.07", 18],
            ["192.168.12.98", 30]
          ];

          var data = data.map(function(d) {
            return {
              IP: d[0],
              count: d[1]
            };
          });

          var total = d3.sum(data, function(d) {
            return d3.sum(d3.values(d));
          });

          var vis = d3.select("#first")
            .append("svg:svg")
            .data([data])
            .attr("width", w)
            .attr("height", h)
            .append("svg:g")
            .attr("transform", "translate(" + r * 1.5 + "," + r * 1.5 + ")");

          var textTop = vis.append("text")
            .attr("dy", ".35em")
            .style("text-anchor", "middle")
            .attr("class", "textTop")
            .text("TOTAL")
            .attr("y", -10),

            textBottom = vis.append("text")
            .attr("dy", ".35em")
            .style("text-anchor", "middle")
            .attr("class", "textBottom")
            .text(total)
            .attr("y", 10);

          var arc = d3.svg.arc()
            .innerRadius(inner)
            .outerRadius(r);

          var arcOver = d3.svg.arc()
            .innerRadius(inner + 0)
            .outerRadius(r + 1);

          var pie = d3.layout.pie()
            .sort(null)
            .startAngle(1.1 * Math.PI)
            .endAngle(3.1 * Math.PI)
            .value(function(d) {
              return d.count;
            });

          var arcs = vis.selectAll("g.slice")
            .data(pie)
            .enter()
            .append("svg:g")
            .attr("class", "slice")
            .on("mouseover", function(d) {
              d3.select(this).select("path").transition()
                .duration(200)
                .attr("d", arcOver)
                .style('opacity', 0.5)

              textTop.text(d3.select(this).datum().data.IP)
                .attr("y", -10);
              textBottom.text(d3.select(this).datum().data.count)
                .attr("y", 10);
            })
            .on("mouseout", function(d) {
              d3.select(this).select("path").transition()
                .duration(100)
                .attr("d", arc)
                .style('opacity', 1);

              textTop.text("TOTAL")
                .attr("y", -10);
              textBottom.text(total);
            });

          arcs.append("svg:path")
            .attr("fill", function(d, i) {
              return color(i);
            })
            .attr("d", arc)
            .transition()
            .ease("exp")
            .duration(1000)
            .attrTween("d", tweenPie);

          function tweenPie(b) {
            var i = d3.interpolate({
              startAngle: 1.1 * Math.PI,
              endAngle: 1.1 * Math.PI
            }, b);
            return function(t) {
              return arc(i(t));
            };
          }

          var legend = d3.select("#first")
            .append("svg")
            .attr("x", w)
            .attr("y", h)
            .attr("class", "legend")
            .attr("width", w / 2)
            .attr("height", h)
            .selectAll("g")
            .data(data)
            .enter()
            .append("g")
            .attr("transform", function(d, i) {
              return "translate(0," + i * 20 + ")";
            });

          legend.append("rect")
            .attr("width", 18)
            .attr("height", 18)
            .style("fill", function(d, i) {
              return color(i);
            });

          legend.append("text")
            .attr("x", 24)
            .attr("y", 9)
            .attr("dy", ".35em")
            .text(function(d) {
              return d.IP;
            });
        </script>
      </div>
    </svg>
  </div>

  <script type="text/javascript">
    var chart = $("#chart"),
      aspect = chart.width() / chart.height(),
      container = chart.parent();
    $(window).on("resize", function() {
      var targetWidth = container.width();
      chart.attr("width", targetWidth);
      chart.attr("height", Math.round(targetWidth / aspect));
    }).trigger("resize");
  </script>

</body>

</html>

我已经尝试了很多,但是无法在div #first内正确显示甜甜圈和图例.我试图改变很多事情,但没有任何效果.这是我使用d3尝试执行的操作是不可能的,还是我正在执行任何特定的错误.另外,div和图表也没有响应.我很迷惑.请任何人帮我. 预先感谢您的帮助.

I have tried a lot but I am unable to display the donut and the legends properly inside the div #first. I have tried to change a lot of things but nothing works for. Is this impossible what I am trying to do using d3 or there is any specific error that I am doing. Also the div and chart are not getting responsive also. I am confused. Please anyone help me out. Thanks in advance for help.

推荐答案

您正在将单独的SVG用于图例和pichart.我将图例附加到以前使用的SVG中,并对其位置进行了转换.

You were using separate SVGs for for legend and pichart. I append legend into previously used SVG and transform for its position.

下面的代码段可能会对您有所帮助.

Below code snippet may help you.

<!DOCTYPE html>
<html>

<head>

  <meta http-equiv="Content-type" content="text/html; charset=utf-8">
  <title>Testing Donut Chart</title>
<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>

  <style type="text/css">
    #first {
      /*    width: 500px;
    height: 300px;*/
      height: 95% !important;
      width: 95% !important;
      border: 3px solid #73AD21 !important;
      position: absolute !important;
    }
    #chart {
      position: absolute;
      height: 100%;
      width: 100%;
    }
    #chart legend {
      position: absolute;
      margin: 0%;
    }
    #first legend {
      position: absolute;
      margin: 0%;
    }
    .slice path {
      stroke: #fff;
      stroke-width: 1px;
    }
    .textTop {
      font-family: 'Segoe UI';
      font-weight: bold;
      font-size: 10pt;
      fill: #2c3218;
    }
    .textBottom {
      fill: #444;
      font-family: 'Segoe UI';
      font-weight: bold;
      font-size: 10pt;
    }
  </style>
</head>

<body>
  <div id="container">
    <svg id="chart" viewBox="0 0 960 500" perserveAspectRatio="xMinYMid">
      <div id="first">
        <script type="text/javascript">
          var w = document.getElementById('first').clientWidth;
           // alert(w);
          var h = document.getElementById('first').clientHeight;
           // alert(h);
          var r = Math.min(w, h) / 2 - 50;
           // alert(r);
          var inner = 70;
          var color = d3.scale.category10();

          var data = [
            ["192.168.12.1", 20],
            ["76.09.45.34", 40],
            ["34.91.23.76", 80],
            ["192.168.19.32", 16],
            ["192.168.10.89", 50],
            ["192.178.34.07", 18],
            ["192.168.12.98", 30]
          ];

          var data = data.map(function(d) {
            return {
              IP: d[0],
              count: d[1]
            };
          });

          var total = d3.sum(data, function(d) {
            return d3.sum(d3.values(d));
          });

          var vis = d3.select("#first")
            .append("svg:svg")
            .data([data])
            .attr("width", w)
            .attr("height", h)
            .append("svg:g")
            .attr("transform", "translate(" + r * 1.5 + "," + r * 1.5 + ")");

          var textTop = vis.append("text")
            .attr("dy", ".35em")
            .style("text-anchor", "middle")
            .attr("class", "textTop")
            .text("TOTAL")
            .attr("y", -10),

            textBottom = vis.append("text")
            .attr("dy", ".35em")
            .style("text-anchor", "middle")
            .attr("class", "textBottom")
            .text(total)
            .attr("y", 10);

          var arc = d3.svg.arc()
            .innerRadius(inner)
            .outerRadius(r);

          var arcOver = d3.svg.arc()
            .innerRadius(inner + 0)
            .outerRadius(r + 1);

          var pie = d3.layout.pie()
            .sort(null)
            .startAngle(1.1 * Math.PI)
            .endAngle(3.1 * Math.PI)
            .value(function(d) {
              return d.count;
            });

          var arcs = vis.selectAll("g.slice")
            .data(pie)
            .enter()
            .append("svg:g")
            .attr("class", "slice")
            .on("mouseover", function(d) {
              d3.select(this).select("path").transition()
                .duration(200)
                .attr("d", arcOver)
                .style('opacity', 0.5)

              textTop.text(d3.select(this).datum().data.IP)
                .attr("y", -10);
              textBottom.text(d3.select(this).datum().data.count)
                .attr("y", 10);
            })
            .on("mouseout", function(d) {
              d3.select(this).select("path").transition()
                .duration(100)
                .attr("d", arc)
                .style('opacity', 1);

              textTop.text("TOTAL")
                .attr("y", -10);
              textBottom.text(total);
            });

          arcs.append("svg:path")
            .attr("fill", function(d, i) {
              return color(i);
            })
            .attr("d", arc)
            .transition()
            .ease("exp")
            .duration(1000)
            .attrTween("d", tweenPie);

          function tweenPie(b) {
            var i = d3.interpolate({
              startAngle: 1.1 * Math.PI,
              endAngle: 1.1 * Math.PI
            }, b);
            return function(t) {
              return arc(i(t));
            };
          }

          var legend = vis.append("g")
            /*.attr("id", "first")
            .append("g")*/
            .attr("transform", "translate("+w / 3+", -"+ h/4 +")")
            .attr("class", "legend")
            .attr("width", w / 2)
            .attr("height", h)
            .selectAll("g")
            .data(data)
            .enter()
            .append("g")
            .attr("transform", function(d, i) {
              return "translate(0," + i * 20 + ")";
            });

          legend.append("rect")
            .attr("width", 18)
            .attr("height", 18)
            .style("fill", function(d, i) {
              return color(i);
            });

          legend.append("text")
            .attr("x", 24)
            .attr("y", 9)
            .attr("dy", ".35em")
            .text(function(d) {
              return d.IP;
            });
        </script>
      </div>
    </svg>
  </div>

  <script type="text/javascript">
    var chart = $("#chart"),
      aspect = chart.width() / chart.height(),
      container = chart.parent();
    $(window).on("resize", function() {
      var targetWidth = container.width();
      chart.attr("width", targetWidth);
      chart.attr("height", Math.round(targetWidth / aspect));
    }).trigger("resize");
  </script>

</body>

</html>

这篇关于图例未附加到div中,但在div外部打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆