1
- library(shiny )
2
1
library(treemap )
3
2
library(d3r )
4
3
library(htmltools )
5
4
6
5
# set up dependency for d2bjs chart library
7
6
d2b_dep <- htmltools :: htmlDependency(
8
7
name = " d2b" ,
9
- version = " 0.0.24 " ,
10
- src = c(
href = " https://unpkg.com/[email protected] .24 /build/" ),
8
+ version = " 0.0.28 " ,
9
+ src = c(
href = " https://unpkg.com/[email protected] .28 /build/" ),
11
10
script = " d2b.min.js"
12
11
)
13
12
@@ -103,46 +102,56 @@ Vue.component('treemap-component', {
103
102
104
103
105
104
106
- app <- tags $ script(HTML("
105
+ app <- tags $ script(HTML(
106
+ sprintf(
107
+ "
108
+ // try to keep color consistent across charts
109
+ // so use global color function
110
+ var color = d3.scaleOrdinal(d3.schemeCategory20);
111
+
107
112
// careful here in a real application
108
113
// set up a global/window store object to hold state
109
114
// will be a simple object
115
+ var tree = %s;
110
116
var store = {
111
- tree: {},
117
+ tree: tree,
118
+ filtered_tree: tree,
112
119
size: 'x',
113
120
width: 800,
114
121
height: 600,
115
122
tile: d3.treemapBinary,
123
+ color: color,
116
124
sunburstChartConfig: function(chart) {
117
125
chart.label(function(d){return d.name});
126
+ chart.color(function(d){return color(d.name);})
118
127
chart.sunburst().size(function(d){return d.x});
119
128
}
120
129
};
121
130
122
- // add a very simple function that will update our store object
123
- // with the x data provided
124
- Shiny.addCustomMessageHandler(
125
- 'updateStore',
126
- function(x) {
127
- // mutate store data to equal the x argument
128
- store[x.field] = x.data;
129
- console.log('changed to ' + x);
130
- }
131
- );
132
-
133
- $(document).on(
134
- 'shiny:connected',
135
- function() {
136
- var app = new Vue({
137
- el: '#app',
138
- components: {
139
- 'sunburst-chart': d2b.vueChartSunburst
140
- },
141
- data: store
142
- })
131
+ var app = new Vue({
132
+ el: '#app',
133
+ components: {
134
+ 'sunburst-chart': d2b.vueChartSunburst
135
+ },
136
+ data: store,
137
+ methods: {
138
+ sunburstChartRendered: function (el, chart) {
139
+ var that = this;
140
+ d3.select(el).selectAll('.d2b-sunburst-chart')
141
+ .on('mouseover', function (d) {
142
+ if(d3.event.target.classList[0] === 'd2b-sunburst-arc'){
143
+ that.filtered_tree = d3.select(d3.event.target).datum().data;
144
+ }
145
+ });
146
+ }
143
147
}
148
+ })
149
+ " ,
150
+ d3r :: d3_nest(
151
+ treemap :: random.hierarchical.data(depth = 4 ),
152
+ value_cols = " x"
144
153
)
145
- "
154
+ )
146
155
))
147
156
148
157
@@ -155,14 +164,18 @@ ui <- tagList(
155
164
style = " height:400px; width:400px; float:left;" ,
156
165
tag(
157
166
" sunburst-chart" ,
158
- list (" :data" = " tree" , " :config" = " sunburstChartConfig" )
167
+ list (
168
+ " :data" = " tree" ,
169
+ " :config" = " sunburstChartConfig" ,
170
+ " @rendered" = " sunburstChartRendered"
171
+ )
159
172
)
160
173
),
161
174
tags $ div(
162
175
style = " height:400px; width:400px; float:left;" ,
163
176
tag(
164
177
" treemap-component" ,
165
- list (" :tree" = " tree " ," :sizefield" = " 'x'" ) # use defaults
178
+ list (" :tree" = " filtered_tree " ," :sizefield" = " 'x'" , " :color " = " color " ) # use defaults
166
179
)
167
180
)
168
181
),
@@ -172,22 +185,4 @@ ui <- tagList(
172
185
d2b_dep
173
186
)
174
187
175
- server <- function (input , output , session ) {
176
- observe({
177
- # every 1 second invalidate our session to trigger update
178
- invalidateLater(1000 , session )
179
- # send a message to update store with a random value
180
- session $ sendCustomMessage(
181
- type = " updateStore" ,
182
- message = list (
183
- field = " tree" ,
184
- data = d3r :: d3_nest(
185
- treemap :: random.hierarchical.data(),
186
- value_cols = " x"
187
- )
188
- )
189
- )
190
- })
191
- }
192
-
193
- shinyApp(ui , server )
188
+ browsable(ui )
0 commit comments