Skip to content

Instantly share code, notes, and snippets.

@helgasoft
Last active Jun 7, 2022
Embed
What would you like to do?
R | ECharts | init with custom or predefined locale
# 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.

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