javascript - D3线由多段组成
问题描述
我根据以下代码画了一条线:在x和y刻度下创建
const x = d3.scaleLinear()
.domain([0, data.length - 1])
.range([this.margin.left, contentWidth]);
const y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.value)])
.range([20, 0]);
一个从 0 到 data.length - 1 的简单比例,另一个计算数据的最大值。
const apLineBottom = d3.line()
.x(d => x(data.indexof(d))
.y(d => (contentHeight / 2) + y(d.value));
const apLineTop = d3.line()
.x(d => x(data.indexof(d))
.y(d => (contentHeight / 2) - 10 - y(d.value));
接下来是线条,我希望它们在中间相互平行(根据给定的数据上下移动)。
svg.append("path")
.attr("class", "line")
.attr("d", apLineBottom(data))
.style("fill", "none")
.style("stroke", "red")
.style("stroke-width", 2);
svg.append("path")
.attr("class", "line")
.attr("d", apLineTop(data))
.style("fill", "none")
.style("stroke", "red")
.style("stroke-width", 2);
接下来,我将它们添加到 svg 中,结果如下:这不是我想要的,因为东西突出了。我错过了什么来平滑曲线吗?
供参考:图像仅包含底线。
提前致谢
解决方案
推荐阅读
- sql - 根据 vba sql 中销售的数量,按前 5 个类别的每个子类别查找平均收入
- linux-kernel - 如何在 SELinux 的 UBIFS 中启用扩展属性?
- jenkins-groovy - 詹金斯管道“意外令牌”
- jmeter - 如何在 Jmeter 中创建不同的用户输入
- azure - Azure ASP.NET Web Forms 能否用于构建 IoT 应用程序?
- vue.js - 如何在 VueJS 中初始化 Swiper?
- javascript - 获取 HTTP 请求命令
- laravel - 从 API 获取有效响应时出错。检查日志文件以获取错误详细信息
- sql-server - 对基于日期的变量执行计算时未获得预期值
- python - Python:如何仅基于不同的列合并两个数据框?