angularjs - 如何仅更改 D3.js 图表中数据网格或 x 和 y 轴的颜色
问题描述
我想从 X 和 Y 轴制作单独的数据网格。非常轻的 DataGrid 和深色的 X 和 Y 轴
我试过了,但它总是取笔画=路径域中的当前颜色
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.innerTickSize(-height)
.outerTickSize(0)
.tickPadding(10);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.innerTickSize(-width)
.outerTickSize(0)
.tickPadding(10);
svg.append("g") // Add the X Axis
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g") // Add the Y Axis
.attr("class", "y axis")
.call(yAxis);
这是CSS
.axis line {
stroke: red !important;
opacity: 0.3;
}
.axis path {
stroke: red !important;
opacity: 0.3;
}
.axis text {
fill: red !important;
}
[![enter image description here][2]][2]
解决方案
有两种方法可以解决这个问题:
有一个 d3 解决方案,基本上不需要 css
svg.append("g") // Add the X Axis
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.call(g => g.selectAll(".tick:first-of-type line")
.attr("class", "axis_bar")
.attr("stroke", "black"))
.call(g => g.selectAll(".tick:not(:first-of-type) line")
.attr("class", "axis_y_tick")
.attr("stroke", "red"));
svg.append("g") // Add the Y Axis
.attr("class", "y axis")
.call(yAxis)
.call(g => g.selectAll(".tick:first-of-type line")
.attr("class", "axis_bar")
.attr("stroke", "black"))
.call(g => g.selectAll(".tick:not(:first-of-type) line")
.attr("class", "axis_y_tick")
.attr("stroke", "red"));
svg.append("path") // Add the valueline path.
.attr("d", valueline(data))
.attr("class", "graph")
.attr("stroke", "blue")
.attr("stroke-width", 2);
这将选择第一行和后面的每一行并应用不同的样式参数。
第二种解决方案是仅在 css 中执行此操作。您可能会在 d3 代码段中看到您需要的类选择器:
.tick:first-of-type line {} //Selects the first Tick/Line wich becomes the main axis.
.tick:not(:first-of-type) line {} // Will select every other tick line, but the first.
我编辑了你的小提琴适合 d3 解决方案:http: //jsfiddle.net/nk13jycw/2/
Mike Bostock 对此也有很好的观察:https ://observablehq.com/@d3/styled-axes
推荐阅读
- swift - 如何将 JSON 数组对象转换为列表数组 | 斯威夫特 5
- amazon-ec2 - AWS 中的 Diskio_io_time 是什么,我如何计算它的最大限制?
- azure - 如何使用 powershell 命令从 Visual Studio 社区部署逻辑应用程序?
- node.js - 未从命令行或 npm 配置设置使用 Python 失败的 NPM 安装
- r - ggplot 在全局环境中未定义时找不到自定义 key_glyph 函数
- python - 带有 SemVer 的 CI 中的功能分支工件工作流
- visual-studio-code - 从 VSCode 中删除制表符/空格缩进检查
- c# - 使用 C# 创建 Azure DevOPS 个人访问令牌 (PAT)
- tensorflow - Tensorflow GPU CUDA 无法加载动态库“libcufft.so.10”;错误
- r - 在 R 中绘制具有绝对值的分布图