datastorm-open / rAmCharts

API for Amcharts
48 stars 17 forks source link

[Question] How to manipulate the data in JS portion of setLegend #25

Closed happyshows closed 8 years ago

happyshows commented 8 years ago

Hi,

I have the below settings for legend. However because the value is too big, I'd like to divide it by 1,000 or 1,000,000 and round it. I have tried [[ Math.round(value.sum/1000)]] but it didn't work. Also, is it possible to do if/else clause inside the [[ ]]? Thanks.

setLegend(position = 'bottom' ,useGraphSettings = TRUE, periodValueText = ' {[[value.sum]]}', valueWidth = 80) %>%

bthieurmel commented 8 years ago

You can use valueFunction for this. Here is an example :


xc <- paste("cat.", 1:100)
y <- rnorm(length(xc))

# Change type
amPlot(x = xc, y = y, type = 'l', zoom = TRUE) %>%
  setLegend(position = 'bottom' ,useGraphSettings = TRUE, 
            periodValueText = '[[value]]',valueWidth = 80, 
            valueFunction = htmlwidgets::JS("function(item) {
                                            if(item.values !== undefined){
                                            var value = item.values.value * 100;
                                             return value.toFixed(2);
                                            } else {
                                             return '';
                                            }}"))

item seems to depend of type of graphics. You can use a console.info first to see structure in a browser.... :

amPlot(x = xc, y = y, type = 'l', zoom = TRUE) %>%
  setLegend(position = 'bottom' ,useGraphSettings = TRUE, 
            periodValueText = '[[value]]',valueWidth = 80, 
            valueFunction = htmlwidgets::JS("function(item) {
                                            console.info(item)
                                            }"))
happyshows commented 8 years ago

This solution works, thanks.