Helper function to display/format data column(s) by index or name
Arguments
- col
A single column index(number) or column name(quoted string),
or a sprintf string template for multiple columns.
NULL(default) for charts with single values like tree, pie.
'json' to display tooltip with all available values to choose from.
'log' to write all values in the JS console (F12) for debugging.
Can contain JS function starting with 'function(' or '(x) => {'.- ...
Comma separated column indexes or names, only when col is sprintf. This allows formatting of multiple columns, as for a tooltip.
- scale
A positive number, multiplier for numeric columns. When scale is 0, all numeric values are rounded.
Value
A JavaScript code string (usually a function) marked as executable, see JS.
Details
This function is useful for attributes like formatter, color, symbolSize, label.
Column indexes are counted in R and start with 1.
Omit col or use index -1 for single values in tree/pie charts, axisLabel.formatter or valueFormatter. See ec.data dendrogram example.
Column indexes are decimals for combo charts with multiple series, see ecr.band example. The whole number part is the serie index, the decimal part is the column index inside.
col as sprintf has the same placeholder %@ for both column indexes or column names.
col as sprintf can contain double quotes, but not single or backquotes.
Template placeholders with formatting:
%@ will display column value as-is.
%L@ will display a number in locale format, like '12,345.09'.
%LR@ rounded number in locale format, like '12,345'.
%R@ rounded number, like '12345'.
%R2@ rounded number, two digits after decimal point.
%M@ marker in series' color.
For trigger='axis' (multiple series) one can use decimal column indexes.
See definition above and example below.
Examples
library(dplyr)
tmp <- data.frame(Species = as.vector(unique(iris$Species)),
emoji = c('A','B','C'))
df <- iris |> inner_join(tmp) # add 6th column emoji
#> Joining with `by = join_by(Species)`
df |> group_by(Species) |> ec.init(
series.param= list(label= list(show= TRUE, formatter= ec.clmn('emoji'))),
tooltip= list(formatter=
# with sprintf template + multiple column indexes
ec.clmn('%M@ species <b>%@</b><br>s.len <b>%@</b><br>s.wid <b>%@</b>', 5,1,2))
)
# tooltip decimal indexes work with full data sets (no missing/partial data)
ChickWeight |> mutate(Chick=as.numeric(Chick)) |> filter(Chick>47) |> group_by(Chick) |>
ec.init(
tooltip= list(trigger='axis',
formatter= ec.clmn("48: %@<br>49: %@<br>50: %@", 1.1, 2.1, 3.1)),
xAxis= list(type='category'), legend= list(formatter= 'Ch.{name}'),
series.param= list(type='line', encode= list(x='Time', y='weight')),
)