作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Highcharter 的极坐标图,想知道它是否能够根据标准更改颜色。我正在使用图表来查看预算与支出。当支出 < 预算时,我希望该列为绿色。当支出 > 预算时,我希望该列为红色。这可能吗?
对于代码中的示例:“物流”和“IT 与电信”支出将为绿色,所有其他支出类别将为红色。
答案需要是动态的,因为超出或低于计划的内容会不断变化。
下面是代码的简化版本。
library (shiny)
library (highcharter)
hc <- highchart() %>%
hc_chart(polar = TRUE) %>%
hc_title(text = "Budget vs Spending") %>%
hc_xAxis(categories = c("Energy", "Facilties", "IT & Telecom",
"Logistics", "Office Products", "Plant Consumables",
"Temp Labor", "Travel", "Other"),
tickmarkPlacement = "on",
lineWidth = 0) %>%
hc_yAxis(gridLineInterpolation = "polygon",
lineWidth = 0,
min = 0) %>%
hc_series(
list(
name = "Spend",
data = c(50000, 39000, 42000, 31000, 26000, 14000, 26000, 26000, 26000),
pointPlacement = "on",
type = "column"
),
list(
name = "Budget",
data = c(43000, 19000, 60000, 35000, 17000, 10000,10000,10000,10000),
pointPlacement = "on",
type = "line"
)
)
hc
最佳答案
您想将 colorByPoint
设置为 TRUE
,然后使用 ifelse
设置 colors
,如下所示:
library(shiny)
library(highcharter)
library(tidyverse)
hc <- highchart() %>%
hc_chart(polar = TRUE) %>%
hc_title(text = "Budget vs Spending") %>%
hc_xAxis(categories = c("Energy", "Facilties", "IT & Telecom",
"Logistics", "Office Products", "Plant Consumables",
"Temp Labor", "Travel", "Other"),
tickmarkPlacement = "on",
lineWidth = 0) %>%
hc_yAxis(gridLineInterpolation = "polygon",
lineWidth = 0,
min = 0) %>%
hc_series(
list(
name = "Spend",
data = c(50000, 39000, 42000, 31000, 26000, 14000, 26000, 26000, 26000),
pointPlacement = "on",
colorByPoint = TRUE,
type = "column",
colors = ifelse(c(50000, 39000, 42000, 31000, 26000, 14000, 26000, 26000, 26000) > c(43000, 19000, 60000, 35000, 17000, 10000,10000,10000,10000),"#F00","#0F0")
),
list(
name = "Budget",
data = c(43000, 19000, 60000, 35000, 17000, 10000,10000,10000,10000),
pointPlacement = "on",
type = "line"
)
)
hc
希望对你有帮助
关于R高宪章: Polar graph having conditional colors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47928278/
我是一名优秀的程序员,十分优秀!