Skip to content

Instantly share code, notes, and snippets.

@helgasoft
Last active Aug 9, 2022
Embed
What would you like to do?
R | ECharts | custom or built-in locale; Graphic text and image
# set a custom locale in ECharts with echarty
# idea from @williamorim
# install.packages("remotes")
remotes::install_github("helgasoft/echarty")
library(echarty)
library(lubridate)
df <- data.frame(date=as.Date('2019-12-31') %m+% months(1:13), num=runif(13))
jscode <- "var localeObj = {
time: {
month: [
'janeiro', 'fevereiro', 'março', 'abril', 'maio', 'junho',
'julho', 'agosto', 'setembro', 'outubro', 'novembro', 'dezembro'
],
monthAbbr: [
'jan', 'fev', 'mar', 'abr', 'maio', 'jun',
'jul', 'ago', 'set', 'out', 'nov', 'dez'
],
dayOfWeek: [
'Domingo', 'Segunda-feira', 'Terça-feira', 'Quarta-feira', 'Quinta-feira', 'Sexta-feira', 'Sábado'
],
dayOfWeekAbbr: [
'Dom', '2ª', '3ª', '4ª', '5ª', '6ª', 'Sab'
]
},
// below is in Spanish, needs translation to Portuguese...
// see also English - https://github.com/apache/echarts/blob/f3471f0a7080e68f8819f7b000d32d73fb0820fb/i18n/langEN.js
legend: {
selector: {
all: 'Todas',
inverse: 'Inversa'
}
},
toolbox: {
brush: {
title: {
rect: 'Selección de cuadro',
polygon: 'Selección de lazo',
lineX: 'Seleccionar horizontalmente',
lineY: 'Seleccionar verticalmente',
keep: 'Mantener selección',
clear: 'Despejar selecciones'
}
},
dataView: {
title: 'Ver datos',
lang: ['Ver datos', 'Cerrar', 'Actualizar']
},
dataZoom: {
title: {
zoom: 'Zoom',
back: 'Restablecer zoom'
}
},
magicType: {
title: {
line: 'Cambiar a gráfico de líneas',
bar: 'Cambiar a gráfico de barras',
stack: 'Pila',
tiled: 'Teja'
}
},
restore: {
title: 'Restaurar'
},
saveAsImage: {
title: 'Guardar como imagen',
lang: ['Clic derecho para guardar imagen']
}
}
};
echarts.registerLocale('PT', localeObj);"
p <- df %>% ec.init(js= jscode, locale= 'PT', ctype= 'line', title= list(text='PT'))
p$x$opts$xAxis <- list(type= 'time', splitNumber= 12,
boundaryGap= c('8%', '8%'), axisLabel= list(rotate=45))
p
# ---------------- use predefined locale from ECharts
library(lubridate)
df <- data.frame(date=as.Date('2019-12-31') %m+% months(1:13), num=runif(13))
p <- df |> ec.init(locale= 'FR', ctype= 'line', title= list(text='FR')) |>
ec.plugjs('https://github.com/apache/echarts/raw/f3471f0a7080e68f8819f7b000d32d73fb0820fb/i18n/langFR.js') # URL
# ec.plugjs('file://c:/temp/langFR.js') # or local file
p$x$opts$xAxis <- list(type= 'time', splitNumber= 12,
boundaryGap= c('8%', '8%'), axisLabel= list(rotate=45))
p
@helgasoft
Copy link
Author

helgasoft commented Sep 16, 2021

Set custom/predefined locale with echarty, idea from @williamorim.
PT (Portugal) is custom, FR (France) is predefined. As of ECharts v.5.3.2 the predefined locales are CS, DE, EN, ES, FI, FR, IT, JA, KO, PL, PT-br, RO, RU, SI, TH, ZH.
NL (Dutch) should be made custom - inquiry from @rdatasculptor.

If you like this solution, please consider granting a Github star to echarty.

@helgasoft
Copy link
Author

helgasoft commented Jul 27, 2022

Graphic with text and image, inquiry by @issactoast

example_df <- data.frame(type = c("a", "b"), value = c(10, 20))

library(echarty)
example_df |> ec.init(ctype= 'bar',
    graphic= list(elements= list(
        list(type= 'text', top= 'top', left= '33%',
	     style= list(text= 'echarty',fontSize= 50, fontStyle= 'italic')),
        list(type= 'image', 	style= list(
             image= "https://www.r-project.org/logo/Rlogo.png",
             width= 150, height= 150, opacity= .6
        ))
    ))
)

image
if you like this solution, please consider granting a Github star to echarty.

@helgasoft
Copy link
Author

helgasoft commented Aug 8, 2022

Layout with two pie charts, inquiry by @DavZim.

library(dplyr)
df <- tibble(
	name = rep(c("v1", "v2", "v3"), 2), # pie native 'axes' are name/value
	value = c(20, 40, 40, 50, 30, 20),
	group = rep(c("A", "B"), each = 3)  # echarty currently requires group column to be last
)

library(echarty)
p <- lapply(df |> group_by(group) |> group_split(), function(x) { 
	ec.init(preset= FALSE, height= 250,
		title= list(text= unique(x$group)), 
		series= list(list(type= 'pie', data= ec.data(x, 'names'))),
                legend= list(show=TRUE))
})
ec.layout(p, cols=2)	

image

df2 <- tibble(
	group = rep(c("A", "B"), each = 100),
	time = rep(1:100, 2)
) %>% mutate(value = time + rnorm(100) + ifelse(group == "A", 10, 0)) %>% 
	select(time,value,group)

library(echarty)
p <- lapply(df2 |> group_by(group) |> group_split(), function(gd) { 
	tmp <- lm(value ~ time, gd)
	ec.init( 
		height= 300, title= list(text= unique(gd$group)), 
		xAxis= list(show=TRUE), yAxis=list(show=TRUE),
		series= list(
		  list(type= 'scatter', data= ec.data(gd[,1:2]), symbolSize=3, name='data'),
		  list(type='line', data= ec.data(data.frame(gd$time, tmp$fitted.values)),
		  	  showSymbol= FALSE, lineStyle= list(width=1, color='red'), name='lm')
		),
		legend= list(show=TRUE))
})
ec.layout(p, cols=2)

image

if you like this solution, please consider granting a Github star to echarty.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment