在 div 或 td 等 html 元素中包含 D3 图表?

fdk*_*fsf 0 html javascript css jquery d3.js

最近,我开始在HTML D3图表工作,和我最常用的图表似乎是一个HTML元素,如中添加的<div><td>等等。

在这个D3 Radial bar Chart的情况下,它似乎不是 html 元素的一部分。例如,我想在它周围添加一个边框,所以我想可能包含在 div 或 table 中,然后将边框添加到 html 元素。但我不知道如何将它包含在这些元素中。本质上,HTML 如下所示:

<html>
<head>
<title></title>
</head>
<body>
<script>
   // This is where the control is drawn
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

所以,假设我想将它包含在 a 中,DIV以便我可以在它周围添加边框,那么我该怎么做呢?

就我而言,问题是图表用完了整个水平空间。就好像图表位于水平拉伸 100% 的 div 中一样。

这是代码。任何帮助表示赞赏。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Household monthly electricity consumption</title>
<style>
body {
  font: 12px sans-serif;
}

svg {
  margin: 0px auto;
  display: block;
}

path.arc {
  opacity: 0.9;
  transition: opacity 0.5s;
}

path.arc:hover {
  opacity: 0.7;
}

.axis line, .axis circle  {
  stroke: #cccccc;
  stroke-width: 1px
}

.axis circle {
  fill: none;
}

.r.axis text {
  text-anchor: end
}

</style>
<script src="https://d3js.org/d3.v4.min.js"></script>
</head>
<body>
<script>
const width = 960,
  height = 500,
  chartRadius = height / 2 - 40;

const color = d3.scaleOrdinal(d3.schemeCategory10);

let svg = d3.select('body').append('svg')
  .attr('width', width)
  .attr('height', height)
  .append('g')
    .attr('transform', 'translate(' + width / 2 + ',' + height / 2 + ')');

let tooltip = d3.select('body').append('div')
  .attr('class', 'tooltip');

const PI = Math.PI,
  arcMinRadius = 10,
  arcPadding = 10,
  labelPadding = -5,
  numTicks = 10;


d3.csv('energy.csv', (error, data) => {

  let scale = d3.scaleLinear()
    .domain([0, d3.max(data, d => d.value) * 1.1])
    .range([0, 2 * PI]);

  let ticks = scale.ticks(numTicks).slice(0, -1);
  let keys = data.map((d, i) => d.name);
  //number of arcs
  const numArcs = keys.length;
  const arcWidth = (chartRadius - arcMinRadius - numArcs * arcPadding) / numArcs;

  let arc = d3.arc()
    .innerRadius((d, i) => getInnerRadius(i))
    .outerRadius((d, i) => getOuterRadius(i))
    .startAngle(0)
    .endAngle((d, i) => scale(d))

  let radialAxis = svg.append('g')
    .attr('class', 'r axis')
    .selectAll('g')
      .data(data)
      .enter().append('g');

  radialAxis.append('circle')
    .attr('r', (d, i) => getOuterRadius(i) + arcPadding);

  radialAxis.append('text')
    .attr('x', labelPadding)
    .attr('y', (d, i) => -getOuterRadius(i) + arcPadding)
    .text(d => d.name);

  let axialAxis = svg.append('g')
    .attr('class', 'a axis')
    .selectAll('g')
      .data(ticks)
      .enter().append('g')
        .attr('transform', d => 'rotate(' + (rad2deg(scale(d)) - 90) + ')');

  axialAxis.append('line')
    .attr('x2', chartRadius);

  axialAxis.append('text')
    .attr('x', chartRadius + 10)
    .style('text-anchor', d => (scale(d) >= PI && scale(d) < 2 * PI ? 'end' : null))
    .attr('transform', d => 'rotate(' + (90 - rad2deg(scale(d))) + ',' + (chartRadius + 10) + ',0)')
    .text(d => d);

  //data arcs
  let arcs = svg.append('g')
    .attr('class', 'data')
    .selectAll('path')
      .data(data)
      .enter().append('path')
      .attr('class', 'arc')
      .style('fill', (d, i) => color(i))

  arcs.transition()
    .delay((d, i) => i * 200)
    .duration(1000)
    .attrTween('d', arcTween);

  arcs.on('mousemove', showTooltip)
  arcs.on('mouseout', hideTooltip)


  function arcTween(d, i) {
    let interpolate = d3.interpolate(0, d.value);
    return t => arc(interpolate(t), i);
  }

  function showTooltip(d) {
    tooltip.style('left', (d3.event.pageX + 10) + 'px')
      .style('top', (d3.event.pageY - 25) + 'px')
      .style('display', 'inline-block')
      .html(d.value);
  }

  function hideTooltip() {
    tooltip.style('display', 'none');
  }

  function rad2deg(angle) {
    return angle * 180 / PI;
  }

  function getInnerRadius(index) {
    return arcMinRadius + (numArcs - (index + 1)) * (arcWidth + arcPadding);
  }

  function getOuterRadius(index) {
    return getInnerRadius(index) + arcWidth;
  }
});
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Dek*_*kel 5

您示例中的脚本创建了一个新svg元素并将其附加到body

d3.select('body').append('svg')
Run Code Online (Sandbox Code Playgroud)

如果需要 - 您可以在该 svg 周围添加边框:

svg {
    border: 1px solid black;
}
Run Code Online (Sandbox Code Playgroud)

或者另一种选择 - 您可以将该svg元素附加到页面中的某个其他元素(其他一些div)并设置该div元素的样式。

d3.select('div#div-id').append('svg')
Run Code Online (Sandbox Code Playgroud)