reCharts饼图 - 自定义工具提示

时间:2014-12-11 02:16:00

标签: r nvd3.js rcharts

我正在尝试为rCharts nvd3饼图自定义工具提示,但是当我使用下面的代码时,图表不会呈现。我想知道调整nvd3饼图的工具提示是否与其他nvd3图表的工作方式不同?任何见解将不胜感激!

这是我运行的代码:

pie.sum$CATEGORY = c('Cat1','Cat2','Cat3','Cat4')
pie.sum$VALUE = c(124,55,275,20)
pie.sum$PERCENT = round((pie.sum$VALUE/sum(pie.sum$VALUE)) * 100,2)

n3 = nPlot(x = "CATEGORY", y = "VALUE", data = pie.sum, type = "pieChart")
n3$chart(tooltipYContent = NA, tooltipYContent = NA)
n3$chart(tooltipContent = "#! function(key, x, y, e){ 
return 'Category: ' + x +
'
Value' + y +
'
% of value: ' + e.point.PERCENT
} !#")
n3$set(width = 800, height = 500) # mk changed width to 800 and height to 500
n3

1 个答案:

答案 0 :(得分:1)

你可以这样做:

pie.sum = data.frame(CATEGORY = c('Cat1','Cat2','Cat3','Cat4'),
                     VALUE = c(124,55,275,20))
pie.sum$PERCENT = round((pie.sum$VALUE/sum(pie.sum$VALUE)) * 100,2)

library(rCharts)
n3 = nPlot(x = "CATEGORY", y = "VALUE", data = pie.sum, type = "pieChart")
n3$chart(tooltipContent = "#! function(key, y, e, graph){return '<h3>' + 'Category: ' + key + '</h3>' + '<p>'+ 'Value ' + y + '<br>' + ' % of value: ' + e.point.PERCENT} !#")
n3$set(width = 800, height = 500) # mk changed width to 800 and height to 500
n3