There is a nue sunburst in town, and it is absolutely beautiful. The new charting library d2b
provides us with this delightful sunburst. d2b
also plays nicely with vuejs
, so of course we will use that version in this quick example from R.
look at that breadcrumb! - it's a separate component for use anywhere
check out resize - dang!!
jump in the console and change the data - pop!!
library(vueR)
library(d3r)
library(treemap)
library(htmltools)
hier_dat <- treemap::random.hierarchical.data()
hier_json <- d3r::d3_nest(
hier_dat,
value_cols = "x"
)
library(dplyr)
hier_json <- hier_dat %>%
treemap(index = c("index1", "index2", "index3"), vSize = "x") %>%
{.$tm} %>%
select(index1:index3,vSize, color) %>%
rename(size = vSize) %>%
d3_nest(value_cols = c("size", "color"))
browsable(tagList(
html_dependency_vue(),
tags$script(src = "https://unpkg.com/d3"),
tags$script(src = "https://unpkg.com/d2b@0.5.1/build/d2b.js"),
tags$script(src = "https://unpkg.com/vue-d2b"),
tags$div(
id = "app",
style = "height:400px",
tag(
"sunburst-chart",
list(":data" = "sunburstChartData", ":config" = "sunburstChartConfig")
)),
tags$script(HTML(
sprintf(
"
var app = new Vue({
el: '#app',
components: {
'sunburst-chart': vued2b.ChartSunburst
},
data: {
sunburstChartData: %s,
sunburstChartConfig: function(chart) {
var color = d3.scaleOrdinal(d3.schemeCategory20c);
chart.label(function(d){return d.name});
//chart.sunburst().size(function(d){return d.x});
//chart.color(function(d){return color(d.name);})
chart.color(function(d){return typeof(d.color) === 'undefined' ? '#BBB' : d.color; })
}
}
})
",
hier_json
)
))
))
Thanks to the d2b
author Kevin Warne for this fine piece of craftmanship and his pleasant responsiveness (see issue).