styleColorBar Center and shift Left/Right dependent on Sign

我怕爱的太早我们不能终老 提交于 2019-11-30 00:45:32

You could make a custom styleColorBar function that uses the CSS gradients (same as the original styleColorBar) to make the kind of bars you want.

Here is an example (sorry for the long line, adding new lines breaks the CSS):

color_from_middle <- function (data, color1,color2) 
{
  max_val=max(abs(data))
  JS(sprintf("isNaN(parseFloat(value)) || value < 0 ? 'linear-gradient(90deg, transparent, transparent ' + (50 + value/%s * 50) + '%%, %s ' + (50 + value/%s * 50) + '%%,%s  50%%,transparent 50%%)': 'linear-gradient(90deg, transparent, transparent 50%%, %s 50%%, %s ' + (50 + value/%s * 50) + '%%, transparent ' + (50 + value/%s * 50) + '%%)'",
             max_val,color1,max_val,color1,color2,color2,max_val,max_val))
} 

Using some test data:

data <- data.frame(a=c(rep("a",9)),value=c(-4,-3,-2,-1,0,1,2,3,4))

datatable(data) %>%
  formatStyle('value',
              background=color_from_middle(data$value,'red','blue'))

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!