如何设置缩放时的顶点(截距)?

huangapple go评论54阅读模式
英文:

How do i set the apex (intercept) when scaling

问题

this.y = d3
	.scalePow()
	.exponent(2)
	.domain([2000, 10000])
	.range([600, 0]);

这段代码的作用是将2000映射到600,将10000映射到0,而介于两者之间的值会按照某个二次方程进行缩放,该二次方程的顶点位于2000。现在问题很简单,如何将顶点设置为例如6000?我想在6000处有大间隔的详细值,而在外边缘有窄间隔的步进值。

我添加了一张我用手绘制的图片以进行说明。我可能会稍后添加一张更好的图片如何设置缩放时的顶点(截距)?

我不确定"parabola apex"是否是正确的词汇,德语中它被称为"Scheitelpunkt"。

英文:

Lets assume this is the d3 code

this.y = d3
	.scalePow()
	.exponent(2)
	.domain([2000, 10000])
	.range([600, 0]);

What it does is mapping 2000 to 600, 10000 to 0, and everything inbetween is scaled by some quadratic formula which has its apex at 2000. Now the question is easy, how to i set the apex to say 6000? i want detailed values (big gaps) in the 6000 and narrow steps in the outer rim.

I added a picture i drew with my fingers for clarificarion. I might add a better pic later如何设置缩放时的顶点(截距)?

I am not sure if parabola apex is the right word, in german its called Scheitelpunkt

答案1

得分: 0

简单地,将.exponent(2)更改为.exponent(1/2)

如何设置缩放时的顶点(截距)?

这与x^2sqrt(x)是反函数有关。

如何设置缩放时的顶点(截距)?

英文:

Simply, change the .exponent(2) to .exponent(1/2):

如何设置缩放时的顶点(截距)?

This has everything to do with the fact that x^2 and sqrt(x) are inverse functions.

如何设置缩放时的顶点(截距)?

答案2

得分: 0

可以使用Symlog比例尺来实现您想要的效果,它提供了双对数变换。

为了使其工作,您所称之为apex(即6000)将成为比例尺上的零点,域将为[-4000, 4000](因为2000 = 6000 - 4000,而10000 = 6000 + 4000)。

此外,请不要忘记格式化刻度线,添加6000,当然也要格式化传递给比例尺的值。

这是演示:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
const svg = d3.select("svg")
  .attr("width", 600);

const scale = d3.scaleSymlog()
  .constant(500)
  .range([20, 580])
  .domain([-4000, 4000]);

const axis = d3.axisBottom(scale).tickFormat(d => d + 6000);

svg.append("g").attr("transform", `translate(0,)`).call(axis)

<!-- language: lang-html -->
<script src="https://d3js.org/d3.v7.min.js"></script>
<svg></svg>

<!-- end snippet -->
英文:

What you want can be done using a Symlog scale, which provides a bi-symmetric log transformation.

For that to work, what you call the apex (that is, 6000) will be the zero in the scale, and the domain will be [-4000, 4000] (since 2000 = 6000 - 4000, and 10000 = 6000 + 4000).

Also, don't forget to format the ticks, adding 6000, and obviously the values you pass to the scale as well.

Here's the demo:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const svg = d3.select(&quot;svg&quot;)
  .attr(&quot;width&quot;, 600);

const scale = d3.scaleSymlog()
  .constant(500)
  .range([20, 580])
  .domain([-4000, 4000]);

const axis = d3.axisBottom(scale).tickFormat(d =&gt; d + 6000);

svg.append(&quot;g&quot;).attr(&quot;transform&quot;, `translate(0,)`).call(axis)

<!-- language: lang-html -->

&lt;script src=&quot;https://d3js.org/d3.v7.min.js&quot;&gt;&lt;/script&gt;
&lt;svg&gt;&lt;/svg&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月22日 21:40:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76306801.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定