【发布时间】:2015-03-10 01:46:25
【问题描述】:
带有 rCharts 的 Highcharts 3D
rCharts 可以与 Highcharts 的 3D 模块一起使用吗?以下是 Highcharts 3D 的示例:
错误
下面的示例代码出现的错误是:
Error in envRefInferField(x, what, getClass(class(x)), selfEnv) : ‘zAxis’ is not a valid field or method name for reference class “Highcharts”
示例代码
这是我正在使用的示例代码:
library(rCharts)
# Based on example from: http://jsfiddle.net/highcharts/V37Vv/light/
plotData <- data.frame(x = c(1,1,1,2,2,4,4,7,7,8),
y = c(1,1,1,3,6,5,2,1,1,1),
z = c(1,2,5,2,4,7,8,3,5,5),
category=rep(c("A", "B"), 5), stringsAsFactors=FALSE)
xLabel <- "X"
yLabel <- "Y"
zLabel <- "Z"
title <- "Title"
if (is.null(rownames(plotData))){
rownames(plotData) <- as.character(1:nrow(plotData))
}
# HighCharts point name
plotData$name <- rownames(plotData)
# Scatter plot
h1 <- rCharts::Highcharts$new()
h1$setLib("libraries/widgets/highcharts")
h1$chart(renderTo="container",
margin=c(150, 75, 75, 75),
type='scatter',
options3d=list(enabled=TRUE,
alpha=20,
beta=30,
depth=200))
h1$title(text=title)
h1$xAxis(title=list(enabled=TRUE, text=xLabel))
h1$yAxis(title=list(enabled=TRUE, text=yLabel))
h1$zAxis(title=list(enabled=TRUE, text=zLabel))
# Divide the dataset, split by category and put into list() format
# From: http://rcharts.io/viewer/?5735146#.VF6NS4W1Fy4
series <- lapply(split(plotData, plotData$category), function(x) {
res <- lapply(split(x, rownames(x)), as.list)
names(res) <- NULL
return(res)
})
invisible(sapply(series, function(x) {
h1$series(data=x, type="scatter", name=x[[1]]$category)
}
))
h1$legend(enabled=FALSE)
# Force circle markers and change size
h1$plotOptions(scatter=list(marker=list(symbol='circle', radius=6)))
h1$tooltip(formatter = "#! function() { return this.point.name + ', ' +
this.series.name + ', ' + this.x + ', ' + this.y; } !#")
# Enable exporting
h1$exporting(enabled=TRUE)
# Set name
h1$set(dom=xLabel)
# Print chart
print(h1)
#h1$show("inline")
Highcharts/rCharts 文件夹:libraries/widgets/highcharts
此文件夹包含一个 config.yml:
config.yml
highcharts:
jshead:
- js/jquery-1.7.min.js
- js/highcharts.js
- js/highcharts-more.js
- js/exporting.js
- js/highcharts-3d.js
cdn:
jshead:
- "http://code.jquery.com/jquery-1.7.min.js"
- "http://code.highcharts.com/highcharts.js"
- "http://code.highcharts.com/highcharts-more.js"
- "http://code.highcharts.com/highcharts-3d.js"
- "http://code.highcharts.com/modules/exporting.js"
js/文件夹
js/文件夹有config.yml中引用的文件
布局/文件夹
layouts/ 文件夹有 chart.html
chart.html
<script type='text/javascript'>
(function($){
$(function () {
var chart = new Highcharts.Chart({{{ chartParams }}});
});
})(jQuery);
</script>
【问题讨论】:
-
正如我从source code 看到的那样 - 不支持 zAxis。尝试删除:
h1$zAxis(title=list(enabled=TRUE, text=zLabel))行。您可能无法更改 zAxis,但图表仍然可以使用。 -
注释掉那条线会导致二维图,这不是预期的结果。
-
那么可能 rCharts lib 不支持 3D Highcharts。您是否生成了 Highcharts 代码/选项?检查哪里可能有问题。
标签: r highcharts 3d rcharts