问题
I have a simple example using tooltips with rCharts that doesn't seem to work:
set.seed(1)
test <- data.frame(x = rnorm(100), y = rnorm(100))
rPlot(y ~ x, data = test,
type = 'point',
tooltip = "function(item){return item.x + '\n' + item.name + '\n' + item.y}")
An empty page comes up. The plot is there if I remove the tooltip option. I'm using rCharts_0.4.1, R Under development on x86_64-apple-darwin10.8.0 (64-bit) and version 31.0.1650.63 of Chrome.
Bonus question! Can tooltips contain variables in the data set but not used in x
, y
, etc? I have a large data set and I'd like to annotate the data points with an ID variable that has a unique value per row.
Thanks,
Max
回答1:
Rcharts 0.4.2
I can't point out where I've seen this before, but the best I can offer is that the current instruction is to wrap your js function like so:
"#!function(item) { return item.x }!#"
set.seed(1)
test <- data.frame(x = rnorm(100), y = rnorm(100), id = 1:100)
p <- rPlot(y ~ x, data = test,
type = 'point',
tooltip = "#!function(item){ return 'x: ' + item.x +
' y: ' + item.y + ' id: ' + item.id }!#")
I could never get the tips on different lines. You can see the problem by typing p$show('inline')
and including line breaks where you want them when creating the tooltip. Mustache is converting the linebreaks, which means they disappear in the resulting JS function and cause the tooltip function to span several lines - causing an error. I've tried to escape the newline character and append each string containing a newline character with .replace('\\n', '\n')
, but that obviously results in the same problem.
For the time being, your best bet is to make the plot as shown here, type p$save('filepath.html')
and manually add the line breaks. Otherwise, the question is how to pass a literal newline character to mustache.
回答2:
Your code works/worked using rCharts
0.3.51 (Chrome 31.0.1650.63 m, Win7 x64):
set.seed(1)
require(rCharts)
test <- data.frame(x = rnorm(100), y = rnorm(100),
name=replicate(100, paste(sample(letters, 5, rep=T), collapse="")))
rPlot(y ~ x, data = test,
type = 'point',
tooltip = "function(item){return item.x + '\n' + item.name + '\n' + item.y}")
You should be able to reference all columns in test
- as I did with name
.
来源:https://stackoverflow.com/questions/20929183/rplot-tooltip-problems