[d3js] 已写好的绘图function调用不成功,网页无图像显示?

最近在学习 d3 brush 见官网有案例边下载调试但不成功

将function写在 <script> 标签内调用也没有出错,可是网页无显示任何图像,代码如下:

相关代码:

<!DOCTYPE html>
<html>
<head>
    <script src = "https://d3js.org/d3.v7.min.js"></script>
</head>
<body>
<script>
var margin = ({top: 20, right: 20, bottom: 30, left: 40})
var width = 960 - margin.left - margin.right
var height = 440
var focusHeight = 100
var data =  [{ "date":"0.1",  "value":394.46}, 
  { "date":"0.2",  "value":1366.42}, 
  { "date":"0.3",  "value":1498.58}, 
  { "date":"0.4",  "value":1452.43}, 
  { "date":"0.5",  "value":20.6}, 
  { "date":"0.6",  "value":1454.6}, 
  { "date":"0.7",  "value":30.83}, 
  { "date":"0.8",  "value":1217.68}, 
  { "date":"0.9",  "value":1436.51}, 
  { "date":"1.0",  "value":29.4}
  ];

var area = (x, y) => d3.area()
    .defined(d => !isNaN(d.value))
    .x(d => x(d.date))
    .y0(y(0))
    .y1(d => y(d.value))


var x = d3.scaleUtc()
    .domain(d3.extent(data, d => d.date))
    .range([margin.left, width - margin.right])
var y = d3.scaleLinear()
    .domain([0, d3.max(data, d => d.value)])
    .range([height - margin.bottom, margin.top])  

var xAxis = (g, x, height) => g
    .attr("transform", `translate(0,${height - margin.bottom})`)
    .call(d3.axisBottom(x).ticks(width / 80).tickSizeOuter(0))  

var yAxis = (g, y, title) => g
    .attr("transform", `translate(${margin.left},0)`)
    .call(d3.axisLeft(y))
    .call(g => g.select(".domain").remove())
    .call(g => g.selectAll(".title").data([title]).join("text")
        .attr("class", "title")
        .attr("x", -margin.left)
        .attr("y", 10)
        .attr("fill", "currentColor")
        .attr("text-anchor", "start")
        .text(title))



var chart = function(){
  const svg = d3.create("svg")
      .attr("viewBox", [0, 0, width, height])
      .style("display", "block");

  svg.append("clipPath")
      .attr("id", "clip")
    .append("rect")
      .attr("x", margin.left)
      .attr("y", 0)
      .attr("height", height)
      .attr("width", width - margin.left - margin.right);

  const gx = svg.append("g");

  const gy = svg.append("g");

  const path = svg.append("path")
      .datum(data)
      .attr("clip-path", "clip")
      .attr("fill", "steelblue");

  return Object.assign(svg.node(), {
    update(focusX, focusY) {
      gx.call(xAxis, focusX, height);
      gy.call(yAxis, focusY, data.y);
      path.attr("d", area(focusX, focusY));
    }
  });
}
chart()

var focus = function(){
  const svg = d3.create("svg")
      .attr("viewBox", [0, 0, width, focusHeight])
      .style("display", "block");

  const brush = d3.brushX()
      .extent([[margin.left, 0.5], [width - margin.right, focusHeight - margin.bottom + 0.5]])
      .on("brush", brushed)
      .on("end", brushended);

  const defaultSelection = [x(d3.utcYear.offset(x.domain()[1], -1)), x.range()[1]];

  svg.append("g")
      .call(xAxis, x, focusHeight);

  svg.append("path")
      .datum(data)
      .attr("fill", "steelblue")
      .attr("d", area(x, y.copy().range([focusHeight - margin.bottom, 4])));

  const gb = svg.append("g")
      .call(brush)
      .call(brush.move, defaultSelection);

  function brushed({selection}) {
    if (selection) {
      svg.property("value", selection.map(x.invert, x).map(d3.utcDay.round));
      svg.dispatch("input");
    }
  }

  function brushended({selection}) {
    if (!selection) {
      gb.call(brush.move, defaultSelection);
    }
  }

  return svg.node();
}
focus()

var update = function(){
  const [minX, maxX] = focus;
  const maxY = d3.max(data, d => minX <= d.date && d.date <= maxX ? d.value : NaN);
  chart.update(x.copy().domain(focus), y.copy().domain([0, maxY]));
}


</script>
</body>
</html>

本人纯新手,还请帮忙看看如何在网页显示图像,谢谢。

阅读 1.7k
1 个回答
✓ 已被采纳

你的问题是你创建了 chart 和 focus 函数,但没有把它们添加到 HTML 中

<!DOCTYPE html>
<html>
<head>
    <script src="https://d3js.org/d3.v7.min.js"></script>
</head>
<body>
  <div id="chart-container"></div>
  <div id="focus-container"></div>
  <script>
    // ...你的原始代码...

    const chartNode = chart();
    document.getElementById("chart-container").appendChild(chartNode);

    const focusNode = focus();
    document.getElementById("focus-container").appendChild(focusNode);
  </script>
</body>
</html>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题