我目前正在使用R中的Java脚本包装器highcharter。 我想手动设置每个图层的Y轴以及每个图层的标题,但还没有找到一种方法。
例如,所有图层的标题当前均为“基本细分”,我想针对每个细分进行更新。就像我想手动设置y轴一样。
先谢谢了。
下面的当前代码。
df <- data_frame(
name = c("Animals", "Fruits", "Cars"),
y = c(5, 2, 4),
drilldown = tolower(name)
)
df
hc <- highchart() %>%
hc_chart(type = "column") %>%
hc_title(text = "Basic drilldown") %>%
hc_xAxis(type = "category") %>%
hc_legend(enabled = FALSE) %>%
hc_plotOptions(
series = list(
boderWidth = 0,
dataLabels = list(enabled = TRUE)
)
) %>%
hc_add_series(
data = df,
name = "Things",
colorByPoint = TRUE
)
dfan <- data_frame(
name = c("Cats", "Dogs", "Cows", "Sheep", "Pigs"),
value = c(4, 3, 1, 2, 1)
)
dffru <- data_frame(
name = c("Apple", "Organes"),
value = c(4, 2)
)
dfcar <- data_frame(
name = c("Toyota", "Opel", "Volkswagen"),
value = c(4, 2, 2)
)
hc <- hc %>%
hc_drilldown(
allowPointDrilldown = TRUE,
series = list(
list(
id = "animals",
data = list_parse2(dfan)
),
list(
id = "fruits",
data = list_parse2(dffru)
),
list(
id = "cars",
data = list_parse2(dfcar)
)
)
)
hc
EDIT *更新了答案,以动态设置R高图的yaxis。
drilldown = JS('function(e) {
console.log(e.seriesOptions);
this.setTitle({text: e.seriesOptions.name || e.seriesOptions.id });
this.yAxis[0].update({ min: this.yAxis[0].getExtremes().max * 0.5 })}')
答案 0 :(得分:0)
首先,您需要重构代码,因为它是不正确的。例如,尝试使用所有系列名称创建新变量,并将此名称列表分配给drilldown
中的data.frame
字段:
names <- c("Animals", "Fruits", "Cars")
df <- data.frame(
name = names,
y = c(5, 2, 4),
drilldown = names
)
然后,更改下钻对象定义中的下钻id
,因为没有必要使它们从小写字母开始:
hc_drilldown(
allowPointDrilldown = TRUE,
series = list(
list(
id = "Animals",
data = list_parse2(dfan)
),
list(
id = "Fruits",
data = list_parse2(dffru)
),
list(
id = "Cars",
data = list_parse2(dfcar)
)
)
)
最后一步是定义chart.events.drilldown
和chart.events.drillup
函数处理程序,您将在其中使用chart.title.text
函数设置Chart.update()
。为了对其进行定义,您必须使用JS()
R内置函数,如下所示:
hc_chart(type = "column", events = list(
load = JS("function() {console.log(this)}"),
drilldown = JS("function(e) {this.update({title: {text: e.seriesOptions.id}})}"),
drillup = JS("function() {this.update({title: {text: 'Basic drilldown' }})}")
)) %>%
实际上,我不太理解问题的这一部分:
就像我想手动设置y轴一样。
如果您能更精确地描述它,那么我将扩展答案。