绘制 x 轴点的更有效方法?

发布于 2024-08-03 18:05:24 字数 874 浏览 10 评论 0原文

我一直在从事一个需要用价格结果填充条形图的项目。该图表显示给定价格范围内的商品数量。例如,如果在亚马逊上有 9 个商品的价格范围为 0-10 美元,则 x 轴将显示 0-10 美元,y 轴将填充值 9。

我的条形图有 8 个条形,全部带有类似的价格范围。 $0-$10、$10-$20、$20-$30...等等。

我的问题是:定义这些单独点的最佳方式是什么?这些商品之间没有共同的价格范围,因此x 轴不能是静态数字。它们必须在结果范围内动态计算。

因此,目前我正在按如下方式创建 x 轴点:

我采用最低结果: @numbers[0] = results[0];

我取最高的结果: @numbers[8] = results[-1];

然后我找到两者的中位数: @数字[4] = (@数字[0]+@数字[8])/2;

然后我再重复该过程 6 次

@numbers[2] = (@numbers[0]+@numbers[4])/2; @数字[6] = (@数字[4]+@数字[8])/2; @数字[1] = (@数字[0]+@数字[2])/2; @数字[3] = (@数字[2]+@数字[4])/2; @数字[5] = (@数字[4]+@数字[6])/2; @数字[7] = (@数字[6]+@数字[8])/2;

这给了我需要的结果,但它看起来非常重复,我想有更好的方法。

我尝试创建一个循环,但无法以更简洁的方式编写它。

有没有更快的方法来做到这一点,或者也许有更多类似于 DRY 的方法?

I have been working on a project that requires a bar graph to be populated with price results. The chart displays the number of items within a given price range. For instance, if on amazon there are 9 items within the price range of $0-$10 the x-axis would display $0-$10 and the y-axis would be populated with a value of 9.

My bar graph has 8 bars, all with similar price ranges. $0-$10, $10-$20, $20-$30... etc.

My question is this: What is the best way to define those individual points? There is no common price range between these items, so the x-axis cannot be static numbers. They must be dynamically calculated within the range of results.

As such, currently I am creating the x-axis points as follows:

I take the lowest result:
@numbers[0] = results[0];

And I take the highest result:
@numbers[8] = results[-1];

Then I find the median of the two:
@numbers[4] = (@numbers[0]+@numbers[8])/2;

I then repeat the process 6 more times

@numbers[2] = (@numbers[0]+@numbers[4])/2; @numbers[6] = (@numbers[4]+@numbers[8])/2; @numbers[1] = (@numbers[0]+@numbers[2])/2; @numbers[3] = (@numbers[2]+@numbers[4])/2; @numbers[5] = (@numbers[4]+@numbers[6])/2; @numbers[7] = (@numbers[6]+@numbers[8])/2;

This gives me the results I need, but it seems awfully repetitive and I would imagine there is a better way.

I tried creating a loop, but I could not write it in a less verbose manner.

Is there a quicker way to do this, or perhaps something more along the lines of DRY?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷你仙 2024-08-10 18:05:24

您的垃圾箱大小始终相同吗?在您的示例中,所有共享范围=10。如果是这样,那么你可以这样做:

binspacing = overall range / (numberofbins-1);

bin n 的位置将是numbers[0] 的 x 轴位置加上 n 乘以 binspacing。

Are your bins always of equal size? In your example, all share range=10. If so, then you could do:

binspacing = overall range / (numberofbins-1);

and the position of bin n would be the x-axis position of numbers[0] plus n times the binspacing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文