Chart JS条形图中的Y轴限制?

时间:2015-05-21 11:25:45

标签: bar-chart chart.js

图表js条形图的Y轴限制是什么?

我使用了17位数字,并且在该图表中没有显示任何内容。

1 个答案:

答案 0 :(得分:0)

y轴没有限制(除了数字类型的javascript限制外)

但是,你可能看到的可能是9007199254740992以上的精度损失 - 这不是巧合的17位数(见What is JavaScript's highest integer value that a Number can go to without losing precision?及相关问题

Chart.js使用值范围来确定如何缩放条形图。它通过从最大值减去min并使用差异来实现。

现在,如果所有值都相同,Chart.js会添加一个小数字(0.5)来产生差异,然后继续这种差异。

如果添加0.5实际产生差异,那么一切都很好 - 但是如果结果高于9007199254740992,它将大部分被精度限制吞噬,产生0差异并弄乱Chart.js' sy轴计算

当数字高于9007199254740992并且它们的差异不够大时会发生同样的问题(它们基本上对同一个数字进行四舍五入 - >它们被视为一组相等的值 - > 0.5被添加 - >它大多数都被精确限制吞没了)

这里有一个快速的价值示例以及为什么它会起作用/不起作用

// works because there is a (small) difference and the numbers are < 9007199254740992
var a = [9007199254740991, 9007199254740992];

// won't work because there is no difference and the numbers are > 9007199254740992 - 0.5
var b = [9007199254740992, 9007199254740992];

// won't work because there is no difference and the numbers are = 9007199254740992 - 0.5
var c = [9007199254740991.5, 9007199254740991.5];

// works because there is no difference and the numbers are < 9007199254740992 - 0.5
var d = [9007199254740991.4, 9007199254740991.4];

// works because there is a significant difference even though the numbers are > 9007199254740992
var e = [12345678901234567890, 12345678901434567891];

// works because there is a significant difference even though the numbers are > 9007199254740992
var f = [0, 12345678901434567891];

// won't work because there is only a small difference and the numbers are > 9007199254740992
var g = [9007199254740992, 9007199254740993];

简而言之,您的条形图不会渲染,因为您有一组相同(或没有显着差异)且值为9007199254740992 - 0.5或更高的值。

你可以添加一个虚拟值0来强制它渲染(或者可能添加另一个虚拟系列的0值),确保存在显着差异 - 或者你的所有值是否都在17位数范围和彼此非常接近,只是绘制偏移量,即说你有1701和1705 ...图1和5并在工具提示中输入170作为值前缀。