Comma Separated Numbers For an Axis in Flot

梦想的初衷 提交于 2019-12-05 15:32:18

问题


Is there a way to have Flot make axis numbers be comma-separated?

So for example, instead of 1000000 have 1,000,000


回答1:


You can do that by using the tickFormatter property of the axis.

xaxis: {
  tickFormatter: function(val, axis) {
    // insert comma logic here and return the string
  }
}

Source: http://people.iola.dk/olau/flot/API.txt (under "Customizing the axes")

This page has a function to format numbers with commas: http://www.mredkj.com/javascript/nfbasic.html

For Example on the yaxis:

    $(function () {  
            var plotarea = $("#plotarea");
            $.plot( plotarea , [data], {
                            xaxis: {mode: "time", timeformat: "%m/%d %H:%M:%S"},
                            yaxis: {tickFormatter: function numberWithCommas(x) {
                                      return x.toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ",");
                                }
                            },
                            points: { show: true },
                            lines: { show: true, fill: true }

                    } );
    });


来源:https://stackoverflow.com/questions/5968212/comma-separated-numbers-for-an-axis-in-flot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!