Вопрос

I'm hoping this is a simple question for someone out there.

I would like to use rCharts to create a simple bar chart with a custom hover-over. I've found sample code I can use to annotate a point graph, but not a bar chart.

The point graph and bar graphs are built with different reference classes (PolyCharts vs. Morris), but I'm hoping syntax may be similar or someone can give suggestions on how to set the HoverCallback properties through rCharts. Below is a code snippet as an example.

require(rCharts)

sex = c("Male", "Female") 
ttl = c(4132,4399) 
pct = c(48.4, 51.6) 

dta = data.frame(sex,ttl,pct)

rp <- rPlot(pct ~ sex, data = dta, type = 'point', 
                  tooltip="function(item){return item.ttl +'\n' + item.pct}"
                  )
str(rp)
sp$guides(y = list(title = "", min=0,max = 100))
rp

# Bar Chart (Reference class = Morris)

mp <- mPlot(x = 'sex', y = c('ttl'), data = dta, type = "Bar",
            names.arg=c("Male","Female")
)

Any suggestions, for coding, or documentation I can review would be greatly appreciated.

Это было полезно?

Решение

Here is how you can modify tooltips with Morris. You can view the final chart with code here.

mp <- mPlot(ttl ~ sex, data = dta, type = "Bar")
mp$set(hoverCallback = "#! function(index, options, content){
  var row = options.data[index]
  return '<b>' + row.sex + '</b>' + '<br/>' +
     'ttl: ' + row.ttl + '<br/>' + 
     'pct: ' + row.pct
} !#")
mp

Although rCharts tries to provide a consistent interface across multiple viz libraries, it is usually limited to data and plot aesthetics. Customization is specific to each viz library, and is different across libraries. For MorrisJS, you can look up documentation directly on the Morris Website. Any of the options can be added to the chart using the set method.

The funny looking #!...!# line you see in the code above is a hack to allow javascript objects like functions to be directly passed to the HTML, since otherwise they will be converted to strings.

Hope this helps.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top