在 Protovis 中绘制负数的正确方法是什么?
对于最简单的用例,即值范围为 -10 到 10 的条形图,如何使用 Protovis JavaScript 图表库干净地对其进行编码?
我所说的“干净”是指将轴居中,显示 x 和 y 轴标签,并表示图表的列值,其中负值低于 y 轴,正值超过 y 轴。
For the simplest use case, a bar chart with values ranging from -10 to 10, how does one go about coding this cleanly using the Protovis JavaScript charting library?
By cleanly I mean centering the axis, showing x and y axis labels, and representing the column values of the chart where negative values fall below the y axis and positive values exceed the y axis.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个工作示例: http://jsfiddle.net/nrabinowitz/yk5By/3/
其中重要部分如下:
制作一个从最小值到最大值的 x 轴刻度(在您的情况下,它将是
pv.Scale.linear(-10,10).range(0,w)
; 在我的示例中,我根据数据计算最小值和最大值。条形宽度以数据距 0 的绝对距离为基础:
然后根据基准是否为正或来调整
.left()
属性负面:因为我们使用简单的 x 轴刻度,所以添加轴标签非常简单:
Here's a working example: http://jsfiddle.net/nrabinowitz/yk5By/3/
The important parts of this are as follows:
Make an x-axis scale going from your min value to your max value (in your case, it would be
pv.Scale.linear(-10,10).range(0,w)
; in my example, I calculate min and max based on the data).Base the width of the bar on the absolute distance of the datum from 0:
Then adjust the
.left()
property based on whether the datum is positive or negative:Because we're using a simple x-axis scale, the adding axis labels is super-easy: