highcharter
highcharter copied to clipboard
highcharter hc_axis change color of one x-axis label only
Hi, This my dataset
df <- data.frame(dose=c("D0.5", "D1", "D2"),
len=c(4.2, 10, 29.5))
I want to change the color of just one label (the label "D1") in red (not all the labels). I saw this documentation : https://api.highcharts.com/highcharts/xAxis.labels.formatter and I tried to do the same thing but nothing has changed
How do I apply style to individual labels to get only the label "D1" in red ? This is what I did :
df <- data.frame(dose=c("D0.5", "D1", "D2"),
len=c(4.2, 10, 29.5))
highchart() %>%
hc_add_series(
data = df$len,
type = 'line'
) %>%
hc_xAxis(categories = df$dose,
labels = list(
formatter = JS(
"
function() {
if (this.value =='D1' ) {
return '<span style='fill: red;'>' + this.value + '</span>';
}
return this.value;
}
"
)
))
Some help would be appreciated
You're close but you need to use double quotes and escape them for the style attribute. Also I think 'color' is more appropriate when stylizing text.
highchart() %>%
hc_add_series(
data = df$len,
type = 'line'
) %>%
hc_xAxis(categories = df$dose,
labels = list(
# useHTML = T,
formatter = JS(
"function() {
if (this.value =='D1' ) {
console.log(this.value)
return '<span style=\"color: red;\">' + this.value + '</span>';
}
return this.value;
}"
)
))