ecStat is a
statistical and data mining plugin for ECharts.
It is included in echarty and this is sample code on how to use it.
#' see https://echarts.apache.org/examples/en/editor.html?c=scatter-clustering
#' from https://github.com/ecomfe/echarts-stat
set.seed(444)
data <- matrix(round(runif(28)*5, 2), ncol= 2)
s1 <- list(type='scatter', symbolSize=15, datasetIndex=1, encode=list(tooltip=list(0, 1)))
plt <- function(data, name='name?') {
if (name=='reg') {
s2 <- list(type='line', symbolSize=0, smooth=TRUE, color='red', datasetIndex=2)
p <- ec.init(load= 'ecStat',
#js= c('echarts.registerTransform(ecStat.transform.regression)','',''),
dataset= list(
list(source= data),
list(transform= list(type='ecStat:regression',
config= list(method= 'polynomial', order= 3)) )
),
xAxis=list(show=T), yAxis=list(show=T), tooltip= list(position='top'),
series= list(s1, s2)
)
}
if (name=='hist') {
s2 <- list(type='bar', datasetIndex=2, barWidth='97%',
xAxisIndex=2, yAxisIndex=2, label=list(show=F))
p <- ec.init( load='ecStat',
dataset = list(
list(source= data),
list(transform= list(type= 'ecStat:histogram'),
config= list(dimensions=1))
),
tooltip= list(position='top'),
grid= list(list(bottom='62%', top=11), list(top= '44%', bottom=22))
,xAxis = list(list(gridIndex=1, scale=T),
list(gridIndex=2, scale=T, type= 'category'))
,yAxis = list(list(gridIndex=1), list(gridIndex= 2))
,series = list(s1, s2)
)
}
if (name=='cluster') {
s2 <- s1; s2$datasetIndex <- 2
CLUSTER_COUNT <- 3
COLOR_ALL <- c('#37A2DA','#e06343','#37a354','#b55dba','#b5bd48','#8378EA','#96BFFF')
pieces <- list();
for (i in 1:CLUSTER_COUNT) pieces <- append(pieces,
list(list(value=i-1, label=paste('cluster',i), color=COLOR_ALL[i])))
p <- ec.init(load= 'ecStat',
dataset = list(
list(source= data),
list(transform= list(type='ecStat:clustering',
config=list( clusterCount= CLUSTER_COUNT, outputType='single',
outputClusterIndexDimension=2)))),
tooltip = list(position='top'),
grid = list(left=120), xAxis=list(show=T), yAxis=list(show=T),
visualMap = list(type='piecewise', top='middle', show=TRUE,
min=0, max=CLUSTER_COUNT, left=10, splitNumber=CLUSTER_COUNT,
dimension=3, pieces=pieces),
series = list(s2)
)
}
p$x$opts$name <- name # for one option of the dropdown
p
}
p1 <- plt(data, 'reg')
p2 <- plt(data, 'cluster')
p3 <- plt(data, 'hist')
p1$x$o2 <- p2$x$opts # save the 2nd plot
p1$x$o3 <- p3$x$opts # save the 3rd plot
p1 # show 1st
Skipping Shiny server again. Here is the JavaScript code
var chopt = {p1:null, p2:null, p3:null};
function getData() {
try {
// find 1st htmlwidget, easy when there is only one ;-)
const inlineJsonElement = document.querySelector(
`script[type="application/json"][data-for^=htmlwidget-]`
);
const data = JSON.parse(inlineJsonElement.textContent);
return data;
} catch (err) {
console.error(`Couldn't read JSON data from htmlwidget`, err);
}
}
function rlick(myRadio) {
//const rbs = document.querySelectorAll('input[name="stat"]');
//let s = document.querySelector('input[name="stat"]:checked').value;
let selectedValue = myRadio.value;
var wt = document.querySelector("[id^=htmlwidget-]");
// get the ECharts object and replace options
var p = get_e_charts(wt.id);
switch(selectedValue) {
case chopt.p1.name:
p.setOption(chopt.p1, true);
break;
case chopt.p2.name:
p.setOption(chopt.p2, true);
break;
case chopt.p3.name:
p.setOption(chopt.p3, true);
break;
default:
alert('selection not found');
}
};
document.addEventListener("DOMContentLoaded", function() {
const pp = getData();
chopt.p1 = pp.x.opts;
chopt.p2 = pp.x.o2;
chopt.p3 = pp.x.o3;
});