jqplot饼图数据标签的格式精度不含尾随零 [英] jqplot Pie Chart data label format precision without trailing zeros

查看:209
本文介绍了jqplot饼图数据标签的格式精度不含尾随零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在饼图格式化数据标签,以显示最多2个十进制个字符,从不显示结尾零,例如:

  2  - > 2 
2.0 - > 2
2.566 - > 2.57

换句话说,我在问如何设置 dataLabelFormatString 参数
我知道它使用 sprintf ,因此我尝试了'%.2f'和'%g'。他们虽然没有解决我的问题。由于第一个数字使得每个数字都显示为十进制字符,另一个只是去除尾随零,但不能精确地处理这个数字,例如, '%.2g'是指字符的总长度。

我不知道如何有效地将这两者结合起来(甚至有可能吗?)。如何设置参数,例如,使用'%.2f',然后使用'%g'的第一个格式,它应该做的伎俩,但如何做到这一点?



<我可以'注入'一个if / else的格式,如果是这样,然后怎么样?

否则我能想到的唯一方法是,一旦绘制阴谋,运行一个jquery脚本获取标签,并应用 parseFloat(label.toFixed(2))

解决方案

要关闭这个话题,我在这里发布我的答案。我设法解决了这个问题,感谢 jQuery 。谈到做客户端时,它又一次成为了一种真正的救命。 :)



我已经找到了类似于我的问题中提到的想法,即使用 parseFloat(label.toFixed(2))。虽然我做了一个稍微倒过来的方式。我决定设置图表为 dataLabels 参数使用 label 。一旦图表完成绘制,我使用 $。jqplot.postDrawHooks.push(...)来绑定我的函数。我的功能修改图例标签显示名称,而不是我将它们设置为数据数组之前计算的百分比值。

这个方法完全按照我所希望的那样工作。 对于那些感兴趣的人,请在这里找到我的jsfiddle显示解决方案。



如果有人有更好的解决方案我非常希望随时将答案从我的答案中移走

I would like to know how to format the data labels on a Pie Chart so it shows max 2 decimal characters, and never shows trailing zeros, for example:

2 -> 2
2.0 -> 2
2.566 -> 2.57

In other words, I am asking how to set dataLabelFormatString parameter? I know it is using sprintf, thus I tried '%.2f' and '%g'. They do not solved my problem though. Since the first makes each number to show to decimal characters, the other just removes trailing zeros but doesn't work with precision as in this case the digits you place in front of it ,e.g. '%.2g' refer to the total length of the character.

I do not know how to, effectively, combine these two (is it even possible?). How to set the parameter, for example, to first format using '%.2f' then using '%g' and it should do the trick, but how to do it?

Can I maybe 'inject' an if/else into the format, if so then how?

Otherwise the only way I can think of would be to, once the plot is drawn, run a jquery script getting the labels and applying parseFloat(label.toFixed(2)) on each.

解决方案

To close this topic I am posting my answer here. I managed to solved this issue, thanks to jQuery. Once again it appears to be a real life saver when it comes to doing the client side. :)

I have approached it similarly to my thought mentioned in my question, i.e. using parseFloat(label.toFixed(2)). Though I did it in a slightly inverted way. I decided to set the chart to use label for dataLabels parameter. I use the $.jqplot.postDrawHooks.push(...) to bind my function for execution once the chart is finished painting. My function modifies the legend labels to display names instead of percentage values which I calculate before I set them to data array.

This approach works perfectly as I would want it. To those interested, please find my jsfiddle showing the solution here.

If someone has a better solution I am very keen to move the accept from my answer at any time.

这篇关于jqplot饼图数据标签的格式精度不含尾随零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆