简体   繁体   English

使用SVG变换缩放y维时,d3.svg.line笔划会倾斜

[英]d3.svg.line stroke is skewed when scaling y-dimension with SVG transform

Using d3's d3.svg.line produces a line which looks after magnifying only in the y-dimension like this one 使用d3的d3.svg.line会生成一条在像这样的y维度上放大后才能看的线 线段

Here is the corresponding path element 这是对应的路径元素

<path d="M 70.13775,
    303.1818181818182 L 73.03775,
    285.9090909090909 L 75.93775,
    402.5 L 78.83775,
    402.5 L 81.73775,
    419.77272727272725 L 84.63775,
    342.0454545454545 L 87.53775"></path>

As one can see, when the pen is moved to the right, the width of the corresponding line segment is a different one compared to the width of the line of the path segment, when the pen is moved vertically. 可以看到,当笔向右移动时,当笔垂直移动时,对应的线段的宽度与路径段的线的宽度不同。

Question : Is it possible to draw the line in a such way that the width of line is the same everywhere? 问题 :是否可以以在任何地方都相同的线宽来绘制线? As if one draws the line with holding a pen with a round brush perpendicular to the drawing area. 就像用垂直于绘图区域的圆形笔刷握住笔画线一样。

If you want to scale your line along one axis, instead of using an svg transform you would probably be better off using a d3.scale to scale your incoming data when you make your d3.svg.line() . 如果要沿一个轴缩放线,而不是使用svg变换,则在制作d3.svg.line()时,最好使用d3.scale来缩放传入的数据。

Lets say you have a d3 scale called myScale 假设您有一个名为myScale的d3比例尺

Then when you make your d3 svg line, you can use the scale to modify one of the coordinates: 然后,在制作d3 svg线时,可以使用比例尺来修改其中一个坐标:

var lineGen = d3.svg.line()
  .x(function(d) {return d.x;})
  .y(function(d) {return myScale(d.y);});

The solution of using vector-effect will probably give you the result you want in bleeding edge browsers, but it seems like overkill to use an effect to do something this simple, and the lack of browser support for SVG1.2 features might become problematic for you. 使用vector-effect的解决方案可能会在先进的浏览器中为您提供所需的结果,但是使用效果来完成这种简单的操作似乎已经过头了,并且缺少浏览器对SVG1.2功能的支持可能会成为问题。您。 If you were trying to fix a static svg file that would probably be the way to go, but since you are generating it with d3, you might as well use the built-in methods for scaling. 如果您试图修复一个静态的svg文件,那么可能会这样做,但是由于您是使用d3生成的,因此最好使用内置方法进行缩放。

EDIT 编辑

For a simpler solution, you could even scale it by a constant factor: 对于一个更简单的解决方案,您甚至可以将其缩放一个常数:

var scaleFactor = 1.75;
var lineGen = d3.svg.line()
  .x(function(d) {return d.x;})
  .y(function(d) {return d.y * scaleFactor;});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM