Last active
October 23, 2024 20:48
-
-
Save helgasoft/0618c6537c45bfd9e86d3f9e1da497b8 to your computer and use it in GitHub Desktop.
R | ECharts | custom or built-in locale; Graphic text and image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# set a custom locale in ECharts with echarty | |
# idea from @williamorim | |
# install.packages("remotes") | |
remotes::install_github("helgasoft/echarty") // v.1.6.4+ | |
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);" | |
ec.init(df, js=c(jscode,'',''), locale= 'PT', ctype= 'line', title= list(text='PT'), | |
xAxis= list(type= 'time', splitNumber= 12, boundaryGap= c('8%', '8%'), axisLabel= list(rotate=45)) | |
) | |
# ---------------- use predefined locale from ECharts | |
ec.init( df, | |
load= 'https://github.com/apache/echarts/raw/f3471f0a7080e68f8819f7b000d32d73fb0820fb/i18n/langFR.js', #URL | |
# load= 'file://c:/temp/langFR.js', # or local file | |
locale= 'FR', ctype= 'line', title= list(text='FR'), | |
xAxis= list(type= 'time', splitNumber= 12, boundaryGap= c('8%', '8%'), axisLabel= list(rotate=45)) | |
) |
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
))
))
)
if you like this solution, please consider granting a Github star ⭐ to echarty.
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)
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)
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
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.