Animated transitions between charts. Morphing views show
different aspects of the data.
Data is global
fish production for 2018 - the last year with complete data for most
countries.
First chart is a bar chart. User can select Aquaculture or
Capture type from legend as filter for next charts. Defaut is
Capture.
R source code is behind the ➟ button
library(dplyr)
data <- readRDS(url('https://helgasoft.github.io/echarty/img/fish.data.zip')) # RDS binary
# get Top10 countries sorted by total production
df <- aggregate(data$value, by=list(name=data$name), FUN=sum) |> arrange(-x)
t10 <- df |> slice_max(x, n=10) |> select(name) |> unlist() |> unname()
other <- data |> filter(!name %in% t10)
other <- aggregate(other$value, by=list(aorc=other$aorc), FUN=sum) |>
rename(value=x) |> mutate(name='Other', value=round(value))
bdata <- data |> filter(name %in% t10) |> group_by(name) |>
mutate(sort= sum(value)) |> arrange(-sort) |> select(-sort) # sort by group total
bdata <- rbind(bdata, other) |> ungroup()
mup <- function(filt) {
tmp <- bdata |> filter(aorc==filt) |> arrange(value) |> select(name,value)
tt <- list()
for(v in 1:nrow(tmp)) { tt <- append(tt, list(
list(label= unname(unlist(tmp[v,'name'])),
value= unname(unlist(tmp[v,'value'])),
color= palet[v]
))) }
list(tmp=tmp, tt=tt)
}
# --------------------- constants
year <- '2018' # last year with complete data for most countries
toolbox <- list(
right= '10%', top= 3, backgroundColor= 'beige',
feature= list(mySwitcher= list(
show= TRUE, title= 'switch',
icon= 'path://M4 8a.5.5 0 0 1 .5-.5h5.793L8.146 5.354a.5.5 0 1 1 .708-.708l3 3a.5.5 0 0 1 0 .708l-3 3a.5.5 0 0 1-.708-.708L10.293 8.5H4.5A.5.5 0 0 1 4 8z',
onclick= htmlwidgets::JS("function() { toggleOpt(); }"))
))
# JavaScript code for the above switch button
jscode <- "curo= 0; isCatch= true;
toggleOpt= function () {
opt= chart.getOption();
curo= ++curo % 3;
i= curo;
if (i>0) { if (isCatch) i= i+2; }
else isCatch= true; // back to default
optcurr= Object.assign({}, opt.all5[i]);
optcurr.all5= opt.all5;
chart.setOption(optcurr, true);
};
chart.on('legendselectchanged', function(params) {
isCatch = params.selected.Capture;
});"
palet <- c('#FFF8C9','#FEEAA2','#FED677','#FDBB47','#FA9928','#EF7818','#D85A08','#B74203','#8E3004','#662506','#421508')
delay <- htmlwidgets::JS("_ => Math.random() * 1000;") # random delay time for parts
library(echarty)
# -------------------- bar chart
tmp <- bdata |> group_by(aorc) |> ec.init() # preset 3 datasets, use below
opt1 <- list(
title= list(text= paste(year,'World Fish Production','Top10'),
subtext= 'Select type from legend for next charts (defaut Capture)'),
xAxis= list(type='category', axisLabel= list(rotate= 45)),
yAxis= list(show=TRUE, name= 'million metric tons',
nameRotate= 90, nameGap= 35, nameLocation= 'center',
axisLabel= list(formatter= ec.clmn('%R@', -1, scale=0.000001))),
dataset= tmp$x$opts$dataset,
series= list(
list(type='bar', name= 'Aquaculture', datasetIndex= 1, id= 'anim1',
universalTransition= list(enabled= TRUE, delay= delay)),
list(type='bar', name= 'Capture', datasetIndex= 2)
),
legend= list(show= TRUE, top= 30, right= 15),
toolbox= toolbox,
tooltip= list(trigger='item', formatter= ec.clmn("<h3>%@</h3>%LR@ mt", 1,2))
)
# -------------------- 2 pie charts
tmp <- ec.data(bdata |> filter(aorc=='ER.FSH.AQUA.MT') |> arrange(value), 'names')
opt2 <- list(
title = list(text= paste(year,'Fish Aquaculture','Top10 in metric tons'),
subtext= 'Data source: World Bank',
sublink= 'https://datacatalog.worldbank.org/search/dataset/0037712'),
series = list(type= 'pie',
id= 'anim1', data= tmp,
label= list(position= 'inside',
formatter= ec.clmn("%@\n%L@ ",'name','value')),
universalTransition= list(enabled= TRUE, delay= delay)
),
toolbox= toolbox, tooltip= list(formatter= '{d}%'),
color= palet
)
tmp <- ec.data(bdata |> filter(aorc=='ER.FSH.CAPT.MT'), 'names')
opt4 <- opt2
opt4$title$text <- paste(year,'Fish Capture','Top10 in m.tons')
opt4$series$data <- tmp
# -------------------- 3 map charts
dd <- mup('ER.FSH.AQUA.MT')
opt3 <- list(
title= list(text=paste(year,'Fish Aquaculture')),
geo= list(list(map= 'world', roam= TRUE, zoom= 1.5)),
series= list(list(
type='map', id= 'anim1', geoIndex=0, # geoIndex not parsed, set as JS
data= ec.data(dd$tmp, 'names'),
universalTransition= list(enabled= TRUE, delay= delay)
)),
visualMap= list(type='piecewise', pieces= dd$tt),
toolbox= toolbox,
tooltip= list(valueFormatter= ec.clmn('%LR@', -1, scale=0.001))
)
dd <- mup('ER.FSH.CAPT.MT')
opt5 <- opt3
opt5$title <- list(text=paste(year,'Fish Capture'))
opt5$series[[1]]$data <- ec.data(dd$tmp, 'names')
opt5$visualMap <- list(type='piecewise', pieces= dd$tt)
library(echarty) # display
p <- ec.init(load= 'world', preset= FALSE, js= c('',jscode))
p$x$opts <- opt1
p$x$opts$all5 <- list(opt1,opt2,opt3,opt4,opt5)
p
Note: Map has zoom and pan. Hovering the map slider filters countries by value and shows them in yellow.