d3.js - 使用 d3.js 版本 4 或版本 5 限制折线图中 X 轴上的平移
问题描述
我想使用 d3.js 版本 5 限制折线图中 X 轴上的平移。但我不知道该怎么做。在 X 轴上,我使用 scalePoint() 以日期格式显示数据,在 y 轴上 scaleLinear() 用于显示数字。你能帮我解决这个问题吗?这是我的代码
private createLineChart() {
this.width = 2000 - this.margin.left - this.margin.right;
this.height = 600 - this.margin.top - this.margin.bottom;
// X AXIS
this.xScale = d3.scalePoint()
.domain(this.dataset[0].fluencyData.map((data) => {
return new Date(data.date);
}))
.range([0, this.width]);
this.xScaleCopy = this.xScale.copy();
// Y AXIS
this.yScale = d3.scaleLinear()
.domain([0, 110])
.range([this.height, 0]);
this.xAxis = d3.axisBottom().scale(this.xScale).tickSizeOuter(0).tickFormat(d3.timeFormat('%b %d'));
this.yAxis = d3.axisLeft().scale(this.yScale).tickSize(-this.width);
// Line Generator
this.line = d3.line()
.x((data) => this.xScale(new Date(data.date)))
.y((data) => this.yScale(data.wcpm));
// .curve(d3.curveMonotoneX);
// Add SVG to Div
this.svg = d3.select('#displayChart').append('svg')
// .attr('preserveAspectRatio', 'xMinYMin meet')
// .attr(
// 'viewBox',
// '0 0 ' +
// (this.width + this.margin.left + this.margin.right) +
// ' ' +
// (this.height + this.margin.top + this.margin.bottom))
.attr('width', this.width + this.margin.left + this.margin.right)
.attr('height', this.height + this.margin.top + this.margin.bottom)
.attr('transform', 'translate(' + this.margin.left + ',' + this.margin.top + ')');
// Define the div for the tooltip
this.toolTipDiv = d3.select('#displayChart').append('div')
.attr('class', 'tooltip')
.style('opacity', 0);
// Append XAXIS to the SVG
const groupX = this.svg.append('g')
.attr('class', 'xAxis')
.attr('transform', 'translate(0,' + this.height + ')')
.call(this.xAxis);
// Append YAXIS to SVG
const groupY = this.svg.append('g')
.attr('class', 'yAxis')
.attr('transform', 'translate(20,0)')
.call(this.yAxis);
// Make a Path for Dataset
this.svg.append('path')
.datum(this.dataset[0].fluencyData)
.attr('class', 'line')
.attr('d', this.line);
// .attr('transform', 'translate(50,0)');
// Text Heading of DATE in chart
// this.svg.append('text')
// .attr('transform', 'translate(' + (-20) + ',' + (this.height + 13) + ')')
// .attr('dy', '.35em')
// .attr('class', ' xAxis')
// .text('Date');
// Append Circles in chart with different Colors
this.appendCircles();
const zoom = d3.zoom()
.scaleExtent([1, 1]) // This control how much you can unzoom (x0.5) and zoom (x20)
.on('zoom', () => {
console.log(d3.event);
this.xAxis.scale(this.xScale);
groupX.call(this.xAxis);
this.xScale.range(this.xScaleCopy.range().map((d) => {
return d3.event.transform.applyX(d);
}));
// Make a Path for Dataset
this.svg.select('.line').remove();
this.svg.selectAll('circle').remove();
this.svg.append('path')
.datum(this.dataset[0].fluencyData)
.attr('class', 'line')
.attr('d', this.line);
}
}
在缩放中做什么以限制平移 x 轴?输出图像链接:https ://ibb.co/NVDJCky
在 Y 轴上,您可以看到当我们在 X 轴上应用平移时,x 轴是重叠的。
解决方案
推荐阅读
- c# - C# 将 ListView 导出为 CSV
- c++ - 如何加载同一家族的多种字体
- mysql - 添加新行时列名与 SQL 函数冲突
- java - 在图像中加载后无法删除外部 JAR 文件
- reactjs - 如何在 thunk-action 中传递静态类型的参数?
- execution - Camunda 仅用于建模 BPMN 还是我可以执行 REST 服务之类的代码
- python - 在 Python 中更清晰地显示 JSON 数据
- r - 当我将 data.frame 转换为 CSV 时,R 中的数据类型会发生变化
- python - 使用安全元件芯片生成 CMS,了解生成 CMS 签名的方式(带有签名者信息和签名属性的签名数据)
- javascript - Javascript 对象 - 为什么它的行为如此 + 对象名称周围的大括号