1

I have below Dygraph using the dygraphs package

library(dygraphs)
library(htmlwidgets)
library(zoo)

valueFormatter = "function formatValue(v) {
                                            var suffixes = ['', 'K', 'M', 'G', 'T'];
                                            if (v < 1000) return v;
                                            var magnitude = Math.ceil(String(Math.floor(v)).length / 4-1);
                                            if (magnitude > suffixes.length - 1)
                                            magnitude = suffixes.length - 1;
                                            return String(Math.round(v / Math.pow(10, magnitude * 3), 2)) +suffixes[magnitude]
                                        }" 
Data = zoo(matrix(c(10000, 1000000, 100000000, 50000), nc = 1), as.Date(c('2015-01-05', '2016-01-05', '2017-01-05', '2018-01-05'))); colnames(Data) = 'x'
dygraph(Data, main = "") %>% dySeries(c("x")) %>% 
                            dyAxis("y", axisLabelFormatter = JS(valueFormatter), 
                                        valueFormatter     = JS(valueFormatter),
                                        rangePad = 20)

However in the label of Y-axis I want to bring Thousand seperator for the tick values e.g. instead of 30000K I want to have 3,0000K. Is there any way to achieve this.

Bogaso
  • 2,838
  • 3
  • 24
  • 54

1 Answers1

1

Convert your number 30000K into thousand separated value 30,000K.

Regex to place comma after each 3rd digit

var regex = /\B(?=(\d{3})+(?!\d))/g;
var number = "200";
var thousandSeprator = "20000";
console.log(number.replace(regex,',')); // 200
console.log(thousandSeprator.replace(regex,',')); // 20,000

Updated valueFormatter function

valueFormatter = "function formatValue(v) {
                                            var suffixes = ['', 'K', 'M', 'G', 'T'];
                                            var regex = /\B(?=(\d{3})+(?!\d))/g; 
                                            if (v < 1000) return v.toString().replace(regex,',');
                                            var magnitude = Math.ceil(String(Math.floor(v)).length / 4-1);
                                            if (magnitude > suffixes.length - 1)
                                            magnitude = suffixes.length - 1;        
                                            return v.toString().replace(regex,',') +suffixes[magnitude]
                                        }"
front_end_dev
  • 1,998
  • 1
  • 9
  • 14
  • Thanks. But when I put this phrase "/\B(?=(\d{3})+(?!\d))/g" in R console, I get error Error: '\B' is an unrecognized escape in character string starting ""/\B". I am using iOS – Bogaso Nov 10 '18 at 09:17
  • @Bogaso double backword slash works as mention by `jay.sf`. Reference link - https://rstudio-pubs-static.s3.amazonaws.com/74603_76cd14d5983f47408fdf0b323550b846.html – front_end_dev Nov 10 '18 at 09:25
  • I slightly modified as "/\\B(?=(\\d{3})+(?!\\d))/g". However with this valueFormatter, I lost Thousand (K) suffix – Bogaso Nov 10 '18 at 09:26
  • Try to log this value `suffixes[magnitude]` to get a better idea what going wrong – front_end_dev Nov 10 '18 at 09:28