如何访问 nvd3 中 d3 图的 x 和 y 尺度
How do I access the x- and y-scale of the d3 plot in nvd3?
我正在使用 nvd3 绘制一些系列,并希望在绘图中添加一些任意矩形。如何访问 d3 图的基础 x 和 y 刻度,以便将矩形的坐标转换为 svg 像素坐标,以便与现有数据处于同一比例:
function d3_render(response) {
nv.addGraph(function() {
var data = response;
chart.xAxis
.axisLabel('Time (s)')
.tickFormat(d3.format(',f'));
chart.x2Axis
.tickFormat(d3.format(',f'));
chart.yAxis
.axisLabel('Units normalized')
.tickFormat(d3.format(',.2f'));
chart.y2Axis
.tickFormat(d3.format(',.2f'));
d3.select('#chart svg')
.datum(data)
.transition().duration(1000)
.call(chart);
// Adding my rectangle here ...
d3.select('#chart svg')
.append('g')
.append('rect')
.attr('x', 50) // need to transform to same scale.
.attr('y', 50) // need to transform to same scale.
.attr('width', 100) // need to transform to same scale.
.attr('height', 100) // need to transform to same scale.
.attr('fill', 'red')
nv.utils.windowResize(chart.update);
return chart;
});
您可以通过轴访问刻度,即 chart.xAxis.scale()
等等。要将它们应用于您的坐标,您可以执行以下操作:
.attr("x", chart.xAxis.scale()(50));
相关文章:
- d3.js Chord图的动态工具提示
- D3.js剪辑路径截断了我的图的边缘
- 将d3中饼图的一部分隔开
- d3绘制动画折线图的基本示例
- 对d3堆叠条形图的数据进行排序
- 制作没有子元素的D3气泡图
- 具有任意多条线的D3折线图(以及特定的数据格式)
- 如何更新 d3 图的数据
- 面积图的条件填充 D3.js
- d3.js:垂直移动线's与双线图的交点
- D3.js强制布局:动态添加节点;t随图的其余部分移动
- 更新数据后保留D3图的位置
- 如何在D3中为用于显示饼图的数据添加键
- 带标签的 D3 圆环图
- 在浏览器中的 NVD3(或带有 CSS 的 D3)的屏幕截图中
- 具有平滑更新动画的 D3 折线图
- IE中实时图的D3转换不平滑
- 如何使用分区统计图的 d3.js 将数据从 .tsv 绑定到 SVG 的路径
- 尝试理解蜂巢图的 D3 代码
- 可排序热图的D3转换困难