javascript - 如何将像素转换为 d3 中的转换值?

标签 javascript jquery d3.js

我有一个 g 元素,我试图通过将圆的 cx 和 cy 位置赋予 g 元素来将其放在我的圆元素的顶部。但是,这不起作用,因为 g 使用翻译。知道如何将像素转换为翻译吗?

colorPickerX = d3.select(dataPointId).attr("cx");
colorPickerY = d3.select(dataPointId).attr("cy");
var svg = d3.select("body")
    .append("svg")
    .attr("width", 300)
    .attr("height", 300)
    .append("g")
    .attr("transform", "translate("+colorPickerX+","+colorPickerX+")"); \\This is wrong because Cx and Cy are Pixels

最佳答案

使用 parseFloat 执行此操作:

//parseFloat will get the number and ignore the px in the end.
colorPickerX = parseFloat(d3.select(dataPointId).attr("cx"));
colorPickerY = parseFloat(d3.select(dataPointId).attr("cy"));

然后像上面那样使用它来翻译 g 组。

关于javascript - 如何将像素转换为 d3 中的转换值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37492158/

相关文章:

javascript - 调试哪个 Javascript 在单击某个元素时运行

JavaScript;用新行分割字符串

javascript - 使用 Grunt 时如何检测不同文件夹中的相同文件名?

javascript - JQuery中 "and '个字符的区别

javascript - d3.js 将底部轴线延伸至原点

javascript - Chrome 与 webworkers 和 createImageBitmap 崩溃

javascript - 使用 jquery toggle 获取可折叠面板的问题

javascript - 使用格式化数据渲染,使用 DataTables.net 中的原始数据排序

javascript - d3 轴在 pow 刻度下表现异常

css - 在 DIV 中发布 d3 js 树形图样式