javascript 格式化 jqPlot 点标签,如:数字(百分比)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10692796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 10:43:13  来源:igfitidea点击:

Format jqPlot point labels like: number (percent)

javascriptjqueryjquery-pluginsjqplot

提问by evsar3

this is my first question.

这是我的第一个问题。

I need to format a jqPlot chart point labels like this: 50 (100%) Formating number and show percentage.

我需要像这样格式化 jqPlot 图表点标签:50 (100%) 格式化数字并显示百分比。

var s1 = [32, 28, 18, 6];

var ticks = ['0-20 kph', '21-40 kph', '41-60 kph', '61+ kph'];

plot1 = $.jqplot('bar-graph', [s1], {
    animate: !$.jqplot.use_excanvas,
    title: 'Gráficos de velocidade',
    captureRightClick: true,
    seriesColors: ['green', 'yellow', 'orange', 'red'],
    seriesDefaults: {
        renderer: $.jqplot.BarRenderer,
        pointLabels: {
            show: true,
            formatString: '%s (?%%)' 
        },
        rendererOptions: {
            varyBarColor: true
        }
    },
    axes: {
        xaxis: {
            renderer: $.jqplot.CategoryAxisRenderer,
            ticks: ticks
        }
    },
    highlighter: {
        show: false
    }
});

On jsFinddle: http://jsfiddle.net/evandroprogram/r3PUE/10/

在 jsFnddle 上:http: //jsfiddle.net/evandroprogram/r3PUE/10/

Thanks.

谢谢。

回答by Satyajit

You can probably implement a function which returns a proper format string instead of setting the format explicitly. Something like this:

您可能可以实现一个返回正确格式字符串的函数,而不是显式设置格式。像这样的东西:

formatString: function(){return '%s (100%)';}()

You can do your calculations inside that function to come up with the appropriate string.

您可以在该函数内进行计算以得出适当的字符串。