“nice"的算法图表上的网格线间隔 [英] Algorithm for "nice" grid line intervals on a graph
问题描述
我需要一个相当聪明的算法来为图表(图表)提供漂亮"的网格线.
I need a reasonably smart algorithm to come up with "nice" grid lines for a graph (chart).
例如,假设一个条形图的值为 10、30、72 和 60.您知道:
For example, assume a bar chart with values of 10, 30, 72 and 60. You know:
最小值:10最大值:72范围:62
Min value: 10 Max value: 72 Range: 62
第一个问题是:你从什么开始?在这种情况下,0 将是直观的值,但这不会支持其他数据集,所以我猜:
The first question is: what do you start from? In this case, 0 would be the intuitive value but this won't hold up on other data sets so I'm guessing:
网格最小值应为 0 或低于范围内数据最小值的不错"值.或者,可以指定它.
Grid min value should be either 0 or a "nice" value lower than the min value of the data in range. Alternatively, it can be specified.
网格最大值应该是高于范围最大值的不错"值.或者,也可以指定它(例如,如果您要显示百分比,则可能需要 0 到 100,而不管实际值如何).
Grid max value should be a "nice" value above the max value in the range. Alternatively, it can be specified (eg you might want 0 to 100 if you're showing percentages, irrespective of the actual values).
应该指定范围内的网格线(刻度线)的数量或给定范围内的数字(例如 3-8),以便这些值是不错的"(即整数),并且您可以最大限度地利用图表区域.在我们的示例中,80 是一个合理的最大值,因为它会使用 90% 的图表高度 (72/80),而 100 会造成更多空间浪费.
The number of grid lines (ticks) in the range should be either specified or a number within a given range (eg 3-8) such that the values are "nice" (ie round numbers) and you maximise use of the chart area. In our example, 80 would be a sensible max as that would use 90% of the chart height (72/80) whereas 100 would create more wasted space.
有人知道一个好的算法吗?语言无关紧要,因为我会根据需要实现它.
Anyone know of a good algorithm for this? Language is irrelevant as I'll implement it in what I need to.
推荐答案
CPAN 提供了一个实现 这里(见源链接)
CPAN provides an implementation here (see source link)
另见图形轴的刻度算法
仅供参考,使用您的示例数据:
FYI, with your sample data:
- 枫木:最小=8,最大=74,标签=10,20,..,60,70,刻度=10,12,14,..70,72
- MATLAB:最小值=10,最大值=80,标签=10,20,,..,60,80
这篇关于“nice"的算法图表上的网格线间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!