简体   繁体   中英

jqplot pointLabels how to format number up to 2 decimal

I am using jqplot for showing graph.

following is the code :

tempArr1 = [9189.539999999999, 10170.039999999999, 980.5]

plot2 = $.jqplot('bar_chart_id1', [tempArr1], {
                            title:{
                                text: chatTitle2,
                                textColor: '#B66B09'
                            },
                            seriesDefaults:{
                                renderer:$.jqplot.BarRenderer,
                                rendererOptions: {
                                    // Set the varyBarColor option to true to use different colors for each bar.
                                    varyBarColor: true,
                                    barPadding: 2,
                                    barMargin: 3,
                                    barWidth: 30,
                                },
                                pointLabels: {show: true},  
                            },
                            axes: {
                                xaxis: {
                                    renderer: $.jqplot.CategoryAxisRenderer,
                                    ticks: lblArray,
                                    label:'Manufacturer',
                                },
                                yaxis:{
                                    labelRenderer: $.jqplot.CanvasAxisLabelRenderer,
                                    min: 0,
                                    tickInterval:1000,
                                    label:'Amount',
                                    tickOptions: {formatString: '$%#.2f'} 
                                }
                            },
                            highlighter: { show: true },
                        });

在此处输入图片说明 As you can see the point label is showing many decimal points (9189.539999999999). I need to show the label as '$91890.54'. I have tried the formatString option in pointLabels as '$%#.2' buts its not working.

Any help is greatly appreciated. Thank you.

tempArr1 = [9189.539999999999, 10170.039999999999, 980.5];
tempArr1 = tempArr1.map(i => { return parseFloat(i.toFixed(2)); });

四舍五入到JavaScript中的小数点后2位

Math.round(num * 100) / 100

Source

 pointLabels: { show: true, location: 'n', lables: tempArr1 , formatString: "$%#.2f" },

应该满足您的确切需求

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM