javascript - 将 CSS 元素映射到 D3

标签 javascript css d3.js

我有一个用 D3 绘制条形图的功能。我需要将此 CSS 元素添加到我的结构中:

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

.x.axis path {
  display: none;
}

我不明白应该将这些 css 元素映射到哪些变量或如何映射。

我用过this example对于我的代码。基本上我正在尝试映射这些 css 元素,例如:

svg.append("g")
        .attr("class", "y axis")
        .call(yAxis)
        .append("text")
        .attr("transform", "rotate(-90)")
        .attr("y", 6)
        .attr("dy", ".71em")
        .style("font-family","sans-serif")
        .style("font-size","10px")
        .style("text-anchor", "end")
        .text("Publications");

但我不知道如何映射.axis path.axis line.x.axis path css 元素。如果有任何帮助,我将不胜感激。

最佳答案

<html>
  <head>
    <style>
      .axis path,
      .axis line {
        fill: none;
        stroke: #000;
        shape-rendering: crispEdges;
      }

      .x.axis path {
        display: none;
      }
    </style>
  </head>
  <body>
    <script>
      svg = ...// Your code here
      yAxis = ...// Your code here
      svg.append("g")
        .attr("class", "y axis")
        .call(yAxis)
        .append("text")
        .attr("transform", "rotate(-90)")
        .attr("y", 6)
        .attr("dy", ".71em")
        .style("font-family","sans-serif")
        .style("font-size","10px")
        .style("text-anchor", "end")
        .text("Publications");
    </script>
  </body>
</html>

当你执行 .call(yAxis) , d3 创建与轴相关的 <path><line> <g class="y axis"> 中的节点.一旦 d3 创建它们,这些节点将满足 <style> 中的 css 选择器。 (例如 .axis path )并关闭中风等。我相信这回答了我认为你在问什么。

关于javascript - 将 CSS 元素映射到 D3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29659864/

相关文章:

javascript - es6 js promise : What is . catch(callback) 相当于?

javascript - 如何使用 Firebase 云消息传递通过 Web 浏览器订阅主题

android - HTML/CSS : How to prevent Android and iPhone browsers from upscaling background images

javascript - D3.js 未在 HTML 中附加更新的树

javascript - 使用 str.indexOf 函数时为 "Uncaught TypeError: undefined is not a function"

javascript - 从点到圆 Angular 矩形或椭圆的最长距离

javascript - 无法创建自动填充数组而不手动写入

javascript - D3.js 将文本换行为矩形

javascript - 当 2 个 div 重叠时,你如何做一个 JQuery slideDown?

Python Selenium 循环