【发布时间】:2016-07-04 07:46:22
【问题描述】:
我需要在闪亮的应用程序(highcharter、rcharts 等)上使用 R 的任何 highcharts 包为饼图添加图例。
例如。代码
library("shiny")
library("highcharter")
data(citytemp)
ui <- fluidPage(
h1("Highcharter Demo"),
fluidRow(
column(width = 4, class = "panel",
selectInput("type", label = "Type", width = "100%",
choices = c("line", "column", "bar", "spline")),
selectInput("stacked", label = "Stacked", width = "100%",
choices = c(FALSE, "normal", "percent")),
selectInput("theme", label = "Theme", width = "100%",
choices = c(FALSE, "fivethirtyeight", "economist",
"darkunica", "gridlight", "sandsignika",
"null", "handdrwran", "chalk")
)
),
column(width = 8,
highchartOutput("hcontainer",height = "500px")
)
)
)
server = function(input, output) {
output$hcontainer <- renderHighchart({
hc <- hc_demo() %>%
hc_rm_series("Berlin") %>%
hc_chart(type = 'pie')
if (input$stacked != FALSE) {
hc <- hc %>%
hc_plotOptions(showInLegend=TRUE,dataLabels=FALSE)
}
hc
})
}
shinyApp(ui = ui, server = server)
代码运行了一个糟糕的饼图示例,无论我在哪里看,我都找不到带有图例的 highcharts 饼图示例。
修复它的尝试包括:
hc_legend(enabled=TRUE)
hc_plotOptions(showInLegend=TRUE,dataLabels=FALSE)
使用 Rcharts 尝试过类似的尝试,他们都失败了,然后我在寻找 highcharts 的源代码时绝望地迷失了
使用类似的功能,我能够通过使用上述两种解决方案中的任何一种来创建带有典型 JS 格式图例的 highchart 饼图,有人对此问题有合理的解决方案吗?可能是一个链接到一个体面的资源来解决这个问题?
带有图例的highcharts的实际JS示例,无耻地从官方highcharts网站上撕下来。
$(function () {
$(document).ready(function () {
// Build the chart
$('#container').highcharts({
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie'
},
title: {
text: 'Browser market shares January, 2015 to May, 2015'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
showInLegend: true
}
},
series: [{
name: 'Brands',
colorByPoint: true,
data: [{
name: 'Microsoft Internet Explorer',
y: 56.33
}, {
name: 'Chrome',
y: 24.03,
sliced: true,
selected: true
}, {
name: 'Firefox',
y: 10.38
}, {
name: 'Safari',
y: 4.77
}, {
name: 'Opera',
y: 0.91
}, {
name: 'Proprietary or Undetectable',
y: 0.2
}]
}]
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<div id="container" style="min-width: 310px; height: 400px; max-width: 600px; margin: 0 auto"></div>
【问题讨论】:
-
你可以尝试准备自己的 HTML 图例,比如这里:jsfiddle.net/N3KAC/1
-
@SebastianBochan 我会告诉你进展如何。我也许可以将其合并到 R 或闪亮中。
-
对不起,我不熟悉 R gems 等;)
标签: javascript r highcharts rcharts