如何在 Highcharts 中设置轴的最小上限?
我正在尝试设置一个最小上限,具体来说:
- Y 轴应从 0 开始
- Y 轴应至少为 10 或更高(自动缩放)
- Y 轴的上限不应小于 10。
似乎是 Highcharts 所做的事情,但我似乎不知道如何做。有人有这方面的经验吗?
I'm trying to set a minimum upper bound, specifically:
- The Y axis should start at 0
- The Y axis should go to at least 10, or higher (automatically scale)
- The upper bound for the Y axis should never be less than 10.
Seems like something Highcharts does, but I can't seem to figure out how. Anybody have experience with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Highcharts 似乎没有在图表创建时执行此操作的选项。但是,它们确实公开了一些方法来询问极值并更改极值,
getExtremes()
和setExtremes(Number min, Number max, [Boolean redraw], [Mixedanimation])< /code> 在他们的文档中找到。
因此,一个可能的解决方案(创建图表后):
yAxis[0]
引用第一个 y 轴,我假设在这种情况下您只有一个轴。 doc 解释了如何访问其他轴。这并不理想,因为图表必须重新绘制,这不太明显,但它仍然存在。希望 Highcharts 能够将此类功能内置到选项中。
Highcharts doesn't seem to have an option for doing this at chart creation time. However, they do expose a couple methods to interrogate the extremes and change the extremes,
getExtremes()
andsetExtremes(Number min, Number max, [Boolean redraw], [Mixed animation])
found in their documentation.So, a possible solution (after chart creation):
yAxis[0]
references the first y-axis, and I'm assuming that you only have one axis in this case. The doc explains how to access other axes.This isn't ideal, because the chart has to redraw which isn't too noticeable, but it's still there. Hopefully, Highcharts could get this sort of functionality built in to the options.
仅使用选项(无事件或函数)执行此操作的方法是:
此处
minRange
定义轴的最小跨度。maxPadding
默认为 0.01,这会使轴长度超过 10,因此我们将其设置为零。这会产生与 setExtreme 相同的结果。请参阅此 JSFiddle 演示。
A way to do this only using options (no events or functions) is:
Here
minRange
defines the minimum span of the axis.maxPadding
defaults to 0.01 which would make the axis longer than 10, so we set it to zero instead.This yields the same results as a
setExtreme
would give. See this JSFiddle demonstration.添加到 Julian D 的非常好的答案,如果您计算的最大值的位数与您想要的上限不同,以下内容可以避免潜在的重新定位问题。
就我而言,我的百分比数据当前进入 20 年代,但我想要的范围是 0 到至少 100,如果需要,有可能超过 100,因此以下设置 min & max ,然后如果 dataMax 结果更高,则重新分配 max 直至其值。这意味着计算图形定位时始终为 3 位值提供足够的空间,而不是计算 2 位数字,然后通过挤压“100”来破坏,但在出现下一个问题之前最多允许“999”。
Adding to Julian D's very good answer, the following avoids a potential re-positioning problem if your calculated max varies in number of digits to your desired upper bound.
In my case I had percentage data currently going into the 20's but I wanted a range of 0 to at least 100, with the possibility to go over 100 if required, so the following sets min & max in the code, then if dataMax turns out to be higher, reassigns max up to it's value. This means the graph positioning is always calculated with enough room for 3-digit values, rather than calculated for 2-digits then broken by squeezing "100" in, but allows up to "999" before there would next be a problem.
HigtCharts 有一个非常好的所有方法的文档和示例。
http://www.highcharts.com/ref/#yAxis--min
在你的情况下,我认为你应该“yAxis”的“min”和“max”属性。
如果您动态创建图表,您应该设置
min=0
max=10 ,如果您的所有数据值小于 10
,
只有 min=0 ,如果您的值大于 10
祝你好运。
HigtCharts has a really good documentation of all methods with examples.
http://www.highcharts.com/ref/#yAxis--min
In your case I think you should the "min" and "max" properties of "yAxis".
If you are creating your chart dynamically you should set
min=0
max=10 , if your all data values are less then 10
and
only min=0, if you have value greater then 10
Good luck.
尝试设置轴的最小值和轴单位中刻度线的间隔,如下所示:
另外不要忘记设置最大值。
希望有帮助。
Try setting the minimum value of the axis and the interval of the tick marks in axis units like so:
Also don't forget to set the max value.
Hope that helps.